Java Reflection Annotation Find findAnnotation(Class clazz, Class annotationType)

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

Description

Look for the annotationType in the class or one of its ancestors.

License

LGPL

Parameter

Parameter Description
clazz The class to scan.
annotationType The annotation type to find.

Return

The annotation, null if not found.

Declaration

public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationType) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.lang.annotation.Annotation;

public class Main {
    /**/*from   w  w  w  .j a v  a2s . co  m*/
     * Look for the annotationType in the class or one of its ancestors.
     *
     * @param clazz          The class to scan.
     * @param annotationType The annotation type to find.
     * @return The annotation, null if not found.
     */
    public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationType) {
        T a = clazz.getAnnotation(annotationType);
        if (a != null) {
            return a;
        }

        Class<?> parentClass = clazz.getSuperclass();
        if (parentClass != null && !parentClass.equals(Object.class)) {
            a = findAnnotation(parentClass, annotationType);
        }

        if (a == null) {
            for (Class<?> i : clazz.getInterfaces()) {
                a = findAnnotation(i, annotationType);
                if (a != null) {
                    return a;
                }
            }
        }

        return a;
    }
}

Related

  1. findAnnotation(Class clazz, Class annotationType)
  2. findAnnotation(Class clazz, Class annotationType)
  3. findAnnotation(Class clazz, Class annotationClass)
  4. findAnnotation(Class clazz, Class annotationClass)
  5. findAnnotation(Class clazz, Class annotationClass)
  6. findAnnotation(Class klass, Class annotationClass)
  7. findAnnotation(final Class clazz, final Class type)
  8. findAnnotation(final Class clazz, final Class annotation)
  9. findAnnotation(final Class type, final Class annotationType)