Java Reflection Annotation getAnnotation(Class target, Class annoCls)

Here you can find the source of getAnnotation(Class target, Class annoCls)

Description

get Annotation

License

Open Source License

Declaration

public static <T extends Annotation> T getAnnotation(Class<?> target, Class<T> annoCls) 

Method Source Code

//package com.java2s;
/**//w  ww .j a  va  2  s. c  om
 * Copyright (c) 2008-2011, Open & Green Inc.
 * All rights reserved.
 * info@gaoshin.com
 * 
 * This version of SORMA is licensed under the terms of the Open Source GPL 3.0 license. http://www.gnu.org/licenses/gpl.html. Alternate Licenses are available.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 
 * AND NON-INFRINGEMENT OF THIRD-PARTY INTELLECTUAL PROPERTY RIGHTS.  
 * See the GNU General Public License for more details.
 * 
 */

import java.lang.annotation.Annotation;

public class Main {
    public static <T extends Annotation> T getAnnotation(Class<?> target, Class<T> annoCls) {
        if (Object.class.equals(target)) {
            return null;
        }
        try {
            T annotation = target.getAnnotation(annoCls);
            if (annotation != null)
                return annotation;
            else
                return getAnnotation(target.getSuperclass(), annoCls);
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getAnnotation(Class componentClass, Class annotationClass)
  2. getAnnotation(Class configInterface, Method method, Class annotationType, boolean searchMethodType)
  3. getAnnotation(Class klazz, Class annotationClass)
  4. getAnnotation(Class objectClass, Class annotationClass)
  5. getAnnotation(Class onClass, Class desiredAnnotationClass)
  6. getAnnotation(Class target, Class annotationClass)
  7. getAnnotation(Class theClass, Class theAnnotation)
  8. getAnnotation(Class type, Class annotationType)
  9. getAnnotation(Class type, Class ann)