Here you can find the source of getAnnotation(Class
cls
classcls
implementing interfacescls
super classnull
is returned.
Parameter | Description |
---|---|
T | the annotation type |
annotationClass | the annotation class |
cls | the class to start searching |
public static <T extends Annotation> T getAnnotation(Class<T> annotationClass, Class<?> cls)
//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; } }