Java Reflection Annotation getAnnotation(Class annotationClass, Class cls)

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

Description

Recursively searches for an annotation

Search order

  1. cls class
  2. cls implementing interfaces
  3. cls super class
If after all those steps, no annotation is found, null is returned.

License

Open Source License

Parameter

Parameter Description
T the annotation type
annotationClass the annotation class
cls the class to start searching

Return

the annotation, if found.

Declaration

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

Method Source Code


//package com.java2s;
/*/*from w w  w .  j  a  va 2s . c om*/
 * This file is part of l2jserver <l2jserver.com>.
 *
 * l2jserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * l2jserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with l2jserver.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.annotation.Annotation;

public class Main {
    /**
     * Recursively searches for an annotation <h1>Search order</h1>
     * <p>
     * <ol>
     * <li><code>cls</code> class</li>
     * <li><code>cls</code> implementing interfaces</code></li>
     * <li><code>cls</code> super class</code></li>
     * </ol>
     * If after all those steps, no annotation is found, <code>null</code> is
     * returned.
     * 
     * @param <T>
     *            the annotation type
     * 
     * @param annotationClass
     *            the annotation class
     * @param cls
     *            the class to start searching
     * @return the annotation, if found.
     */
    public static <T extends Annotation> T getAnnotation(Class<T> annotationClass, Class<?> cls) {
        T annotation = cls.getAnnotation(annotationClass);
        if (annotation == null) {
            for (final Class<?> interfaceCls : cls.getInterfaces()) {
                annotation = getAnnotation(annotationClass, interfaceCls);
                if (annotation != null)
                    break;
            }
        }
        if (annotation == null && cls.getSuperclass() != null && cls.getSuperclass() != Object.class)
            annotation = getAnnotation(annotationClass, cls.getSuperclass());
        return annotation;
    }
}

Related

  1. getAnnotation(Class annotationType, Class classType)
  2. getAnnotation(Class at, Enum enu)
  3. getAnnotation(Class realClass, String pAttributeName, Class annotationClass)
  4. getAnnotation(Class a, Class c)
  5. getAnnotation(Class annotation, Class ownerClass, Method method)
  6. getAnnotation(Class annotationType, AnnotatedElement... objects)
  7. getAnnotation(Class cl, Object o)
  8. getAnnotation(Class clazz, Method method, int parameterIndex)
  9. getAnnotation(Class cls, Annotation... annotations)