Skip to content

Commit

Permalink
Extract duplicated code into a separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
stsypanov authored and jhoeller committed Feb 15, 2019
1 parent 92053bb commit d2bfca7
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions spring-core/src/main/java/org/springframework/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1120,13 +1120,7 @@ public static Method getMethod(Class<?> clazz, String methodName, @Nullable Clas
}
}
else {
Set<Method> candidates = new HashSet<>(1);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
candidates.add(method);
}
}
Set<Method> candidates = findMethodCandidatesByName(clazz, methodName);
if (candidates.size() == 1) {
return candidates.iterator().next();
}
Expand Down Expand Up @@ -1165,13 +1159,7 @@ public static Method getMethodIfAvailable(Class<?> clazz, String methodName, @Nu
}
}
else {
Set<Method> candidates = new HashSet<>(1);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
candidates.add(method);
}
}
Set<Method> candidates = findMethodCandidatesByName(clazz, methodName);
if (candidates.size() == 1) {
return candidates.iterator().next();
}
Expand Down Expand Up @@ -1362,4 +1350,14 @@ public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>
}
}

private static Set<Method> findMethodCandidatesByName(Class<?> clazz, String methodName) {
Set<Method> candidates = new HashSet<>(1);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
candidates.add(method);
}
}
return candidates;
}
}

0 comments on commit d2bfca7

Please sign in to comment.