Java Reflection Annotation getAnnotation(Class target, Class annotationClass)

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

Description

Returns the annotation of the annotationClass of the clazz or any of it super classes.

License

Apache License

Parameter

Parameter Description
clazz The class to inspect.
annotationClass Class of the annotation to return

Return

The annotation of annnotationClass if found else null.

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.annotation.Annotation;

public class Main {
    /**//from  ww w. java  2s  .c  o  m
     * Returns the annotation of the annotationClass of the clazz or any of it super classes.
     * 
     * @param clazz
     *           The class to inspect.
     * @param annotationClass
     *           Class of the annotation to return
     *           
     * @return The annotation of annnotationClass if found else null.
     */
    public static <T extends Annotation> T getAnnotation(Class<?> target, Class<T> annotationClass) {
        Class<?> clazz = target;
        T annotation = clazz.getAnnotation(annotationClass);
        if (annotation != null) {
            return annotation;
        }
        while ((clazz = clazz.getSuperclass()) != null) {
            annotation = clazz.getAnnotation(annotationClass);
            if (annotation != null) {
                return annotation;
            }
        }
        return null;
    }
}

Related

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