Java Reflection Annotation Find findAnnotationInAnnotations(AnnotatedElement annotatedElement, Class annotationClass)

Here you can find the source of findAnnotationInAnnotations(AnnotatedElement annotatedElement, Class annotationClass)

Description

find Annotation In Annotations

License

Open Source License

Declaration

private static <A extends Annotation> A findAnnotationInAnnotations(AnnotatedElement annotatedElement,
            Class<A> annotationClass) 

Method Source Code


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

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Main {
    private static <A extends Annotation> A findAnnotationInAnnotations(AnnotatedElement annotatedElement,
            Class<A> annotationClass) {
        Annotation[] allAnnotations = annotatedElement.getAnnotations();
        for (Annotation annotation : allAnnotations) {
            A result = findAnnotation(annotation, annotationClass);
            if (result != null) {
                return result;
            }//from  www.ja v a 2 s . co  m
        }
        return null;
    }

    /**
     * Deep search of specified annotation considering "annotation inheritance" (annotation annotated with specified annotation).
     *
     * @param annotatedElement specified annotated element
     * @param annotationClass specified annotation class
     * @param <A> the type of the annotation to query for and return if present
     * @return specified element's annotation for the specified annotation type if deeply present on this element, else null
     */
    public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement,
            Class<A> annotationClass) {
        Objects.requireNonNull(annotatedElement);
        Objects.requireNonNull(annotationClass);

        A result = annotatedElement.getAnnotation(annotationClass);
        if (result != null) {
            return result;
        }

        return findAnnotationInAnnotations(annotatedElement, annotationClass);
    }

    /**
     * Deep search of specified annotation considering "annotation inheritance" (annotation annotated with specified annotation).
     *
     * @param annotation
     * @param annotationClass
     * @param <A>
     * @return
     */
    public static <A extends Annotation> A findAnnotation(Annotation annotation, Class<A> annotationClass) {
        Set<Class<? extends Annotation>> visitedAnnotations = new HashSet<Class<? extends Annotation>>();

        return findAnnotation(annotation, annotationClass, visitedAnnotations);
    }

    private static <A extends Annotation> A findAnnotation(Annotation currentAnnotation, Class<A> annotationClass,
            Set<Class<? extends Annotation>> visitedAnnotations) {

        Class<? extends Annotation> currentAnnotationType = currentAnnotation.annotationType();
        visitedAnnotations.add(currentAnnotationType);

        A result = currentAnnotationType.getAnnotation(annotationClass);
        if (result != null) {
            return result;
        }

        Annotation[] allAnnotations = currentAnnotationType.getAnnotations();
        for (Annotation annotation : allAnnotations) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (visitedAnnotations.contains(annotationType)) {
                continue;
            }

            result = findAnnotation(annotation, annotationClass, visitedAnnotations);
            if (result != null) {
                return result;
            }
        }

        return null;
    }
}

Related

  1. findAnnotationDeclaringClass(Class annotationType, Class clazz)
  2. findAnnotationFromClass(Class target, Class annotation)
  3. findAnnotationFromMethodOrClass(final Method method, final Class annotationClass)
  4. findAnnotationHelper( Class base, Type iface)
  5. findAnnotationIn(List modifiers, Class clazz)
  6. findAnnotationInAnyParameter(Method method, Class annotationClass)
  7. findAnnotationInMethodOrInItsAnnotations(Method method, Class annotationClass)
  8. findAnnotationMethods(String fullClassName, Class anno)
  9. findAnnotationsMap(Class a, Class c)