Here you can find the source of getAnnotation(Class> theClass, Class
Parameter | Description |
---|---|
theClass | the class to inspect |
theAnnotation | the annotation to retrieve |
public static <T extends Annotation> T getAnnotation(Class<?> theClass, Class<T> theAnnotation)
//package com.java2s; //License from project: Apache License import java.lang.annotation.Annotation; public class Main { /**// w w w . j av a 2 s . co m * Return the given annotation from the class. If the class does not have the annotation, it's parent class and any * interfaces will also be checked. * @param theClass the class to inspect * @param theAnnotation the annotation to retrieve * @return the class's annotation, or it's "inherited" annotation, or null if the annotation cannot be found. */ public static <T extends Annotation> T getAnnotation(Class<?> theClass, Class<T> theAnnotation) { T aAnnotation = null; if (theClass.isAnnotationPresent(theAnnotation)) { aAnnotation = theClass.getAnnotation(theAnnotation); } else { if (shouldInspectClass(theClass.getSuperclass())) aAnnotation = getAnnotation(theClass.getSuperclass(), theAnnotation); if (aAnnotation == null) { for (Class<?> aInt : theClass.getInterfaces()) { aAnnotation = getAnnotation(aInt, theAnnotation); if (aAnnotation != null) { break; } } } } return aAnnotation; } private static boolean shouldInspectClass(final Class<?> theClass) { return !Object.class.equals(theClass) && theClass != null; } }