mdbuck77 commented on a change in pull request #680:
URL: https://github.com/apache/commons-lang/pull/680#discussion_r547395975



##########
File path: src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
##########
@@ -742,35 +745,51 @@ public static Method getMatchingMethod(final Class<?> 
cls, final String methodNa
         Validate.notNull(cls, "cls");
         Validate.notEmpty(methodName, "methodName");
 
-        // Address methods in superclasses
-        Method[] methodArray = cls.getDeclaredMethods();
-        final List<Class<?>> superclassList = 
ClassUtils.getAllSuperclasses(cls);
-        for (final Class<?> klass : superclassList) {
-            methodArray = ArrayUtils.addAll(methodArray, 
klass.getDeclaredMethods());
-        }
+        final List<Method> methods = Arrays.stream(cls.getDeclaredMethods())
+                .filter(method -> method.getName().equals(methodName))
+                .collect(toList());
+
+        ClassUtils.getAllSuperclasses(cls).stream()
+                .map(Class::getDeclaredMethods)
+                .flatMap(Arrays::stream)
+                .filter(method -> method.getName().equals(methodName))
+                .forEach(methods::add);
 
-        Method inexactMatch = null;
-        for (final Method method : methodArray) {
-            if (methodName.equals(method.getName()) &&
-                    Objects.deepEquals(parameterTypes, 
method.getParameterTypes())) {
+        for (Method method : methods) {
+            if (Arrays.deepEquals(method.getParameterTypes(), parameterTypes)) 
{
                 return method;
-            } else if (methodName.equals(method.getName()) &&
-                    ClassUtils.isAssignable(parameterTypes, 
method.getParameterTypes(), true)) {
-                if ((inexactMatch == null) || (distance(parameterTypes, 
method.getParameterTypes())
-                        < distance(parameterTypes, 
inexactMatch.getParameterTypes()))) {
-                    inexactMatch = method;
-                }
             }
+        }
+
+        final TreeMap<Double, List<Method>> candidates = new TreeMap<>();
 
+        methods.stream()
+                .filter(method -> ClassUtils.isAssignable(parameterTypes, 
method.getParameterTypes(), true))
+                .forEach(method -> {
+                    final double distance = distance(parameterTypes, 
method.getParameterTypes());
+                    List<Method> methods1 = 
candidates.computeIfAbsent(distance, k -> new ArrayList<>());
+                    methods1.add(method);
+                });
+
+        if (candidates.isEmpty()) {
+            return null;
         }
-        return inexactMatch;
+
+        final List<Method> bestCandidates = 
candidates.values().iterator().next();
+        if (bestCandidates.size() == 1) {
+            return bestCandidates.get(0);
+        }
+
+        final String target = methodName + 
Arrays.stream(parameterTypes).map(String::valueOf).collect(Collectors.joining(",",
 "(", ")"));
+        final String strCandidates = 
bestCandidates.stream().map(Method::toString).collect(Collectors.joining("\n  
"));
+        throw new IllegalStateException("Found multiple candidates for method 
" + target + " on class " + cls + ":\n  " + strCandidates);

Review comment:
       I replaced the newlines with opening and closing brackets.
   
   Michael




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to