Here you can find the source of getAnnotations(Class> c, Class
Parameter | Description |
---|---|
c | The class containing the fields. |
annotationClass | The annotation. |
public static <T extends Annotation> List<T> getAnnotations(Class<?> c, Class<T> annotationClass)
//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; } }