You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Wanyi9988 (via GitHub)" <gi...@apache.org> on 2025/07/31 15:29:08 UTC

[PR] LANG-1774: Improve getCanonicalName robustness and error handling [commons-lang]

Wanyi9988 opened a new pull request, #1422:
URL: https://github.com/apache/commons-lang/pull/1422

   **Summary**
   This pull request improves the robustness of the getCanonicalName(String name) method in ClassUtils by introducing explicit validation of malformed or invalid class name inputs. The method now handles edge cases more consistently and predictably.
   
    **Changes**
   Added validation logic to check the format of internal class names, especially array type descriptors.
   Ensures consistent IllegalArgumentException is thrown for:
   Incomplete or malformed array types such as "[", "[]", "[;"
   Unknown primitive type encodings like "[$" or "[$a"
   Incorrect object array types like "[[L", "[[Iorg.apache.commons...", etc.
   Returns StringUtils.EMPTY when input is null, empty, or blank
   Replaced implicit exceptions (e.g. StringIndexOutOfBoundsException) with clear, controlled error messages.
   Added unit tests for invalid input scenarios to ensure expected exception behavior and coverage.
   
    **Motivation**
   The original implementation:
   Lacked clear documentation or consistent behavior on invalid descriptors.
   Returned null for null input, but threw exceptions for empty/blank input.
   Threw uncaught runtime exceptions (like IndexOutOfBoundsException) for malformed inputs due to lack of validation.
   This PR unifies and improves error handling, improving maintainability, predictability, and test coverage.
   
    **Tests**
   Added tests for previously unhandled malformed inputs.
   Confirmed all existing and new test cases pass with `mvn clean verify`.
   
   ----------------------------------------------
   This PR description was generated with the help of ChatGPT


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "Wanyi9988 (via GitHub)" <gi...@apache.org>.
Wanyi9988 commented on code in PR #1422:
URL: https://github.com/apache/commons-lang/pull/1422#discussion_r2249137489


