You can annotate your annotation with a basic annotation instead of inheritance. This is used in the Spring framework .
To give an example
@Target(value = {ElementType.ANNOTATION_TYPE}) public @interface Vehicle { } @Target(value = {ElementType.TYPE}) @Vehicle public @interface Car { } @Car class Foo { }
Then you can check if the class was annotated with Vehicle using Spring AnnotationUtils :
Vehicle vehicleAnnotation = AnnotationUtils.findAnnotation (Foo.class, Vehicle.class); boolean isAnnotated = vehicleAnnotation != null;
This method is implemented as:
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) { return findAnnotation(clazz, annotationType, new HashSet<Annotation>()); } @SuppressWarnings("unchecked") private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) { try { Annotation[] anns = clazz.getDeclaredAnnotations(); for (Annotation ann : anns) { if (ann.annotationType() == annotationType) { return (A) ann; } } for (Annotation ann : anns) { if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) { A annotation = findAnnotation(ann.annotationType(), annotationType, visited); if (annotation != null) { return annotation; } } } } catch (Exception ex) { handleIntrospectionFailure(clazz, ex); return null; } for (Class<?> ifc : clazz.getInterfaces()) { A annotation = findAnnotation(ifc, annotationType, visited); if (annotation != null) { return annotation; } } Class<?> superclass = clazz.getSuperclass(); if (superclass == null || Object.class == superclass) { return null; } return findAnnotation(superclass, annotationType, visited); }
AnnotationUtils also contains additional methods for finding annotations by methods and other annotated elements. The Spring class is also effective enough for searching through bridge methods, proxies, and other corner cabinets, especially those found in Spring.
Grygoriy Gonchar Sep 03 '13 at 6:47 a.m.
source share