Java Reflection Annotation getAnnotation(Method method, Class annotationClass)

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

Description

Extract the annotation from the method or the declaring class.

License

Apache License

Parameter

Parameter Description
method a parameter
annotationClass a parameter
T a parameter

Return

the annotation or null

Declaration

public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) 

Method Source Code

//package com.java2s;
/*/*w w w .java 2s  .  co m*/
 * Copyright (C) 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Main {
    /**
     * Extract the annotation from the method or the declaring class.
     *
     * @param method
     * @param annotationClass
     * @param <T>
     * @return the annotation or null
     */
    public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
        T t = method.getAnnotation(annotationClass);
        if (t == null) {
            t = getAnnotation(method.getDeclaringClass(), annotationClass);
        }
        return t;
    }

    public static <T extends Annotation> T getAnnotation(Parameter parameter, Class<T> annotationClass) {
        for (Annotation annotation : parameter.getAnnotations()) {
            if (annotation.annotationType() == annotationClass) {
                return (T) annotation;
            }
        }
        return null;
    }

    public static <T extends Annotation> T getAnnotation(Class<?> objectClass, Class<T> annotationClass) {
        if (objectClass == null || Object.class == objectClass) {
            return null;
        }

        T annotation = objectClass.getAnnotation(annotationClass);
        if (annotation != null) {
            return annotation;
        }

        return getAnnotation(objectClass.getSuperclass(), annotationClass);
    }
}

Related

  1. getAnnotation(final Object obj, final Class annoType)
  2. getAnnotation(final Object object, final Class annotationClass)
  3. getAnnotation(Member m, Class annotationClass)
  4. getAnnotation(Method m, Class annotationClass)
  5. getAnnotation(Method method, Class annotationType)
  6. getAnnotation(Method method, Class annotationClass)
  7. getAnnotation(Method method, Class type)
  8. getAnnotation(Object bean, final Class annotation)
  9. getAnnotation(Object context, Class annotation)