You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "garydgregory (via GitHub)" <gi...@apache.org> on 2025/08/01 12:00:19 UTC
Re: [PR] [LANG-1774]: Improve getCanonicalName robustness and error handling [commons-lang]
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