This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new d47dc7af7 [LANG-1694] MethodUtils.getMatchingMethod() fails with 
"Found multiple candidates" (#1033)
d47dc7af7 is described below

commit d47dc7af76471c3d0bc600361a62c65e0cb865b9
Author: SeasonPan <244014...@qq.com>
AuthorDate: Thu Mar 2 06:40:35 2023 +0800

    [LANG-1694] MethodUtils.getMatchingMethod() fails with "Found multiple 
candidates" (#1033)
    
    * [LANG-1694]'MethodUtils.getMatchingMethod' fails with "Found multiple 
candidates" when the method is abstract
    
    * [LANG-1694]MethodUtilsTest modify the modifier‘s order
    
    * [LANG-1694]MethodUtils.getMatchingMethod modify PMD check
---
 .../java/org/apache/commons/lang3/reflect/MethodUtils.java  |  3 ++-
 .../org/apache/commons/lang3/reflect/MethodUtilsTest.java   | 13 +++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java 
b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
index b70949e57..35d9cfc2e 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -751,7 +751,8 @@ public class MethodUtils {
         }
 
         final List<Method> bestCandidates = 
candidates.values().iterator().next();
-        if (bestCandidates.size() == 1) {
+        if (bestCandidates.size() == 1 || 
!Objects.equals(bestCandidates.get(0).getDeclaringClass(),
+                bestCandidates.get(1).getDeclaringClass())) {
             return bestCandidates.get(0);
         }
 
diff --git 
a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
index 21535a5c5..ad9265712 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -1053,6 +1053,9 @@ public class MethodUtilsTest extends AbstractLangTest {
 
         assertThrows(IllegalStateException.class,
                 () -> 
MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod4", 
null, null));
+
+        
assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, 
"testMethod5", RuntimeException.class),
+                GetMatchingMethodImpl.class.getMethod("testMethod5", 
Exception.class));
     }
 
     private static final class GetMatchingMethodClass {
@@ -1089,4 +1092,14 @@ public class MethodUtilsTest extends AbstractLangTest {
         public void testMethod4(final Color aColor1, final Color aColor2) {
         }
     }
+
+    protected abstract static class AbstractGetMatchingMethod {
+        public abstract void testMethod5(Exception exception);
+    }
+
+    private static class GetMatchingMethodImpl extends 
AbstractGetMatchingMethod {
+        @Override
+        public void testMethod5(final Exception exception) {
+        }
+    }
 }

Reply via email to