Java Reflection Annotation getAnnotation(Class annotation, Class ownerClass, Method method)

Here you can find the source of getAnnotation(Class annotation, Class ownerClass, Method method)

Description

Retrieves the annotation from the method, if it is not on the method, the parent is checked.

License

Open Source License

Parameter

Parameter Description
ownerClass a parameter
method a parameter

Declaration

public static <T extends Annotation> T getAnnotation(Class<T> annotation, Class<?> ownerClass, Method method) 

Method Source Code


//package com.java2s;
/*//from  w w  w.  j av  a 2s .c  o  m
   Leola Programming Language
   Author: Tony Sparks
   See license.txt
*/

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class Main {
    /**
     * Retrieves the annotation from the method, if it is not on the method, the parent
     * is checked.  This solves the "inherited annotation" problem for interfaces.
     * @param ownerClass
     * @param method
     * @return
     */
    public static <T extends Annotation> T getAnnotation(Class<T> annotation, Class<?> ownerClass, Method method) {
        if (method == null) {
            return null;
        }

        if (method.isAnnotationPresent(annotation)) {
            return method.getAnnotation(annotation);
        } else {
            for (Class<?> aInterface : ownerClass.getInterfaces()) {
                Method inheritedMethod = getInheritedMethod(aInterface, method);
                if (inheritedMethod != null) {
                    T result = getAnnotation(annotation, aInterface, inheritedMethod);
                    if (result != null) {
                        return result;
                    }
                }
            }

            /* Query the parent class for the annotation */
            Class<?> superClass = method.getDeclaringClass().getSuperclass();
            Method inheritedMethod = getInheritedMethod(superClass, method);

            return getAnnotation(annotation, superClass, inheritedMethod);
        }
    }

    /**
     * Get the inherited method.
     * @param aClass
     * @param method
     * @return
     */
    public static Method getInheritedMethod(Class<?> aClass, Method method) {
        Method inheritedMethod = null;
        try {
            for (Class<?> a = aClass; a != null; a = a.getSuperclass()) {
                inheritedMethod = aClass.getMethod(method.getName(), method.getParameterTypes());
                if (inheritedMethod != null) {
                    return inheritedMethod;
                }
            }
        } catch (Exception e) {
            /* Ignore */
        }

        return null;
    }
}

Related

  1. getAnnotation(Class ac, AnnotatedElement m, Annotation... directAnnotations)
  2. getAnnotation(Class annotationType, Class classType)
  3. getAnnotation(Class at, Enum enu)
  4. getAnnotation(Class realClass, String pAttributeName, Class annotationClass)
  5. getAnnotation(Class a, Class c)
  6. getAnnotation(Class annotationClass, Class cls)
  7. getAnnotation(Class annotationType, AnnotatedElement... objects)
  8. getAnnotation(Class cl, Object o)
  9. getAnnotation(Class clazz, Method method, int parameterIndex)