Java Reflection Annotation Find findAnnotation(AnnotatedElement annotatedElement, Class annotationType)

Here you can find the source of findAnnotation(AnnotatedElement annotatedElement, Class annotationType)

Description

Searches for an annotation of a given type that's been applied to an element either directly (as a regular annotation) or indirectly (as a meta-annotations, i.e.

License

Apache License

Parameter

Parameter Description
annotatedElement the element whose annotations will be searched
annotationType the type of annotation to search for
A the type of the annotation being searched for

Return

the annotation associated with the given element, or null if not found.

Declaration

public static <A> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 uniVocity Software Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*from   w  w  w  .ja v  a2s  .c o m*/
 ******************************************************************************/

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

import java.util.*;

public class Main {
    private static final Set<Class> javaLangAnnotationTypes = new HashSet<Class>();
    private static final Set<Class> customAnnotationTypes = new HashSet<Class>();

    /**
     * Searches for an annotation of a given type that's been applied to an element either directly (as a regular annotation)
     * or indirectly (as a meta-annotations, i.e. an annotation that has annotations).
     *
     * @param annotatedElement the element whose annotations will be searched
     * @param annotationType   the type of annotation to search for
     * @param <A>              the type of the annotation being searched for
     *
     * @return the annotation associated with the given element, or {@code null} if not found.
     */
    public static <A> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
        if (annotationType == null) {
            return null;
        }

        return findAnnotation(annotatedElement, annotationType, new HashSet<Annotation>());
    }

    private static <A> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType,
            Set<Annotation> visited) {
        Annotation[] declaredAnnotations = annotatedElement.getDeclaredAnnotations();
        for (int i = 0; i < declaredAnnotations.length; i++) {
            Annotation ann = declaredAnnotations[i];
            if (ann.annotationType() == annotationType) {
                return (A) ann;
            }
        }
        for (int i = 0; i < declaredAnnotations.length; i++) {
            Annotation ann = declaredAnnotations[i];
            if (isCustomAnnotation(ann) && visited.add(ann)) {
                A annotation = findAnnotation(ann.annotationType(), annotationType, visited);
                if (annotation != null) {
                    return annotation;
                }
            }
        }
        return null;
    }

    private static boolean isCustomAnnotation(Annotation annotation) {
        Class annotationType = annotation.annotationType();
        if (customAnnotationTypes.contains(annotationType)) {
            return true;
        }
        if (javaLangAnnotationTypes.contains(annotationType)) {
            return false;
        }
        if (annotationType.getName().startsWith("java.lang.annotation")) {
            javaLangAnnotationTypes.add(annotationType);
            return false;
        } else {
            customAnnotationTypes.add(annotationType);
            return true;
        }
    }
}

Related

  1. findAnnotation(AnnotatedElement annotated, Class annotationClass)
  2. findAnnotation(AnnotatedElement annotatedElement, Class annotationClass)
  3. findAnnotation(AnnotatedElement element, Class annotationType)
  4. findAnnotation(Annotation parentAnnotation, Class annotationType)
  5. findAnnotation(Annotation[] parameterAnnotations, Class targetAnnotation)
  6. findAnnotation(Annotation[] searchList, Class annotation)