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

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

Description

Find an annotation of given annotationType on the given element , considering that the given annotationType may be present as a meta annotation on any other annotation on that element.

License

Apache License

Parameter

Parameter Description
element The element to inspect
annotationType The type of annotation to find
T The generic type of the annotation

Return

the annotation, or null if no such annotation is present.

Declaration

@SuppressWarnings("unchecked")
public static <T extends Annotation> T findAnnotation(AnnotatedElement element, Class<T> annotationType) 

Method Source Code


//package com.java2s;
/*// www.  j  av  a2  s  . c  o  m
 * Copyright (c) 2010-2016. Axon Framework
 *
 * 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.
 */

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

import java.util.*;

public class Main {
    /**
     * Find an annotation of given {@code annotationType} on the given {@code element}, considering that the
     * given {@code annotationType} may be present as a meta annotation on any other annotation on that element.
     *
     * @param element        The element to inspect
     * @param annotationType The type of annotation to find
     * @param <T>            The generic type of the annotation
     * @return the annotation, or {@code null} if no such annotation is present.
     */
    @SuppressWarnings("unchecked")
    public static <T extends Annotation> T findAnnotation(AnnotatedElement element, Class<T> annotationType) {
        return (T) findAnnotation(element, annotationType.getName());
    }

    /**
     * Find an annotation of given {@code annotationType} as String on the given {@code element},
     * considering that the given {@code annotationType} may be present as a meta annotation on any other
     * annotation on that element.
     *
     * @param element        The element to inspect
     * @param annotationType The type of annotation as String to find
     * @return the annotation, or {@code null} if no such annotation is present.
     */
    public static Annotation findAnnotation(AnnotatedElement element, String annotationType) {
        Annotation ann = getAnnotation(element, annotationType);
        if (ann == null) {
            Set<String> visited = new HashSet<>();
            for (Annotation metaAnn : element.getAnnotations()) {
                ann = getAnnotation(metaAnn.annotationType(), annotationType, visited);
                if (ann != null) {
                    break;
                }
            }
        }
        return ann;
    }

    private static Annotation getAnnotation(AnnotatedElement target, String annotationType) {
        for (Annotation annotation : target.getAnnotations()) {
            if (annotationType.equals(annotation.annotationType().getName())) {
                return annotation;
            }
        }
        return null;
    }

    private static Annotation getAnnotation(Class<? extends Annotation> target, String annotationType,
            Set<String> visited) {
        Annotation ann = getAnnotation(target, annotationType);
        if (ann == null && visited.add(target.getName())) {
            for (Annotation metaAnn : target.getAnnotations()) {
                ann = getAnnotation(metaAnn.annotationType(), annotationType, visited);
                if (ann != null) {
                    break;
                }
            }
        }
        return ann;
    }
}

Related

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