Example usage for java.lang.reflect AnnotatedElement getDeclaredAnnotation

List of usage examples for java.lang.reflect AnnotatedElement getDeclaredAnnotation

Introduction

In this page you can find the example usage for java.lang.reflect AnnotatedElement getDeclaredAnnotation.

Prototype

default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) 

Source Link

Document

Returns this element's annotation for the specified type if such an annotation is directly present, else null.

Usage

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Perform the search algorithm for {@link #findAnnotation(AnnotatedElement, Class)}
 * avoiding endless recursion by tracking which annotations have already
 * been <em>visited</em>.//from   w  w  w. j av a  2  s .co  m
 * @param annotatedElement the {@code AnnotatedElement} on which to find the annotation
 * @param annotationType the annotation type to look for, both locally and as a meta-annotation
 * @param visited the set of annotations that have already been visited
 * @return the first matching annotation, or {@code null} if not found
 * @since 4.2
 */
@Nullable
private static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement,
        Class<A> annotationType, Set<Annotation> visited) {
    try {
        A annotation = annotatedElement.getDeclaredAnnotation(annotationType);
        if (annotation != null) {
            return annotation;
        }
        for (Annotation declaredAnn : annotatedElement.getDeclaredAnnotations()) {
            Class<? extends Annotation> declaredType = declaredAnn.annotationType();
            if (!isInJavaLangAnnotationPackage(declaredType) && visited.add(declaredAnn)) {
                annotation = findAnnotation((AnnotatedElement) declaredType, annotationType, visited);
                if (annotation != null) {
                    return annotation;
                }
            }
        }
    } catch (Throwable ex) {
        handleIntrospectionFailure(annotatedElement, ex);
    }
    return null;
}