You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/06/20 01:28:25 UTC

[GitHub] [commons-lang] johnson-abraham opened a new pull request #773: Object Casting

johnson-abraham opened a new pull request #773:
URL: https://github.com/apache/commons-lang/pull/773


   Casts an object from one type to the specified type in a null-safe manner.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-lang] coveralls edited a comment on pull request #773: Object Casting

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #773:
URL: https://github.com/apache/commons-lang/pull/773#issuecomment-864486254


   
   [![Coverage Status](https://coveralls.io/builds/40722227/badge)](https://coveralls.io/builds/40722227)
   
   Coverage increased (+0.0003%) to 94.976% when pulling **772654c7f167df7bae5b54e7e69c1d7882b45bf8 on johnson-abraham:obj_cast** into **197d50434748bfb2db935266cfe740fc01a607ee on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-lang] coveralls commented on pull request #773: Object Casting

Posted by GitBox <gi...@apache.org>.
coveralls commented on pull request #773:
URL: https://github.com/apache/commons-lang/pull/773#issuecomment-864486254


   
   [![Coverage Status](https://coveralls.io/builds/40722113/badge)](https://coveralls.io/builds/40722113)
   
   Coverage increased (+0.0009%) to 94.977% when pulling **d1a70b4cd142dd56aa841b87d8d3f19efab7d12f on johnson-abraham:obj_cast** into **197d50434748bfb2db935266cfe740fc01a607ee on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-lang] garydgregory commented on a change in pull request #773: Object Casting

Posted by GitBox <gi...@apache.org>.
garydgregory commented on a change in pull request #773:
URL: https://github.com/apache/commons-lang/pull/773#discussion_r654951361



##########
File path: src/main/java/org/apache/commons/lang3/ObjectUtils.java
##########
@@ -228,6 +227,18 @@ public static boolean anyNull(final Object... values) {
         return !allNotNull(values);
     }
 
+    /**
+     * Casts the object to the targeted class.
+     * @param value the object to cast
+     * @param castClass the class to which the object must be cast to
+     * @param <T> the type of the cast object
+     * @return the cast object, otherwise {@code null}
+     */

Review comment:
       New main public and protected methods need a Javadoc since tag.

##########
File path: src/main/java/org/apache/commons/lang3/ObjectUtils.java
##########
@@ -228,6 +227,18 @@ public static boolean anyNull(final Object... values) {
         return !allNotNull(values);
     }
 
+    /**
+     * Casts the object to the targeted class.
+     * @param value the object to cast
+     * @param castClass the class to which the object must be cast to
+     * @param <T> the type of the cast object
+     * @return the cast object, otherwise {@code null}
+     */
+    public static <T> T cast(Object value, Class<T> castClass) {
+        return (castClass != null) && castClass.isInstance(value) ? castClass.cast(value) : null;

Review comment:
       Match the style of the file: Use final where possible, no need for extra parentheses.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-lang] johnson-abraham closed pull request #773: Object Casting

Posted by GitBox <gi...@apache.org>.
johnson-abraham closed pull request #773:
URL: https://github.com/apache/commons-lang/pull/773


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-lang] garydgregory commented on pull request #773: Object Casting

Posted by GitBox <gi...@apache.org>.
garydgregory commented on pull request #773:
URL: https://github.com/apache/commons-lang/pull/773#issuecomment-864571151


   There is a mismatch here between the code, the Javadoc, and the PR description. Where is the actual intent expressed? Specifically, the description and Javadoc do not state that class cast exceptions are effectively escaped into null returned values. This is quite unexpected since the PR and Javadoc only talk about the method as a null-safe shorthand utility. 
   
   If the method is to only be a null-safe shorthand wrapper then 'cast' is fine as the method name, but if the internet is to add logic to return null values for illegal casts, then the name needs to be different in order to allow for a method that only does the null safe check but allows for CCE.
   
   In the end, the real value of such a low-level shorthand method is better evaluated if Commons Lang itself is made to use it internally.  Are there sites within Lang that can make use of this utility?
   
   I'll sprinkle comments in the code as well.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org