Skip to content

Commit e53c2c6

Browse files
committed
Reduce nested class lookups in ClassUtils
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
1 parent 0550037 commit e53c2c6

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ public static Class<?> forName(String name, @Nullable ClassLoader classLoader)
305305
}
306306
catch (ClassNotFoundException ex) {
307307
int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR);
308-
if (lastDotIndex != -1) {
308+
int previousDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR, lastDotIndex -1);
309+
if (lastDotIndex != -1 && previousDotIndex != 1 && Character.isUpperCase(name.charAt(previousDotIndex + 1))) {
309310
String nestedClassName =
310311
name.substring(0, lastDotIndex) + NESTED_CLASS_SEPARATOR + name.substring(lastDotIndex + 1);
311312
try {

0 commit comments

Comments
 (0)