Is there something like Inheritance annotation in java?

I study annotations and have come to the point where some annotations seem to have a hierarchy among them.

I use annotations to generate code in the background for Maps. There are different types of cards (for example, different codes and annotations), but there are some elements that are common among them, like a name.

@Target(value = {ElementType.TYPE}) public @interface Move extends Page{ String method1(); String method2(); } 

And this will be a general annotation:

 @Target(value = {ElementType.TYPE}) public @interface Page{ String method3(); } 

In the above example, I would expect Move to inherit method3, but I get a warning that extends is not valid with annotations. I tried for the annotation to distribute a common base, but this does not work. Is this possible or just a design problem?

+65
java inheritance annotations
Oct 13 '11 at 23:16
source share
3 answers

Unfortunately not. Apparently, this has something to do with programs that read annotations in a class without loading them completely. See Why can't annotations extend in Java?

However, types inherit their superclass annotations if these annotations are @Inherited .

Also, if you don't need these methods for interaction, you can simply add annotations in your class:

 @Move @Page public class myAwesomeClass {} 

Is there any reason that won't work for you?

+46
Oct. 13 '11 at 23:24
source share

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.

+29
Sep 03 '13 at 6:47
source share

In addition to Grygoriys annotation annotations answer.

You can check for example. methods for adding @Qualifier annotation (or annotation annotated with @Qualifier) ​​in this loop:

 for (Annotation a : method.getAnnotations()) { if (a.annotationType().isAnnotationPresent(Qualifier.class)) { System.out.println("found @Qualifier annotation");//found annotation having Qualifier annotation itself } } 

What you basically do is to get all the annotations present in the method and those annotations that you got, and check these types if they are annotated using @Qualifier. Your annotation should be Target.Annotation_type included as well to make this work.

+5
Oct 30 '14 at 12:33
source share



All Articles