From d1f0720fb98fd22fa1a432d78ea228ef67415466 Mon Sep 17 00:00:00 2001 From: Wolfgang Wachsmuth Date: Thu, 17 Jul 2025 16:38:00 +0200 Subject: [PATCH 1/3] LANG-1778 fix by reversing order --- .../org/apache/commons/lang3/reflect/MethodUtils.java | 5 ++++- .../apache/commons/lang3/reflect/MethodUtilsTest.java | 10 ++++++++++ 2 files changed, 14 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 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..419f5cf259b 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,8 @@ public void testMethod5(final Exception exception) { } } + private static final class ConcreteGetMatchingMethod2 extends AbstractGetMatchingMethod2 {} + public static class GrandParentObject { } public static class InheritanceBean { @@ -670,6 +676,10 @@ 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()); } @Test From 7bfc591d963d66d37422dcc7dc62fb8fc61b747e Mon Sep 17 00:00:00 2001 From: Wolfgang Wachsmuth Date: Thu, 17 Jul 2025 16:53:42 +0200 Subject: [PATCH 2/3] fix checkstyle --- .../org/apache/commons/lang3/reflect/MethodUtilsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 419f5cf259b..158154fd59f 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -65,7 +65,7 @@ protected abstract static class AbstractGetMatchingMethod implements InterfaceGe } protected abstract static class AbstractGetMatchingMethod2 implements InterfaceGetMatchingMethod { @Override - public void testMethod6() {} + public void testMethod6() { } } interface ChildInterface { @@ -116,7 +116,7 @@ public void testMethod5(final Exception exception) { } } - private static final class ConcreteGetMatchingMethod2 extends AbstractGetMatchingMethod2 {} + private static final class ConcreteGetMatchingMethod2 extends AbstractGetMatchingMethod2 { } public static class GrandParentObject { } From 0030a6b15778d06e6e941fb5f1a428c2e228254a Mon Sep 17 00:00:00 2001 From: Wolfgang Wachsmuth Date: Thu, 17 Jul 2025 16:58:34 +0200 Subject: [PATCH 3/3] LANG-1778 assert direct implementation for regression --- .../commons/lang3/reflect/MethodUtilsTest.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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 158154fd59f..c189f4463e3 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -117,6 +117,10 @@ 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 { } @@ -677,9 +681,16 @@ 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(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