Java Reflection Annotation getAnnotation(Class objectClass, Class annotationClass)

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

Description

get Annotation

License

Apache License

Declaration

public static <T extends Annotation> T getAnnotation(Class<?> objectClass, Class<T> annotationClass) 

Method Source Code

//package com.java2s;
/*/*from w  ww.j av a2  s.  co m*/
 * Copyright (C) 2016 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;

public class Main {
    /**
     * Extract the annotation from the controllerMethod 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 annotation = method.getAnnotation(annotationClass);
        if (annotation == null) {
            annotation = getAnnotation(method.getDeclaringClass(), annotationClass);
        }

        return annotation;
    }

    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(Class cls, Class annotationClass)
  2. getAnnotation(Class cls, Class annotation)
  3. getAnnotation(Class componentClass, Class annotationClass)
  4. getAnnotation(Class configInterface, Method method, Class annotationType, boolean searchMethodType)
  5. getAnnotation(Class klazz, Class annotationClass)
  6. getAnnotation(Class onClass, Class desiredAnnotationClass)
  7. getAnnotation(Class target, Class annoCls)
  8. getAnnotation(Class target, Class annotationClass)
  9. getAnnotation(Class theClass, Class theAnnotation)