Support arbitrary levels of nested class names in ClassUtils.forName()#37021
Support arbitrary levels of nested class names in ClassUtils.forName()#37021guanchengang wants to merge 1 commit into
ClassUtils.forName()#37021Conversation
Prior to this commit, ClassUtils#forName only performed a single-level conversion when resolving nested class names in Java source style (e.g. "java.lang.Thread.State"). For multi-level nested classes such as "com.example.Outer.Middle.Inner", the method would fail to resolve the actual class "com.example.Outer$Middle$Inner" because only the last dot was converted to a dollar sign. This commit improves the resolution by introducing a loop that iteratively converts dots to dollar signs from right to left, until the class is found or no further conversion is possible. Additionally, if the given name already contains a dollar sign, the method now fails fast without attempting unnecessary conversions. Signed-off-by: Chengang Guan <guanchengang@qq.com>
6bcad67 to
4b0e0fa
Compare
ClassUtils.forName()
|
Hi spring-team, The issue:This modification caused After investigation, I found the root cause: the test uses mixed-format nested class names like However, my PR added a fast-fail logic that rejects names already containing The inconsistency in the original implementation: Under the current implementation, class loading behaves inconsistently:
This is clearly inconsistent: if we support mixed formats, we should also support pure source-style multi-level names. If we don't, then mixed formats should not work either. Questions for discussion:Should mixed-format names like
If no:
I'm happy to adapt the PR based on the team's decision. Please let me know your thoughts. Thanks! |
Prior to this commit,
ClassUtils#forNameonly performed a single-level conversion when resolving nested class names in Java source style (e.g. "java.lang.Thread.State"). For multi-level nested classes such as "com.example.Outer.Middle.Inner", the method would fail to resolve the actual class "com.example.Outer$Middle$Inner" because only the last dot was converted to a dollar sign.This commit improves the resolution by introducing a loop that iteratively converts dots to dollar signs from right to left, until the class is found or no further conversion is possible. Additionally, if the given name already contains a dollar sign, the method now fails fast without attempting unnecessary conversions.