You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/02/22 13:28:42 UTC

[commons-lang] branch master updated: LANG-1518 - fix searchSupers for generic classes (#494)

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 f4c2ed4  LANG-1518 - fix searchSupers for generic classes (#494)
f4c2ed4 is described below

commit f4c2ed4995d19c595ec0bf5c49d210df9046fda9
Author: Michele Preti <le...@gmail.com>
AuthorDate: Sat Feb 22 14:28:33 2020 +0100

    LANG-1518 - fix searchSupers for generic classes (#494)
    
    * fix searchSupers for generic classes
    
    * fix checkstyle
---
 .../org/apache/commons/lang3/reflect/MethodUtils.java  | 18 +++++++-----------
 .../apache/commons/lang3/reflect/MethodUtilsTest.java  | 18 ++++++++++++++++++
 .../commons/lang3/reflect/testbed/GenericParent.java   |  6 ++++++
 .../reflect/testbed/StringParameterizedChild.java      | 15 +++++++++++++++
 4 files changed, 46 insertions(+), 11 deletions(-)

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 c014450..f724c5c 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -974,17 +974,13 @@ public class MethodUtils {
             final Class<?> mcls = method.getDeclaringClass();
             final List<Class<?>> classes = getAllSuperclassesAndInterfaces(mcls);
             for (final Class<?> acls : classes) {
-                Method equivalentMethod;
-                try {
-                    equivalentMethod = (ignoreAccess ? acls.getDeclaredMethod(method.getName(), method.getParameterTypes())
-                            : acls.getMethod(method.getName(), method.getParameterTypes()));
-                } catch (final NoSuchMethodException e) {
-                    // if not found, just keep searching
-                    continue;
-                }
-                annotation = equivalentMethod.getAnnotation(annotationCls);
-                if (annotation != null) {
-                    break;
+                Method equivalentMethod = (ignoreAccess ? MethodUtils.getMatchingMethod(acls, method.getName(), method.getParameterTypes())
+                    : MethodUtils.getMatchingAccessibleMethod(acls, method.getName(), method.getParameterTypes()));
+                if (equivalentMethod != null) {
+                    annotation = equivalentMethod.getAnnotation(annotationCls);
+                    if (annotation != null) {
+                        break;
+                    }
                 }
             }
         }
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 168bc2a..ab40b22 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -799,6 +799,15 @@ public class MethodUtilsTest {
                 Annotated.class, true, true));
         assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"),
                 Annotated.class, true, true));
+
+        assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentNotAnnotatedMethod", String.class),
+                Annotated.class, true, true));
+        assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentProtectedAnnotatedMethod", String.class),
+                Annotated.class, true, true));
+        assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getDeclaredMethod("privateAnnotatedMethod", String.class),
+                Annotated.class, true, true));
+        assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("publicAnnotatedMethod", String.class),
+                Annotated.class, true, true));
     }
 
     @Test
@@ -827,6 +836,15 @@ public class MethodUtilsTest {
                 Annotated.class, true, false));
         assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"),
                 Annotated.class, true, false));
+
+        assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentNotAnnotatedMethod", String.class),
+                Annotated.class, true, false));
+        assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentProtectedAnnotatedMethod", String.class),
+                Annotated.class, true, false));
+        assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getDeclaredMethod("privateAnnotatedMethod", String.class),
+                Annotated.class, true, false));
+        assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("publicAnnotatedMethod", String.class),
+                Annotated.class, true, false));
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/lang3/reflect/testbed/GenericParent.java b/src/test/java/org/apache/commons/lang3/reflect/testbed/GenericParent.java
index f848567..73b1446 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/testbed/GenericParent.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/testbed/GenericParent.java
@@ -25,4 +25,10 @@ public class GenericParent<T> implements GenericConsumer<T> {
     public void consume(final T t) {
     }
 
+    @Annotated
+    protected void parentProtectedAnnotatedMethod(final T t) {
+    }
+
+    public void parentNotAnnotatedMethod(final T t) {
+    }
 }
diff --git a/src/test/java/org/apache/commons/lang3/reflect/testbed/StringParameterizedChild.java b/src/test/java/org/apache/commons/lang3/reflect/testbed/StringParameterizedChild.java
index dd285bc..36b7156 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/testbed/StringParameterizedChild.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/testbed/StringParameterizedChild.java
@@ -24,4 +24,19 @@ public class StringParameterizedChild extends GenericParent<String> {
     public void consume(final String t) {
         super.consume(t);
     }
+
+    @Override
+    public void parentProtectedAnnotatedMethod(final String t) {
+    }
+
+    public void parentNotAnnotatedMethod(final String t) {
+    }
+
+    @Annotated
+    private void privateAnnotatedMethod(final String t) {
+    }
+
+    @Annotated
+    public void publicAnnotatedMethod(final String t) {
+    }
 }