Java Reflection Annotation getAnnotations(Class c, Class annotationClass)

Here you can find the source of getAnnotations(Class c, Class annotationClass)

Description

Finds the annotation objects of annotated fields.

License

Open Source License

Parameter

Parameter Description
c The class containing the fields.
annotationClass The annotation.

Return

The annotations of the annotated fields.

Declaration

public static <T extends Annotation> List<T> getAnnotations(Class<?> c, Class<T> annotationClass) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**//from  w ww. ja  va2s  . c  o m
     * Finds the annotation objects of annotated fields.
     *
     * @param c
     *          The class containing the fields.
     * @param annotationClass
     *          The annotation.
     * @return The annotations of the annotated fields.
     */
    public static <T extends Annotation> List<T> getAnnotations(Class<?> c, Class<T> annotationClass) {

        Field[] fields = c.getDeclaredFields();
        List<T> annotations = new LinkedList<>();
        for (Field field : fields) {
            T annotation = field.getAnnotation(annotationClass);
            if (annotation != null) {
                annotations.add(annotation);
            }
        }
        return annotations;
    }
}

Related

  1. getAnnotations(AnnotatedElement ae, Class annotationType)
  2. getAnnotations(AnnotatedElement annotated)
  3. getAnnotations(Annotation[][] annotations)
  4. getAnnotations(Class cls)
  5. getAnnotations(Class annotationClass, Annotation[] annotations)
  6. getAnnotations(Class annotation, Collection annotations)
  7. getAnnotations(Class ann, Object o, Method m, int param)
  8. getAnnotations(Enum targetEnum, Class targetAnnotationClass)
  9. getAnnotations(Field field)