From 052bbcc53031bd48dc76d070ba862f5293618600 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 8 Feb 2024 18:14:22 +0100 Subject: [PATCH] Cache parameter types array in ClassUtils.findInterfaceMethodIfPossible() --- .../java/org/springframework/util/ClassUtils.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 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 0a0fe6d9932..3218838cac6 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -1425,12 +1425,17 @@ public abstract class ClassUtils { } private static Method findInterfaceMethodIfPossible(Method method, Class startClass, Class endClass) { + Class[] parameterTypes = null; Class current = startClass; while (current != null && current != endClass) { - Class[] ifcs = current.getInterfaces(); - for (Class ifc : ifcs) { + 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(), method.getParameterTypes()); + return ifc.getMethod(method.getName(), parameterTypes); } catch (NoSuchMethodException ex) { // ignore