Java examples for java.lang.annotation:Annotation Type
Returns array of annotations which annotates the given annotated element and which are annotated by given annotation type.
import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; public class Main{ /**//from w w w .jav a2 s . com * Returns array of annotations which annotates the given annotated element and which are * annotated by given annotation type. * * @param element the annotated element. * @param annotatedBy the annotation type which annotates searched annotations. * @return The array of annotations of given element which are annotated by given annotation. */ public static Annotation[] getAnnotatedAnnotations( AnnotatedElement element, Class<? extends Annotation> annotatedBy) { Preconditions.checkArgumentNotNull(element, "Annotated element cannot be null"); Preconditions.checkArgumentNotNull(annotatedBy, "Annotation cannot be null"); Annotation[] annotations = element.getAnnotations(); List<Annotation> annotatedAnnotations = new ArrayList<Annotation>( annotations.length); for (int i = 0; i < annotations.length; i++) { Annotation[] annotationAnnotations = annotations[i] .annotationType().getAnnotations(); for (int j = 0; j < annotationAnnotations.length; j++) { Class<? extends Annotation> aType = annotationAnnotations[j] .annotationType(); if (annotatedBy.equals(aType)) { annotatedAnnotations.add(annotations[i]); } } } return annotatedAnnotations.toArray(new Annotation[0]); } }