Java examples for Reflection:Class
Find the first Class in the inheritance hierarchy of the specified clazz (including the specified clazz itself)
import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.WeakHashMap; public class Main{ /**/* ww w .java2 s . c o m*/ * Find the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code> * (including the specified <code>clazz</code> itself) which declares an annotation for the * specified <code>annotationType</code>, or <code>null</code> if not found. If the supplied * <code>clazz</code> is <code>null</code>, <code>null</code> will be returned. * <p>If the supplied <code>clazz</code> is an interface, only the interface itself will be checked; * the inheritance hierarchy for interfaces will not be traversed. * <p>The standard {@link Class} API does not provide a mechanism for determining which class * in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle * this explicitly. * @param annotationType the Class object corresponding to the annotation type * @param clazz the Class object corresponding to the class on which to check for the annotation, * or <code>null</code> * @return the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code> * which declares an annotation for the specified <code>annotationType</code>, or <code>null</code> * if not found * @see Class#isAnnotationPresent(Class) * @see Class#getDeclaredAnnotations() */ public static Class<?> findAnnotationDeclaringClass( Class<? extends Annotation> annotationType, Class<?> clazz) { Assert.notNull(annotationType, "Annotation type must not be null"); if (clazz == null || clazz.equals(Object.class)) { return null; } return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz : findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()); } /** * Determine whether an annotation for the specified <code>annotationType</code> is * declared locally on the supplied <code>clazz</code>. The supplied {@link Class} * may represent any type. * <p>Note: This method does <strong>not</strong> determine if the annotation is * {@link java.lang.annotation.Inherited inherited}. For greater clarity regarding inherited * annotations, consider using {@link #isAnnotationInherited(Class, Class)} instead. * @param annotationType the Class object corresponding to the annotation type * @param clazz the Class object corresponding to the class on which to check for the annotation * @return <code>true</code> if an annotation for the specified <code>annotationType</code> * is declared locally on the supplied <code>clazz</code> * @see Class#getDeclaredAnnotations() * @see #isAnnotationInherited(Class, Class) */ public static boolean isAnnotationDeclaredLocally( Class<? extends Annotation> annotationType, Class<?> clazz) { Assert.notNull(annotationType, "Annotation type must not be null"); Assert.notNull(clazz, "Class must not be null"); boolean declaredLocally = false; for (Annotation annotation : Arrays.asList(clazz .getDeclaredAnnotations())) { if (annotation.annotationType().equals(annotationType)) { declaredLocally = true; break; } } return declaredLocally; } }