Java Reflection Annotation getAnnotation(AnnotatedElement ae, Class annotationType)

Here you can find the source of getAnnotation(AnnotatedElement ae, Class annotationType)

Description

Get a single Annotation of annotationType from the supplied Method, Constructor or Field.

License

Open Source License

Parameter

Parameter Description
ae the Method, Constructor or Field from which to get the annotation
annotationType the annotation class to look for, both locally and as a meta-annotation

Return

the matching annotation or null if not found

Declaration

public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) 

Method Source Code


//package com.java2s;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

public class Main {
    /**/*from   ww  w  . j  a va  2 s . c  o m*/
     * Get a single {@link Annotation} of {@code annotationType} from the supplied
     * Method, Constructor or Field. Meta-annotations will be searched if the annotation
     * is not declared locally on the supplied element.
     * @param ae the Method, Constructor or Field from which to get the annotation
     * @param annotationType the annotation class to look for, both locally and as a meta-annotation
     * @return the matching annotation or {@code null} if not found
     * @since 3.1
     */
    public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) {
        T ann = ae.getAnnotation(annotationType);
        if (ann == null) {
            for (Annotation metaAnn : ae.getAnnotations()) {
                ann = metaAnn.annotationType().getAnnotation(annotationType);
                if (ann != null) {
                    break;
                }
            }
        }
        return ann;
    }
}

Related

  1. getAnnotation( final AnnotatedElement anElement, final Class annotationClass)
  2. getAnnotation( final Method method, final Class annotationClass)
  3. getAnnotation(@Nonnull Annotation[] annotations, @Nonnull Class annotation)
  4. getAnnotation(@Nonnull Class cls, @Nonnull Class annotation)
  5. getAnnotation(AnnotatedElement aobj, Class aClass)
  6. getAnnotation(AnnotatedElement element, Class annotation)
  7. getAnnotation(AnnotatedElement element, String annotationTypeName)
  8. getAnnotation(AnnotatedElement target, String annotationType)