Java Reflection Method Annotation getMethodAnnotation(Method method, Class annotationType)

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

Description

get Method Annotation

License

Apache License

Declaration

public static <T extends Annotation> T getMethodAnnotation(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;

import java.util.Arrays;

public class Main {
    public static <T extends Annotation> T getMethodAnnotation(Method method, Class<T> annotationType) {
        T ann = method.getAnnotation(annotationType);
        if (ann == null) {
            // Inspect interface methods
            for (Class iface : method.getDeclaringClass().getInterfaces()) {
                for (Method otherMethod : iface.getMethods()) {
                    if (isMatchingMethodSignatures(method, otherMethod)) {
                        return getMethodAnnotation(otherMethod, annotationType);
                    }//w w  w  .  jav a  2  s . co m
                }
            }
        }
        return ann;
    }

    /**
     * @param method
     * @param otherMethod
     * @return
     */
    public static boolean isMatchingMethodSignatures(Method method, Method otherMethod) {
        return calcMethodSignatureHash(method) == calcMethodSignatureHash(otherMethod);
    }

    private static int calcMethodSignatureHash(Method m) {
        return m.getName().hashCode() ^ m.getReturnType().hashCode() ^ Arrays.hashCode(m.getParameterTypes());
    }
}

Related

  1. getMethodAnnotatedWith(Class toIntrospect, Class c)
  2. getMethodAnnotation(Class a, Method m)
  3. getMethodAnnotation(final Method method, final Class annoClass)
  4. getMethodAnnotation(final Method method, final Class annotationType)
  5. getMethodAnnotation(Method method, Class annotation)
  6. getMethodAnnotation(Method method, Class clazz)
  7. getMethodAnnotationMap(Method method, Collection> annotationClasses)
  8. getMethodAnnotations(Method m)
  9. getMethodByAnnotation(Class cls, Class annotationClass)