Java Reflection Annotation getAnnotations(AnnotatedElement ae, Class annotationType)

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

Description

get Annotations

License

Apache License

Declaration

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

Method Source Code


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

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collection;

public class Main {
    public static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement ae,
            Class<T> annotationType) {
        Collection<T> anns = new ArrayList<>(2);

        // look at raw annotation
        T ann = ae.getAnnotation(annotationType);
        if (ann != null) {
            anns.add(ann);/*from  w  w  w  . ja va  2 s.  c  o  m*/
        }

        // scan meta-annotations
        for (Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(annotationType);
            if (ann != null) {
                anns.add(ann);
            }
        }

        return (anns.isEmpty() ? null : anns);
    }
}

Related

  1. getAnnotationMethods(Class annotationType)
  2. getAnnotationOfField(Field field, Class clazz)
  3. getAnnotationOuterIndecies(Class annotationClass, Annotation[][] annotations)
  4. getAnnotationPropertyValue(Annotation a, String annotationPropertyName)
  5. getAnnotationRecursive(Class cls, Class annotationClass)
  6. getAnnotations(AnnotatedElement annotated)
  7. getAnnotations(Annotation[][] annotations)
  8. getAnnotations(Class cls)
  9. getAnnotations(Class annotationClass, Annotation[] annotations)