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

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

Description

Gets the Annotation for the method, null if does not exists.

License

Open Source License

Parameter

Parameter Description
method Method
annotationType Class

Return

Annotation

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class Main {
    /**//  w  w w  .j  a  v a 2  s. co  m
     * Gets the Annotation for the method, null if does not exists.
     * @param method Method
     * @param annotationType Class
     * @return Annotation
     */
    public static <T extends Annotation> T getMethodAnnotation(final Method method, final Class<T> annotationType) {

        final Annotation[] annotations = method.getDeclaredAnnotations();

        for (Annotation annotation : annotations) {

            if (annotation.annotationType().equals(annotationType)) {

                return (T) annotation;
            }
        }

        return null;
    }
}

Related

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