Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotation that expands/resolves to many annotations?

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?

like image 804
Bjarke Freund-Hansen Avatar asked Sep 09 '11 12:09

Bjarke Freund-Hansen


People also ask

Can an annotation extend another annotation Java?

In Java SE 6, annotations cannot subclass one another, and an annotation is not allowed to extend/implement any interfaces.

What is @target annotation in Java?

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.

What is repeatable 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.

What is inheritance 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.


1 Answers

In general, there is no way. But if you are using spring ( >= 3.0 ) it is possible with custom annotations. Simple example here.

like image 71
Ahe Avatar answered Sep 23 '22 21:09

Ahe