[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
This commit is contained in:
SeasonPan 2023-03-02 06:40:35 +08:00 committed by GitHub
parent be210cfa28
commit d47dc7af76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -751,7 +751,8 @@ public class MethodUtils {
} }
final List<Method> bestCandidates = candidates.values().iterator().next(); 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); return bestCandidates.get(0);
} }

View File

@ -1053,6 +1053,9 @@ public class MethodUtilsTest extends AbstractLangTest {
assertThrows(IllegalStateException.class, assertThrows(IllegalStateException.class,
() -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod4", null, null)); () -> 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 { private static final class GetMatchingMethodClass {
@ -1089,4 +1092,14 @@ public class MethodUtilsTest extends AbstractLangTest {
public void testMethod4(final Color aColor1, final Color aColor2) { 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) {
}
}
} }