[ 
https://issues.apache.org/jira/browse/LANG-1544?focusedWorklogId=527282&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-527282
 ]

ASF GitHub Bot logged work on LANG-1544:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 22/Dec/20 17:10
            Start Date: 22/Dec/20 17:10
    Worklog Time Spent: 10m 
      Work Description: 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]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 527282)
    Time Spent: 2h 50m  (was: 2h 40m)

> MethodUtils.invokeMethod NullPointerException in case of null in args list
> --------------------------------------------------------------------------
>
>                 Key: LANG-1544
>                 URL: https://issues.apache.org/jira/browse/LANG-1544
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.reflect.*
>    Affects Versions: 3.10
>            Reporter: Peter Nagy
>            Priority: Critical
>          Time Spent: 2h 50m
>  Remaining Estimate: 0h
>
> MethodUtils:774
>  
> if (classArray[offset].equals(toClassArray[offset])) {
>  continue;
> } else if (ClassUtils.isAssignable(classArray[offset], toClassArray[offset], 
> true)
>  
> cause NPE if classArray[offset] is null. Can you please extend the if 
> condition with a null check, like this?
>  
> if (classArray[offset] != null && 
> classArray[offset].equals(toClassArray[offset]))



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to