/** * Discover if a Class <b>c</b> has been annotated with <b>type</b>. This also discovers annotations defined through * a @{@link Stereotype}. * * @param c The class to inspect. * @param type The targeted annotation class * @return True if annotation is present either on class, false if the annotation is not present. */ public static boolean isAnnotationPresent(final Class<?> c, final Class<? extends Annotation> type) { return getAnnotation(c, type) != null; }
while (superclass != null) result = getAnnotation(superclass, type); if (result != null) break; for (Class<?> i : interfaces) result = getAnnotation(i, type); if (result != null) break;
/** * Inspect annotation <b>a</b> for a specific <b>type</b> of annotation. This also discovers annotations defined * through a @ {@link Stereotype}. * * @param m The method to inspect. * @param type The targeted annotation class * @return The annotation instance found on this method or enclosing class, or null if no matching annotation was * found. */ public static <A extends Annotation> A getAnnotation(final Annotation a, final Class<A> type) { Set<Annotation> seen = new HashSet<Annotation>(); return getAnnotation(seen, a, type); }
/** * Inspect method <b>m</b> for a specific <b>type</b> of annotation. This also discovers annotations defined through * a @ {@link Stereotype}. * * @param m The method to inspect. * @param type The targeted annotation class * @return The annotation instance found on this method or enclosing class, or null if no matching annotation was * found. */ public static <A extends Annotation> A getAnnotation(final Method m, final Class<A> type) { A result = m.getAnnotation(type); if (result == null) { for (Annotation a : m.getAnnotations()) { result = getAnnotation(a, type); if (result != null) { break; } } } if (result == null) { result = getAnnotation(m.getDeclaringClass(), type); } return result; }
result = getAnnotation(a.annotationType().getDeclaringClass(), type); result = getAnnotation(seen, nested, type);