mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-05-02 20:09:31 +01:00
Cache parameterTypes in ClassUtils.getInterfaceMethodIfPossible
This commit is contained in:
@@ -1415,25 +1415,23 @@ public abstract class ClassUtils {
|
||||
}
|
||||
// Try cached version of method in its declaring class
|
||||
Method result = interfaceMethodCache.computeIfAbsent(method,
|
||||
key -> findInterfaceMethodIfPossible(key, key.getDeclaringClass(), Object.class));
|
||||
key -> findInterfaceMethodIfPossible(key, key.getParameterTypes(), key.getDeclaringClass(),
|
||||
Object.class));
|
||||
if (result == method && targetClass != null) {
|
||||
// No interface method found yet -> try given target class (possibly a subclass of the
|
||||
// declaring class, late-binding a base class method to a subclass-declared interface:
|
||||
// see e.g. HashMap.HashIterator.hasNext)
|
||||
result = findInterfaceMethodIfPossible(method, targetClass, method.getDeclaringClass());
|
||||
result = findInterfaceMethodIfPossible(method, method.getParameterTypes(), targetClass,
|
||||
method.getDeclaringClass());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Method findInterfaceMethodIfPossible(Method method, Class<?> startClass, Class<?> endClass) {
|
||||
Class<?>[] parameterTypes = null;
|
||||
private static Method findInterfaceMethodIfPossible(Method method, Class<?>[] parameterTypes,
|
||||
Class<?> startClass, Class<?> endClass) {
|
||||
|
||||
Class<?> current = startClass;
|
||||
while (current != null && current != endClass) {
|
||||
if (parameterTypes == null) {
|
||||
// Since Method#getParameterTypes() clones the array, we lazily retrieve
|
||||
// and cache parameter types to avoid cloning the array multiple times.
|
||||
parameterTypes = method.getParameterTypes();
|
||||
}
|
||||
for (Class<?> ifc : current.getInterfaces()) {
|
||||
try {
|
||||
return ifc.getMethod(method.getName(), parameterTypes);
|
||||
|
||||
Reference in New Issue
Block a user