You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2020/02/19 10:08:00 UTC

[jira] [Work logged] (LANG-1518) MethodUtils.getAnnotation() with searchSupers =true does not work if super is generic

     [ https://issues.apache.org/jira/browse/LANG-1518?focusedWorklogId=389378&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-389378 ]

ASF GitHub Bot logged work on LANG-1518:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 19/Feb/20 10:07
            Start Date: 19/Feb/20 10:07
    Worklog Time Spent: 10m 
      Work Description: lelmarir commented on pull request #494: LANG-1518 - fix searchSupers for generic classes
URL: https://github.com/apache/commons-lang/pull/494
 
 
   https://issues.apache.org/jira/browse/LANG-1518
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Issue Time Tracking
-------------------

            Worklog Id:     (was: 389378)
    Remaining Estimate: 0h
            Time Spent: 10m

> MethodUtils.getAnnotation() with searchSupers =true does not work if super is generic
> -------------------------------------------------------------------------------------
>
>                 Key: LANG-1518
>                 URL: https://issues.apache.org/jira/browse/LANG-1518
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.reflect.*
>    Affects Versions: 3.9
>            Reporter: Michele Preti
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
>  
> {code:java}
> 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
> }
> {code}
>  
> the method MethodUtils.getAnnotation() should be modified as souch:
> (using *MethodUtils.getMatchingMethod*/*MethodUtils.getMatchingAccessibleMethod* instead of *getDeclaredMethod*/*getMethod*)
> {code:java}
> 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;
> }
> {code}
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)