From e53c2c6331be891b19bf90fc8cc0d2c194d88615 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 19 Sep 2023 15:12:53 +0200 Subject: [PATCH] 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 --- .../src/main/java/org/springframework/util/ClassUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 7fd0c70ddce..a15835748a7 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -305,7 +305,8 @@ public abstract class ClassUtils { } 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 {