Java Reference
In-Depth Information
2-4. Applying the Same Annotation Mul-
tiple Times in a Declaration or Type Use
Problem
You want to apply the same annotation more than once to a specified declaration or
type.
Solution
The same annotation can be applied to a declaration or type more than once, given that
each annotation is marked as @Repeatable . In the following code, the @Repeat-
able annotation is used to develop an annotation that can be repeated, rather than
grouped together as in previous releases of Java. In this situation, an annotation named
Role is being created, and it will be used to signify a role for an annotated class or
method.
@Repeatable(value=Roles.class)
public @interface Role {
String name() default "AUTHOR";
}
The Role annotation uses the Roles annotation when used more than one time
on a specification. The Roles annotation simply generates an array of Role annota-
tions. The code for Roles is as follows:
public @interface Roles {
Role[] value();
}
Now, if you want to assign two different roles to a particular method, you can use
the following syntax:
@Role(name="AUTHOR")
@Role(name="REVIEWER")
public void assignWork(){
Search WWH ::




Custom Search