From d2bfca7900301330e688a85cf17b2cbfc7bfac01 Mon Sep 17 00:00:00 2001 From: stsypanov Date: Mon, 11 Feb 2019 16:44:31 +0200 Subject: [PATCH] Extract duplicated code into a separate method --- .../org/springframework/util/ClassUtils.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 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 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; + } }