An annotation interface A is repeatable if its declaration is (meta-)annotated with an @Repeatable annotation (§9.6.4.8) whose value element indicates a containing annotation interface of A.
An annotation interface AC is a containing annotation interface of A if all of the following are true:
-
AC declares a
value()method whose return type is A[]. -
Any methods declared by AC other than
value()have a default value. -
AC is retained for at least as long as A, where retention is expressed explicitly or implicitly with the
@Retentionannotation (§9.6.4.2). Specifically:- If the retention of AC is
java.lang.annotation.RetentionPolicy.SOURCE, then the retention of A isjava.lang.annotation.RetentionPolicy.SOURCE. - If the retention of AC is
java.lang.annotation.RetentionPolicy.CLASS, then the retention of A is eitherjava.lang.annotation.RetentionPolicy.CLASSorjava.lang.annotation.RetentionPolicy.SOURCE. - If the retention of AC is
java.lang.annotation.RetentionPolicy.RUNTIME, then the retention of A isjava.lang.annotation.RetentionPolicy.SOURCE,java.lang.annotation.RetentionPolicy.CLASS, orjava.lang.annotation.RetentionPolicy.RUNTIME.
- If the retention of AC is
-
A is applicable to at least the same kinds of program element as AC (§9.6.4.1). Specifically, if the kinds of program element where A is applicable are denoted by the set
m_1, and the kinds of program element where AC is applicable are denoted by the setm_2, then each kind inm_2must occur inm_1, except that:- If the kind in
m_2isjava.lang.annotation.ElementType.ANNOTATION_TYPE, then at least one ofjava.lang.annotation.ElementType.ANNOTATION_TYPEorjava.lang.annotation.ElementType.TYPEorjava.lang.annotation.ElementType.TYPE_USEmust occur inm_1. - If the kind in
m_2isjava.lang.annotation.ElementType.TYPE, then at least one ofjava.lang.annotation.ElementType.TYPEorjava.lang.annotation.ElementType.TYPE_USEmust occur inm_1. - If the kind in
m_2isjava.lang.annotation.ElementType.TYPE_PARAMETER, then at least one ofjava.lang.annotation.ElementType.TYPE_PARAMETERorjava.lang.annotation.ElementType.TYPE_USEmust occur inm_1.
This clause implements the policy that an annotation interface may be repeatable on only some of the kinds of program element where it is applicable.
- If the kind in
-
If the declaration of A has a (meta-)annotation that corresponds to
java.lang.annotation.Documented, then the declaration of AC must have a (meta-)annotation that corresponds tojava.lang.annotation.Documented.Note that it is permissible for AC to be
@Documentedwhile A is not@Documented. -
If the declaration of A has a (meta-)annotation that corresponds to
java.lang.annotation.Inherited, then the declaration of AC must have a (meta)-annotation that corresponds tojava.lang.annotation.Inherited.Note that it is permissible for AC to be
@Inheritedwhile A is not@Inherited.
It is a compile-time error if an annotation interface A is (meta-)annotated with an @Repeatable annotation whose value element indicates a type which is not a containing annotation interface of A.
Example 9.6.3-1. Ill-formed Containing Annotation Interface
Consider the following declarations:
import java.lang.annotation.Repeatable;
@Repeatable(FooContainer.class)
@interface Foo {}
@interface FooContainer { Object[] value(); }
Compiling the Foo declaration produces a compile-time error because Foo uses @Repeatable to attempt to specify FooContainer as its containing annotation interface, but FooContainer is not in fact a containing annotation interface of Foo. (The return type of FooContainer.value() is not Foo[].)
The @Repeatable annotation cannot be repeated, so only one containing annotation interface can be specified by a repeatable annotation interface.
Allowing more than one containing annotation interface to be specified would cause an undesirable choice at compile time, when multiple annotations of the repeatable annotation interface are logically replaced with a container annotation (§9.7.5).
An annotation interface can be the containing annotation interface of at most one annotation interface.
This is implied by the requirement that if the declaration of an annotation interface A specifies a containing annotation interface of AC, then the value() method of AC has a return type involving A, specifically A[].
An annotation interface cannot specify itself as its containing annotation interface.
This is implied by the requirement on the value() method of the containing annotation interface. Specifically, if an annotation interface A specified itself (via @Repeatable) as its containing annotation interface, then the return type of A's value() method would have to be A[]; but this would cause a compile-time error since an annotation interface cannot refer to itself in its elements (§9.6.1). More generally, two annotation interfaces cannot specify each other as their containing annotation interface, because cyclic annotation interface declarations are illegal.
An annotation interface AC may be the containing annotation interface of some annotation interface A while also having its own containing annotation interface SC. That is, a containing annotation interface may itself be a repeatable annotation interface.
Example 9.6.3-2. Restricting Where Annotations May Repeat
An annotation whose interface declaration indicates a target of java.lang.annotation.ElementType.TYPE can appear in at least as many locations as an annotation whose interface declaration indicates a target of java.lang.annotation.ElementType.ANNOTATION_TYPE. For example, given the following declarations of repeatable and containing annotation interfaces:
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Repeatable(FooContainer.class)
@interface Foo {}
@Target(ElementType.ANNOTATION_TYPE)
@interface FooContainer {
Foo[] value();
}
@Foo can appear on any class or interface declaration while @FooContainer can appear on only annotation interface declarations. Therefore, the following annotation interface declaration is legal:
@Foo @Foo
@interface Anno {}
while the following interface declaration is illegal:
@Foo @Foo
interface Intf {}
More broadly, if Foo is a repeatable annotation interface and FooContainer is its containing annotation interface, then:
-
If
Foohas no@Targetmeta-annotation andFooContainerhas no@Targetmeta-annotation, then@Foomay be repeated on any program element which supports annotations. -
If
Foohas no@Targetmeta-annotation butFooContainerhas an@Targetmeta-annotation, then@Foomay only be repeated on program elements where@FooContainermay appear. -
If
Foohas an@Targetmeta-annotation, then in the judgment of the designers of the Java programming language,FooContainermust be declared with knowledge of theFoo's applicability. Specifically, the kinds of program element whereFooContainermay appear must logically be the same as, or a subset of,Foo's kinds.For example, if
Foois applicable to field and method declarations, thenFooContainermay legitimately serve asFoo's containing annotation interface ifFooContaineris applicable to just field declarations (preventing@Foofrom being repeated on method declarations). But ifFooContaineris applicable only to formal parameter declarations, thenFooContainerwas a poor choice of containing annotation interface byFoobecause@FooContainercannot be implicitly declared on some program elements where@Foois repeated.Similarly, if
Foois applicable to field and method declarations, thenFooContainercannot legitimately serve asFoo's containing annotation interface ifFooContaineris applicable to field and parameter declarations. While it would be possible to take the intersection of the program elements and makeFoorepeatable on field declarations only, the presence of additional program elements forFooContainerindicates thatFooContainerwas not designed as a containing annotation interface forFoo. It would therefore be dangerous forFooto rely on it.
Example 9.6.3-3. A Repeatable Containing Annotation Interface
The following declarations are legal:
import java.lang.annotation.Repeatable;
// Foo: Repeatable annotation interface
@Repeatable(FooContainer.class)
@interface Foo { int value(); }
// FooContainer: Containing annotation interface of Foo
// Also a repeatable annotation interface itself
@Repeatable(FooContainerContainer.class)
@interface FooContainer { Foo[] value(); }
// FooContainerContainer: Containing annotation interface
// of FooContainer
@interface FooContainerContainer { FooContainer[] value(); }
Thus, an annotation whose interface is a containing annotation interface may itself be repeated:
@FooContainer({@Foo(1)}) @FooContainer({@Foo(2)})
class Test {}
An annotation interface which is both repeatable and containing is subject to the rules on mixing annotations of repeatable annotation interface with annotations of containing annotation interface (§9.7.5). For example, it is not possible to write multiple @Foo annotations alongside multiple @FooContainer annotations, nor is it possible to write multiple @FooContainer annotations alongside multiple @FooContainerContainer annotations. However, if the annotation interface FooContainerContainer was itself repeatable, then it would be possible to write multiple @Foo annotations alongside multiple @FooContainerContainer annotations.