Skip to content

Commit

Permalink
Reduce nested class lookups in ClassUtils
Browse files Browse the repository at this point in the history
Prior to this commit, `ClassUtils#forName` would always attempt to
resolve the given class name as a nested type. For example, searching
for `org.example.Spring` would try to resolve:

* `org.example.Spring`
* if not available, try `org.example$Spring` as well

Java classes usually start with uppercase letters, so this additional
lookup can be costly and not very useful.

This commit only attempts nested class lookups when the previous segment
starts with an uppercase. So `org.example.Spring.Issue` will look for
`org.example.Spring$Issue`, but `org.example.Spring` will not.

Closes gh-31258
  • Loading branch information
bclozel committed Sep 19, 2023
1 parent 0550037 commit e53c2c6
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ public static Class<?> forName(String name, @Nullable ClassLoader classLoader)
}
catch (ClassNotFoundException ex) {
int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR);
if (lastDotIndex != -1) {
int previousDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR, lastDotIndex -1);
if (lastDotIndex != -1 && previousDotIndex != 1 && Character.isUpperCase(name.charAt(previousDotIndex + 1))) {
String nestedClassName =
name.substring(0, lastDotIndex) + NESTED_CLASS_SEPARATOR + name.substring(lastDotIndex + 1);
try {
Expand Down

0 comments on commit e53c2c6

Please sign in to comment.