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 c9a18908bb2..aa00ac6050b 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java @@ -24,6 +24,7 @@ import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashSet; @@ -382,7 +383,9 @@ public static Method getMatchingMethod(final Class cls, final String methodNa .filter(method -> method.getName().equals(methodName)) .collect(Collectors.toList()); - getAllSuperclassesAndInterfaces(cls).stream() + final List> allSuperclassesAndInterfaces = getAllSuperclassesAndInterfaces(cls); + Collections.reverse(allSuperclassesAndInterfaces); + allSuperclassesAndInterfaces.stream() .map(Class::getDeclaredMethods) .flatMap(Stream::of) .filter(method -> method.getName().equals(methodName)) 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 8fa5a62e198..c189f4463e3 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -63,6 +63,10 @@ class MethodUtilsTest extends AbstractLangTest { protected abstract static class AbstractGetMatchingMethod implements InterfaceGetMatchingMethod { public abstract void testMethod5(Exception exception); } + protected abstract static class AbstractGetMatchingMethod2 implements InterfaceGetMatchingMethod { + @Override + public void testMethod6() { } + } interface ChildInterface { } @@ -112,6 +116,12 @@ public void testMethod5(final Exception exception) { } } + private static final class ConcreteGetMatchingMethod2 extends AbstractGetMatchingMethod2 { } + private static final class ConcreteGetMatchingMethod22 extends AbstractGetMatchingMethod2 { + @Override + public void testMethod6() { } + } + public static class GrandParentObject { } public static class InheritanceBean { @@ -670,6 +680,17 @@ void testGetMatchingMethod() throws NoSuchMethodException { assertNullPointerException( () -> MethodUtils.getMatchingMethod(null, "testMethod5", RuntimeException.class)); + + { + final Method testMethod6 = MethodUtils.getMatchingMethod(ConcreteGetMatchingMethod2.class, "testMethod6"); + assertNotNull(testMethod6); + assertEquals(AbstractGetMatchingMethod2.class, testMethod6.getDeclaringClass()); + } + { + final Method testMethod6 = MethodUtils.getMatchingMethod(ConcreteGetMatchingMethod22.class, "testMethod6"); + assertNotNull(testMethod6); + assertEquals(ConcreteGetMatchingMethod22.class, testMethod6.getDeclaringClass()); + } } @Test