##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -458,52 +458,90 @@ public static String getCanonicalName(final Object object, final String valueIfN
     }
 
     /**
-     * Converts a given name of class into canonical format. If name of class is not a name of array class it returns
-     * unchanged name.
-     *
+     * Converts a given internal class name into its canonical format.
      * <p>
-     * The method does not change the {@code $} separators in case the class is inner class.
+     * If the name is a valid Java array type descriptor (e.g., "[I", "[[Ljava.lang.String;"),
+     * it will be converted to a canonical form such as "int[]" or "java.lang.String[][]".
+     * If the name is a non-array class name (e.g., "java.lang.String"), it will be returned as-is
+     * after whitespace trimming.
      * </p>
      *
-     * <p>
-     * Example:
+     * <p>Special handling includes:
+     * <ul>
+     *   <li>{@code null} or blank input returns an empty string</li>
+     *   <li>Invalid or malformed input will result in an {@link IllegalArgumentException}</li>
+     * </ul>
+     * </p>
+     *
+     * <p>Examples:
      * <ul>
-     * <li>{@code getCanonicalName("[I") = "int[]"}</li>
-     * <li>{@code getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"}</li>
-     * <li>{@code getCanonicalName("java.lang.String") = "java.lang.String"}</li>
+     *   <li>{@code getCanonicalName("[I")} → {@code "int[]"}</li>

Review Comment:
   Fixed~



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #1422:
URL: https://github.com/apache/commons-lang/pull/1422#issuecomment-3243167293

   Tracking https://issues.apache.org/jira/browse/LANG-1774


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #1422:
URL: https://github.com/apache/commons-lang/pull/1422#discussion_r2247756618


##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -458,52 +458,90 @@ public static String getCanonicalName(final Object object, final String valueIfN
     }
 
     /**
-     * Converts a given name of class into canonical format. If name of class is not a name of array class it returns
-     * unchanged name.
-     *
+     * Converts a given internal class name into its canonical format.
      * <p>
-     * The method does not change the {@code $} separators in case the class is inner class.
+     * If the name is a valid Java array type descriptor (e.g., "[I", "[[Ljava.lang.String;"),
+     * it will be converted to a canonical form such as "int[]" or "java.lang.String[][]".
+     * If the name is a non-array class name (e.g., "java.lang.String"), it will be returned as-is
+     * after whitespace trimming.
      * </p>
      *
-     * <p>
-     * Example:
+     * <p>Special handling includes:
+     * <ul>
+     *   <li>{@code null} or blank input returns an empty string</li>
+     *   <li>Invalid or malformed input will result in an {@link IllegalArgumentException}</li>
+     * </ul>
+     * </p>
+     *
+     * <p>Examples:
      * <ul>
-     * <li>{@code getCanonicalName("[I") = "int[]"}</li>
-     * <li>{@code getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"}</li>
-     * <li>{@code getCanonicalName("java.lang.String") = "java.lang.String"}</li>
+     *   <li>{@code getCanonicalName("[I")} → {@code "int[]"}</li>

Review Comment:
   Please don't change the example format.



##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -458,52 +458,90 @@ public static String getCanonicalName(final Object object, final String valueIfN
     }
 
     /**
-     * Converts a given name of class into canonical format. If name of class is not a name of array class it returns
-     * unchanged name.
-     *
+     * Converts a given internal class name into its canonical format.
      * <p>
-     * The method does not change the {@code $} separators in case the class is inner class.
+     * If the name is a valid Java array type descriptor (e.g., "[I", "[[Ljava.lang.String;"),
+     * it will be converted to a canonical form such as "int[]" or "java.lang.String[][]".
+     * If the name is a non-array class name (e.g., "java.lang.String"), it will be returned as-is
+     * after whitespace trimming.
      * </p>
      *
-     * <p>
-     * Example:
+     * <p>Special handling includes:
+     * <ul>
+     *   <li>{@code null} or blank input returns an empty string</li>
+     *   <li>Invalid or malformed input will result in an {@link IllegalArgumentException}</li>
+     * </ul>
+     * </p>
+     *
+     * <p>Examples:
      * <ul>
-     * <li>{@code getCanonicalName("[I") = "int[]"}</li>
-     * <li>{@code getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"}</li>
-     * <li>{@code getCanonicalName("java.lang.String") = "java.lang.String"}</li>
+     *   <li>{@code getCanonicalName("[I")} → {@code "int[]"}</li>
+     *   <li>{@code getCanonicalName("[[Ljava.lang.String;")} → {@code "java.lang.String[][]"}</li>
+     *   <li>{@code getCanonicalName("java.lang.String")} → {@code "java.lang.String"}</li>
      * </ul>
      * </p>
      *
-     * @param name the name of class.
-     * @return canonical form of class name.
+     * @param name the internal name of a class
+     * @return canonical form of the class name, or an empty string if input is blank or null
+     * @throws IllegalArgumentException if the class name is invalid or malformed
      */
+
     private static String getCanonicalName(final String name) {
+        if (StringUtils.isBlank(name)) {

Review Comment:
   This doesn't seem right. How is "    " a valid class name? Or null for that matter...



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory closed pull request #1422: [LANG-1774]: Improve getCanonicalName robustness and error handling
URL: https://github.com/apache/commons-lang/pull/1422


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #1422:
URL: https://github.com/apache/commons-lang/pull/1422#issuecomment-3243166308

   Closing in favor of #1437. Note that a blank class name is invalid.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "Wanyi9988 (via GitHub)" <gi...@apache.org>.
Wanyi9988 commented on PR #1422:
URL: https://github.com/apache/commons-lang/pull/1422#issuecomment-3146253881

   Thanks for your feedback!
   
   This new commit addresses the following Checkstyle violations:
   FinalLocalVariable – local variables are now declared as final
   RegexpSingleline – trailing spaces have been removed
   JavaDoc formatting – examples were adjusted to match existing conventions
   
   Note on null/blank input handling:
   I’ve kept the use of StringUtils.isBlank(name) so that the method returns "" when the input is null or only whitespace, aligning with the behavior of other utility methods like getShortClassName(String className) and getShortCanonicalName(final Class<?> cls).
   This approach avoids unexpected exceptions and preserves backward compatibility.
   
   That said, if there's a strong preference for throwing an IllegalArgumentException instead, I’d be glad to adjust!


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]

Posted by "xenoterracide (via GitHub)" <gi...@apache.org>.
xenoterracide commented on PR #1422:
URL: https://github.com/apache/commons-lang/pull/1422#issuecomment-3146257875

   > Hello @xenoterracide 
   > You didn't follow the PR checklist: You must get the PR to pass 'mvn' to pass (no arguments).
   
   I do not think that you meant to summon me. All future summons must come with catered snacks!!!


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

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