Skip to content

[LANG-1774]: Improve getCanonicalName robustness and error handling#1422

Closed
Wanyi9988 wants to merge 2 commits intoapache:masterfrom
Wanyi9988:LANG-1774-fix-bug
Closed

[LANG-1774]: Improve getCanonicalName robustness and error handling#1422
Wanyi9988 wants to merge 2 commits intoapache:masterfrom
Wanyi9988:LANG-1774-fix-bug

Conversation

@Wanyi9988
Copy link
Copy Markdown

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

- 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
@Wanyi9988 Wanyi9988 changed the title LANG-1774: Improve getCanonicalName robustness and error handling [LANG-1774]: Improve getCanonicalName robustness and error handling Aug 1, 2025
Copy link
Copy Markdown
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the example format.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed~

*/

private static String getCanonicalName(final String name) {
if (StringUtils.isBlank(name)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Wanyi9988
Copy link
Copy Markdown
Author

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!

@xenoterracide
Copy link
Copy Markdown
Contributor

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

@garydgregory
Copy link
Copy Markdown
Member

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

@garydgregory
Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants