Java Reflection Annotation getAnnotation(Class componentClass, Class annotationClass)

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

Description

Search for the given annotationClass in the given componentClass and return it if search was successful.

License

Apache License

Parameter

Parameter Description
T the generic type
componentClass the component class
annotationClass the annotation class

Return

the annotation

Declaration

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

Method Source Code

//package com.java2s;
/**//from ww  w .j a va  2  s.c  o  m
 * Copyright (C) 2007 Asterios Raptis
 *
 * 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;

public class Main {
    /**
     * Search for the given annotationClass in the given componentClass and return it if search was
     * successful.
     *
     * @param <T>
     *            the generic type
     * @param componentClass
     *            the component class
     * @param annotationClass
     *            the annotation class
     * @return the annotation
     */
    public static <T extends Annotation> T getAnnotation(Class<?> componentClass, Class<T> annotationClass) {
        T annotation = componentClass.getAnnotation(annotationClass);
        if (annotation != null) {
            return annotation;
        }
        for (Class<?> ifc : componentClass.getInterfaces()) {
            annotation = getAnnotation(ifc, annotationClass);
            if (annotation != null) {
                return annotation;
            }
        }
        if (!Annotation.class.isAssignableFrom(componentClass)) {
            for (Annotation ann : componentClass.getAnnotations()) {
                annotation = getAnnotation(ann.annotationType(), annotationClass);
                if (annotation != null) {
                    return annotation;
                }
            }
        }
        Class<?> superClass = componentClass.getSuperclass();
        if (superClass == null || superClass.equals(Object.class)) {
            return null;
        }
        return getAnnotation(superClass, annotationClass);
    }
}

Related

  1. getAnnotation(Class clazz, Class annotationType)
  2. getAnnotation(Class clazz, String fieldName, Class annotationClass)
  3. getAnnotation(Class clazz, String name, Class[] types, Class annotationType)
  4. getAnnotation(Class cls, Class annotationClass)
  5. getAnnotation(Class cls, Class annotation)
  6. getAnnotation(Class configInterface, Method method, Class annotationType, boolean searchMethodType)
  7. getAnnotation(Class klazz, Class annotationClass)
  8. getAnnotation(Class objectClass, Class annotationClass)
  9. getAnnotation(Class onClass, Class desiredAnnotationClass)