An annotation type T is repeatable if its declaration is (meta-)annotated with an @Repeatable annotation (§9.6.4.8) whose value element indicates a containing annotation type of T.
An annotation type TC is a containing annotation type of T if all of the following are true:
-
TC declares a
value()method whose return type is T[]. -
Any methods declared by TC other than
value()have a default value. -
TC is retained for at least as long as T, where retention is expressed explicitly or implicitly with the
@Retentionannotation (§9.6.4.2). Specifically:- If the retention of TC is
java.lang.annotation.RetentionPolicy.SOURCE, then the retention of T isjava.lang.annotation.RetentionPolicy.SOURCE. - If the retention of TC is
java.lang.annotation.RetentionPolicy.CLASS, then the retention of T is eitherjava.lang.annotation.RetentionPolicy.CLASSorjava.lang.annotation.RetentionPolicy.SOURCE. - If the retention of TC is
java.lang.annotation.RetentionPolicy.RUNTIME, then the retention of T isjava.lang.annotation.RetentionPolicy.SOURCE,java.lang.annotation.RetentionPolicy.CLASS, orjava.lang.annotation.RetentionPolicy.RUNTIME.
- If the retention of TC is
-
T is applicable to at least the same kinds of program element as TC (§9.6.4.1). Specifically, if the kinds of program element where T is applicable are denoted by the set
m_1, and the kinds of program element where TC 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 type may be repeatable on only some of the kinds of program element where it is applicable.
- If the kind in
-
If the declaration of T has a (meta-)annotation that corresponds to
java.lang.annotation.Documented, then the declaration of TC must have a (meta-)annotation that corresponds tojava.lang.annotation.Documented.Note that it is permissible for TC to be
@Documentedwhile T is not@Documented. -
If the declaration of T has a (meta-)annotation that corresponds to
java.lang.annotation.Inherited, then the declaration of TC must have a (meta)-annotation that corresponds tojava.lang.annotation.Inherited.Note that it is permissible for TC to be
@Inheritedwhile T is not@Inherited.
It is a compile-time error if an annotation type T is (meta-)annotated with an @Repeatable annotation whose value element indicates a type which is not a containing annotation type of T.
Example 9.6.3-1. Ill-formed Containing Annotation Type
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 type, but FooContainer is not in fact a containing annotation type of Foo. (The return type of FooContainer.value() is not Foo[].)
The @Repeatable annotation cannot be repeated, so only one containing annotation type can be specified by a repeatable annotation type.
Allowing more than one containing annotation type to be specified would cause an undesirable choice at compile time, when multiple annotations of the repeatable annotation type are logically replaced with a container annotation (§9.7.5).
An annotation type can be the containing annotation type of at most one annotation type.
This is implied by the requirement that if the declaration of an annotation type T specifies a containing annotation type of TC, then the value() method of TC has a return type involving T, specifically T[].
An annotation type cannot specify itself as its containing annotation type.
This is implied by the requirement on the value() method of the containing annotation type. Specifically, if an annotation type A specified itself (via @Repeatable) as its containing annotation type, 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 type cannot refer to itself in its elements (§9.6.1). More generally, two annotation types cannot specify each other to be their containing annotation types, because cyclic annotation type declarations are illegal.
An annotation type TC may be the containing annotation type of some annotation type T while also having its own containing annotation type TC '. That is, a containing annotation type may itself be a repeatable annotation type.
Example 9.6.3-2. Restricting Where Annotations May Repeat
An annotation whose type declaration indicates a target of java.lang.annotation.ElementType.TYPE can appear in at least as many locations as an annotation whose type declaration indicates a target of java.lang.annotation.ElementType.ANNOTATION_TYPE. For example, given the following declarations of repeatable and containing annotation types:
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
@Target(ElementType.TYPE)
@Repeatable(FooContainer.class)
@interface Foo {}
@Target(ElementType.ANNOTATION_TYPE)
@interface FooContainer {
Foo[] value();
}
@Foo can appear on any type declaration while @FooContainer can appear on only annotation type declarations. Therefore, the following annotation type 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 type and FooContainer is its containing annotation type, 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 type 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 type 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 type 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 type forFoo. It would therefore be dangerous forFooto rely on it.
Example 9.6.3-3. A Repeatable Containing Annotation Type
The following declarations are legal:
import java.lang.annotation.Repeatable;
// Foo: Repeatable annotation type
@Repeatable(FooContainer.class)
@interface Foo { int value(); }
// FooContainer: Containing annotation type of Foo
// Also a repeatable annotation type itself
@Repeatable(FooContainerContainer.class)
@interface FooContainer { Foo[] value(); }
// FooContainerContainer: Containing annotation type of FooContainer
@interface FooContainerContainer { FooContainer[] value(); }
Thus, an annotation whose type is a containing annotation type may itself be repeated:
@FooContainer({@Foo(1)}) @FooContainer({@Foo(2)})
class Test {}
An annotation type which is both repeatable and containing is subject to the rules on mixing annotations of repeatable annotation type with annotations of containing annotation type (§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 FooContainerContainer type was itself repeatable, then it would be possible to write multiple @Foo annotations alongside multiple @FooContainerContainer annotations.