Skip to content

Commit a4fc7d3

Browse files
committed
Optimize ClassUtils#getMostSpecificMethod
This commit optimizes ClassUtils#getMostSpecificMethod which is a method frequently invoked in typical Spring applications. It refines ClassUtils#isOverridable by considering static and final modifiers as non overridable and optimizes its implementation. Closes gh-31100
1 parent 88c3a78 commit a4fc7d3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Keith Donald
5151
* @author Rob Harrop
5252
* @author Sam Brannen
53+
* @author Sebastien Deleuze
5354
* @since 1.1
5455
* @see TypeUtils
5556
* @see ReflectionUtils
@@ -83,6 +84,12 @@ public abstract class ClassUtils {
8384
/** The ".class" file suffix. */
8485
public static final String CLASS_FILE_SUFFIX = ".class";
8586

87+
/** Precomputed value for the combination of private, static and final modifiers. */
88+
private static final int NON_OVERRIDABLE_MODIFIER = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
89+
90+
/** Precomputed value for the combination of public and protected modifiers. */
91+
private static final int OVERRIDABLE_MODIFIER = Modifier.PUBLIC | Modifier.PROTECTED;
92+
8693

8794
/**
8895
* Map with primitive wrapper type as key and corresponding primitive
@@ -1379,10 +1386,10 @@ private static boolean isGroovyObjectMethod(Method method) {
13791386
* @param targetClass the target class to check against
13801387
*/
13811388
private static boolean isOverridable(Method method, @Nullable Class<?> targetClass) {
1382-
if (Modifier.isPrivate(method.getModifiers())) {
1389+
if ((method.getModifiers() & NON_OVERRIDABLE_MODIFIER) != 0) {
13831390
return false;
13841391
}
1385-
if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
1392+
if ((method.getModifiers() & OVERRIDABLE_MODIFIER) != 0) {
13861393
return true;
13871394
}
13881395
return (targetClass == null ||

0 commit comments

Comments
 (0)