Skip to content

Commit afe22b8

Browse files
committed
ConcurrentReferenceHashMap cache for getInterfaceMethodIfPossible results
Closes gh-24206
1 parent 634aba4 commit afe22b8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public abstract class ClassUtils {
110110
*/
111111
private static final Set<Class<?>> javaLanguageInterfaces;
112112

113+
/**
114+
* Cache for equivalent methods on an interface implemented by the declaring class.
115+
*/
116+
private static final Map<Method, Method> interfaceMethodCache = new ConcurrentReferenceHashMap<>(256);
117+
113118

114119
static {
115120
primitiveWrapperTypeMap.put(Boolean.class, boolean.class);
@@ -1291,22 +1296,25 @@ public static Method getMostSpecificMethod(Method method, @Nullable Class<?> tar
12911296
* @see #getMostSpecificMethod
12921297
*/
12931298
public static Method getInterfaceMethodIfPossible(Method method) {
1294-
if (Modifier.isPublic(method.getModifiers()) && !method.getDeclaringClass().isInterface()) {
1295-
Class<?> current = method.getDeclaringClass();
1299+
if (!Modifier.isPublic(method.getModifiers()) || method.getDeclaringClass().isInterface()) {
1300+
return method;
1301+
}
1302+
return interfaceMethodCache.computeIfAbsent(method, key -> {
1303+
Class<?> current = key.getDeclaringClass();
12961304
while (current != null && current != Object.class) {
12971305
Class<?>[] ifcs = current.getInterfaces();
12981306
for (Class<?> ifc : ifcs) {
12991307
try {
1300-
return ifc.getMethod(method.getName(), method.getParameterTypes());
1308+
return ifc.getMethod(key.getName(), key.getParameterTypes());
13011309
}
13021310
catch (NoSuchMethodException ex) {
13031311
// ignore
13041312
}
13051313
}
13061314
current = current.getSuperclass();
13071315
}
1308-
}
1309-
return method;
1316+
return key;
1317+
});
13101318
}
13111319

13121320
/**

0 commit comments

Comments
 (0)