Java Reflection Annotation Find findAnnotation(Method method, Class annotationType)

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

Description

find Annotation

License

Apache License

Declaration

static public <T extends Annotation> T findAnnotation(Method method, Class<T> annotationType) 

Method Source Code


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

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

public class Main {
    static public <T extends Annotation> T findAnnotation(Method method, Class<T> annotationType) {
        T anno = method.getAnnotation(annotationType);
        if (anno != null) {
            return anno;
        }/*w  w w.j av  a 2  s  . com*/
        return findAnnotation(method.getDeclaringClass(), method, annotationType);
    }

    static public <T extends Annotation> T findAnnotation(Class<?> inspectedClass, Method prototype,
            Class<T> annotationType) {
        T anno = null;
        if (inspectedClass == null) {
            return null;
        }
        // look for a matching method, first
        try {
            Method candidateMethod = inspectedClass.getMethod(prototype.getName(), prototype.getParameterTypes());
            anno = candidateMethod.getAnnotation(annotationType);
            if (anno != null) {
                return anno;
            }
        } catch (NoSuchMethodException | SecurityException e) {
            // fall through
        }

        // see if the class has an annotation
        anno = inspectedClass.getAnnotation(annotationType);
        if (anno != null) {
            return anno;
        }

        // see if any of our interfaces have the annotation
        for (Class<?> i : inspectedClass.getInterfaces()) {
            anno = findAnnotation(i, prototype, annotationType);
            if (anno != null) {
                return anno;
            }
        }

        // climb the inheritance tree
        if (inspectedClass.getSuperclass() != Object.class) {
            return findAnnotation(inspectedClass.getSuperclass(), prototype, annotationType);
        }
        return null;
    }
}

Related

  1. findAnnotation(final Class clazz, final Class annotation)
  2. findAnnotation(final Class type, final Class annotationType)
  3. findAnnotation(final Class targetAnnotation, final Class annotatedType)
  4. findAnnotation(final Class annotationClass, final Class beanClass, final Field field)
  5. findAnnotation(final Set annotations, final Class annotationType)
  6. findAnnotation(Method method, Class type)
  7. findAnnotation(Set annotations, Class annotationClass)
  8. findAnnotationClass( Class c, Class base)
  9. findAnnotationDeclaringClass(Class annotationType, Class clazz)