From c29c67839be0a1bc51f335de56f91e67cea86814 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Tue, 5 Mar 2024 11:23:55 +0100 Subject: [PATCH] Cache parameterTypes in ClassUtils.getInterfaceMethodIfPossible --- .../org/springframework/util/ClassUtils.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index e89132126e..0ff9f5ed75 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -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);