[LANG-1774]: Improve getCanonicalName robustness and error handling#1422
[LANG-1774]: Improve getCanonicalName robustness and error handling#1422Wanyi9988 wants to merge 2 commits intoapache:masterfrom
Conversation
- Added explicit validation for invalid class name inputs in getCanonicalName - Ensures consistent IllegalArgumentException is thrown for malformed array or primitive type encodings - Rejects invalid or partial array descriptors (e.g. "[", "[]", "[[L") with clear error messages - Preserves original behavior for valid inputs - Returns empty string for null or blank input, consistent with getShortCanonicalName contract - Added corresponding unit tests to assert expected behavior for invalid inputs
garydgregory
left a comment
There was a problem hiding this comment.
Hello @xenoterracide
You didn't follow the PR checklist: You must get the PR to pass 'mvn' to pass (no arguments).
| * <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> |
There was a problem hiding this comment.
Please don't change the example format.
| */ | ||
|
|
||
| private static String getCanonicalName(final String name) { | ||
| if (StringUtils.isBlank(name)) { |
There was a problem hiding this comment.
This doesn't seem right. How is " " a valid class name? Or null for that matter...
- Declare all non-mutated local variables as `final` to satisfy FinalLocalVariable rule - Remove trailing whitespace to pass RegexpSingleline check - Restore original JavaDoc example format to match project style - Preserve `StringUtils.isBlank(name)` check to return `""` for blank/null input, consistent with existing methods such as getShortClassName and getShortCanonicalName(final Class<?> cls) No functional logic change. 1 Fix Checkstyle violations in getCanonicalName method - Declare all non-mutated local variables as `final` to satisfy FinalLocalVariable rule - Remove trailing whitespace to pass RegexpSingleline check - Restore original JavaDoc example format to match project style - Preserve `StringUtils.isBlank(name)` check to return `""` for blank/null input, consistent with existing methods such as getShortClassName and getShortCanonicalName(final Class<?> cls) No functional logic change. add whitespace Fix Checkstyle violations in getCanonicalName method - Declare all non-mutated local variables as `final` to satisfy FinalLocalVariable rule - Remove trailing whitespace to pass RegexpSingleline check - Restore original JavaDoc example format to match project style - Preserve `StringUtils.isBlank(name)` check to return `""` for blank/null input, consistent with existing methods such as getShortClassName and getShortCanonicalName(final Class<?> cls) No functional logic change.
|
Thanks for your feedback! This new commit addresses the following Checkstyle violations: Note on null/blank input handling: That said, if there's a strong preference for throwing an IllegalArgumentException instead, I’d be glad to adjust! |
I do not think that you meant to summon me. All future summons must come with catered snacks!!! |
|
Closing in favor of #1437. Note that a blank class name is invalid. |
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