From d47dc7af76471c3d0bc600361a62c65e0cb865b9 Mon Sep 17 00:00:00 2001 From: SeasonPan <244014926@qq.com> Date: Thu, 2 Mar 2023 06:40:35 +0800 Subject: [PATCH] [LANG-1694] MethodUtils.getMatchingMethod() fails with "Found multiple candidates" (#1033) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [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 --- .../apache/commons/lang3/reflect/MethodUtils.java | 3 ++- .../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 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) { + } + } }