Here you can find the source of getAnnotation(Class> onClass, Class desiredAnnotationClass)
Parameter | Description |
---|---|
onClass | the Class on which seaching will take place |
desiredAnnotationClass | the desired Annotation Class |
A | the type of the Annotation |
null
if not available
public static <A extends Annotation> A getAnnotation(Class<?> onClass, Class<A> desiredAnnotationClass)
//package com.java2s; /*/*from w w w.jav a2 s . c om*/ * File: ReflectionHelper.java * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * The contents of this file are subject to the terms and conditions of * the Common Development and Distribution License 1.0 (the "License"). * * You may not use this file except in compliance with the License. * * You can obtain a copy of the License by consulting the LICENSE.txt file * distributed with this file, or by consulting https://oss.oracle.com/licenses/CDDL * * See the License for the specific language governing permissions * and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file LICENSE.txt. * * MODIFICATIONS: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" */ import java.lang.annotation.Annotation; public class Main { /** * Obtains the specified {@link Annotation} instance on the provided {@link Class}, * interfaces implemented by the {@link Class} and/or the super-class(es)/super-interface(s) * of the {@link Class}. * <p> * Should the {@link Annotation} not be defined on the above, <code>null</code> is returned. * * @param onClass the {@link Class} on which seaching will take place * @param desiredAnnotationClass the desired {@link Annotation} {@link Class} * @param <A> the type of the {@link Annotation} * * @return the {@link Annotation} or <code>null</code> if not available */ public static <A extends Annotation> A getAnnotation(Class<?> onClass, Class<A> desiredAnnotationClass) { if (onClass == null || desiredAnnotationClass == null) { // when no class or annotation has been provided, we can't locate the desired annotation return null; } else if (onClass.getClass().equals(Object.class)) { // we assume that the object class doesn't have the annotation we want return null; } else { // first check if the annotation is defined directly on the class A annotation = onClass.getAnnotation(desiredAnnotationClass); if (annotation == null) { // look for the annotation on all of the interfaces for (Class<?> onInterface : onClass.getInterfaces()) { annotation = getAnnotation(onInterface, desiredAnnotationClass); if (annotation != null) { return annotation; } } // finally look on the super class return getAnnotation(onClass.getSuperclass(), desiredAnnotationClass); } else { return annotation; } } } }