Skip to content

Commit 49294c9

Browse files
committed
ClassUtils.getMostSpecificMethod uses Class.getMethod code path in case of a public method
This should be significantly faster than our standard algorithm, for a very common case. Motivated by SPR-9802, even if the fix there uses a different approach, with transaction name determination not calling getMostSpecificMethod at all anymore. Issue: SPR-9802
1 parent 89b3651 commit 49294c9

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ public static boolean hasAtLeastOneMethodWithName(Class<?> clazz, String methodN
724724
* Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod}
725725
* if bridge method resolution is desirable (e.g. for obtaining metadata from
726726
* the original method definition).
727-
* <p><b>NOTE:</b>Since Spring 3.1.1, if java security settings disallow reflective
727+
* <p><b>NOTE:</b> Since Spring 3.1.1, if Java security settings disallow reflective
728728
* access (e.g. calls to {@code Class#getDeclaredMethods} etc, this implementation
729729
* will fall back to returning the originally provided method.
730730
* @param method the method to be invoked, which may come from an interface
@@ -734,17 +734,28 @@ public static boolean hasAtLeastOneMethodWithName(Class<?> clazz, String methodN
734734
* <code>targetClass</code> doesn't implement it or is <code>null</code>
735735
*/
736736
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
737-
Method specificMethod = null;
738737
if (method != null && isOverridable(method, targetClass) &&
739738
targetClass != null && !targetClass.equals(method.getDeclaringClass())) {
740739
try {
741-
specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(), method.getParameterTypes());
742-
} catch (AccessControlException ex) {
743-
// security settings are disallowing reflective access; leave
744-
// 'specificMethod' null and fall back to 'method' below
740+
if (Modifier.isPublic(method.getModifiers())) {
741+
try {
742+
return targetClass.getMethod(method.getName(), method.getParameterTypes());
743+
}
744+
catch (NoSuchMethodException ex) {
745+
return method;
746+
}
747+
}
748+
else {
749+
Method specificMethod =
750+
ReflectionUtils.findMethod(targetClass, method.getName(), method.getParameterTypes());
751+
return (specificMethod != null ? specificMethod : method);
752+
}
753+
}
754+
catch (AccessControlException ex) {
755+
// Security settings are disallowing reflective access; fall back to 'method' below.
745756
}
746757
}
747-
return (specificMethod != null ? specificMethod : method);
758+
return method;
748759
}
749760

750761
/**
@@ -1150,5 +1161,4 @@ public static boolean isCglibProxyClassName(String className) {
11501161
return (className != null && className.contains(CGLIB_CLASS_SEPARATOR));
11511162
}
11521163

1153-
11541164
}

0 commit comments

Comments
 (0)