Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1518

MethodUtils.getAnnotation() with searchSupers = true does not work if super is generic

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.9
    • Fix Version/s: 3.10
    • Component/s: lang.reflect.*
    • Labels:
      None

      Description

       

      public static class Foo<T> {
      	@Nonnull
      	protected void test(T i) {
      		System.out.println("foo" + i);
      	}
      }
      
      public static class Bar extends Foo<Integer> {
      	@Override
      	protected void test(Integer i) {
      		System.out.println("bar" + i);
      	}
      }
      
      public static void main(String[] args) throws NoSuchMethodException, SecurityException {
      	Method testMethod = Bar.class.getDeclaredMethod("test", Integer.class);
      	
      	System.out.println(MethodUtils.getAnnotation(testMethod, Nonnull.class, true, true)); //==null
      }
      

       

      the method MethodUtils.getAnnotation() should be modified as souch:

      (using MethodUtils.getMatchingMethod/MethodUtils.getMatchingAccessibleMethod instead of getDeclaredMethod/getMethod)

      public static <A extends Annotation> A getAnnotation(final Method method, final Class<A> annotationCls,
      		final boolean searchSupers, final boolean ignoreAccess) {
      
      	Validate.isTrue(method != null, "The method must not be null");
      	Validate.isTrue(annotationCls != null, "The annotation class must not be null");
      	if (!ignoreAccess && !MemberUtils.isAccessible(method)) {
      		return null;
      	}
      
      	A annotation = method.getAnnotation(annotationCls);
      
      	if (annotation == null && searchSupers) {
      		final Class<?> mcls = method.getDeclaringClass();
      		final List<Class<?>> classes = getAllSuperclassesAndInterfaces(mcls);
      		for (final Class<?> acls : classes) {
      			Method equivalentMethod = (ignoreAccess
      					? MethodUtils.getMatchingMethod(acls, method.getName(), method.getParameterTypes())
      					: MethodUtils.getMatchingAccessibleMethod(acls, method.getName(), method.getParameterTypes()));
      			if (equivalentMethod == null) {
      				continue;
      			}
      			annotation = equivalentMethod.getAnnotation(annotationCls);
      			if (annotation != null) {
      				break;
      			}
      		}
      	}
      
      	return annotation;
      }
      

       

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              lelmarir Michele Preti

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 2h 20m
                2h 20m

                  Issue deployment