Browse Source

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
pull/31279/head
Brian Clozel 3 years ago
parent
commit
e53c2c6331
  1. 3
      spring-core/src/main/java/org/springframework/util/ClassUtils.java

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

@ -305,7 +305,8 @@ public abstract class ClassUtils { @@ -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 {

Loading…
Cancel
Save