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 6fe5907907cc..97b24c68d025 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -1120,13 +1120,7 @@ public static Method getMethod(Class clazz, String methodName, @Nullable Clas } } else { - Set candidates = new HashSet<>(1); - Method[] methods = clazz.getMethods(); - for (Method method : methods) { - if (methodName.equals(method.getName())) { - candidates.add(method); - } - } + Set candidates = findMethodCandidatesByName(clazz, methodName); if (candidates.size() == 1) { return candidates.iterator().next(); } @@ -1165,13 +1159,7 @@ public static Method getMethodIfAvailable(Class clazz, String methodName, @Nu } } else { - Set candidates = new HashSet<>(1); - Method[] methods = clazz.getMethods(); - for (Method method : methods) { - if (methodName.equals(method.getName())) { - candidates.add(method); - } - } + Set candidates = findMethodCandidatesByName(clazz, methodName); if (candidates.size() == 1) { return candidates.iterator().next(); } @@ -1362,4 +1350,14 @@ public static Method getStaticMethod(Class clazz, String methodName, Class } } + private static Set findMethodCandidatesByName(Class clazz, String methodName) { + Set candidates = new HashSet<>(1); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if (methodName.equals(method.getName())) { + candidates.add(method); + } + } + return candidates; + } }