I have a set of Java annotation that I quite frequently use, like this:
@SomeAnnotation(Something)
@SomeOtherAnnotation
@SomeLastAnnotation(SomethingElse)
class Foo
{
/* ... */
}
As I use all these annotations together quite often, and I may have to add to them in everywhere they are used once in a while, I would like to create a new annotation that I can use instead. This annotation should then "resolve" to all the annotations that I define somewhere.
@MySuperAnnotation
class Foo
{
/* ... */
}
How do I declare @MySuperAnnotation
such that it "resolves" to all the other annotations?
In Java SE 6, annotations cannot subclass one another, and an annotation is not allowed to extend/implement any interfaces.
If an @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7. 4. For example, this @Target meta-annotation indicates that the declared type is itself a meta-annotation type.
The value of the @Repeatable meta-annotation, in parentheses, is the type of the container annotation that the Java compiler generates to store repeating annotations. In this example, the containing annotation type is Schedules , so repeating @Schedule annotations is stored in an @Schedules annotation.
Annotations, just like methods or fields, can be inherited between class hierarchies. If an annotation declaration is marked with @Inherited , then a class that extends another class with this annotation can inherit it. The annotation can be overridden in case the child class has the annotation.
In general, there is no way. But if you are using spring ( >= 3.0 ) it is possible with custom annotations. Simple example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With