Here you can find the source of getAnnotationWithinHierarchy(Class> fsmClass, Class aggregateClass)
public static <A extends Annotation> Object getAnnotationWithinHierarchy(Class<?> fsmClass, Class<A> aggregateClass) throws ReflectiveOperationException
//package com.java2s; //License from project: Apache License import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Proxy; public class Main { /**/*w w w . j av a 2s. c o m*/ * Get annotation within hierarchy */ public static <A extends Annotation> Object getAnnotationWithinHierarchy(Class<?> fsmClass, Class<A> aggregateClass) throws ReflectiveOperationException { while (fsmClass != null) { if (getAnnotation(fsmClass, aggregateClass) != null) { return getAnnotation(fsmClass, aggregateClass); } fsmClass = fsmClass.getSuperclass();//NOSONAR } return null; } /** * Get annotation of an object via reflection */ public static Object getAnnotation(AnnotatedElement aobj, Class aClass) throws ReflectiveOperationException { for (Object a : aobj.getAnnotations()) { if (isAnnotationInstance(aClass, a)) return a; } return null; } private static boolean isAnnotationInstance(Class aClass, Object a) { if (Proxy.isProxyClass(a.getClass())) { for (Class aInterface : a.getClass().getInterfaces()) { if (aInterface.isAssignableFrom(aClass)) { return true; } } } return aClass.isInstance(a); } }