TYPE_USE target and repeatable annotations.

Alex Buckley alex.buckley at oracle.com
Mon Nov 11 11:10:04 PST 2013


On 11/11/2013 1:02 AM, Srikanth S Adayapalam wrote:
>> From: Srikanth S Adayapalam/India/IBM
>> Subject: TYPE_USE target and repeatable annotations.
>
>> The following program fails to compile with both javac and ECJ:
>>
>> // --
>> import java.lang.annotation.ElementType;
>> import java.lang.annotation.Repeatable;
>> import java.lang.annotation.Target;
>>
>> @Target({ElementType.TYPE_USE})
>> @Repeatable(FooContainer.class)
>> @interface Foo {}
>> @Target({ElementType.TYPE, ElementType.TYPE_USE})
>> @interface FooContainer {
>>   Foo[] value();
>> }
>
> [...]
>
>>  From the spec:
>>
>> If Foo has an @Target meta-annotation, then in the judgment of the
>> designers of the
>> Java programming language, FooContainer must be declared with knowledge
> of the
>> Foo's applicability. Specifically, the kind of program element where
>> FooContainer
>> may appear must be the same as, or a subset of, Foo's kinds.
>>
>> Given TYPE_USE annotations can appear on type declarations, the
>> last sentence is certainly valid for the program above.
>
> Not quite, just after pushing the send button, I realized that
> FooContainer by virtue of its TYPE target, can appear on annotation
> types, while @Foo cannot. So there is no contradiction here.

There is some confusion here. @Foo can appear on the declaration of an 
annotation type just like @FooContainer. The Foo type has a TYPE_USE 
target, so @Foo can appear in at least the places permitted by a TYPE 
target, which includes declarations of annotation types. This policy has 
always been in section 2.3 of java-annotation-design.pdf, and is 
formalized in the 308 Public Review spec as:

"It is a compile-time error if an annotation of type T is syntactically 
a modifier for: ... an annotation type declaration, but T is not 
applicable to annotation type declarations or type declarations or type 
contexts."

Now to the program above. The targets of Foo and FooContainer are such 
that logically, @Foo can appear in exactly the same places as 
@FooContainer. Therefore, the rules for repeatable annotation types 
should not require an error. I think that was your initial suspicion: 
that javac and ecj _should_ compile it.

However, the target set for FooContainer has more elements that the 
target set for Foo, so by the rules of JLS 9.6.3 an error is due:

"An annotation type TC is a containing annotation type of T if all of 
the following are true:
...
- 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 m1, and the kinds of program element 
where TC is applicable are denoted by the set m2, then each kind in m2 
must occur in m1 unless the kind in m2 is 
java.lang.annotation.ElementType.ANNOTATION_TYPE, in which case at least 
one of java.lang.annotation.ElementType.ANNOTATION_TYPE or 
java.lang.annotation.ElementType.TYPE must occur in m1."

To properly integrate this rule with JSR 308, the Proposed Final Draft 
of JLS8 should say:

"... then each kind in m2 must occur in m1, unless a) the kind in m2 is 
ANNOTATION_TYPE, in which case at least one of ANNOTATION_TYPE or TYPE 
or TYPE_USE must occur in m1, and b) the kind in m2 is TYPE, in which 
case at least one of TYPE or TYPE_USE must occur in m1."

Then the program will compile fine. I will file a bug against javac.

> Note however that 8b112 does not complain on this program, I believe
> it should, given that annotation type uses cannot be type annotated,
> the short hand of annotating the declaration should also be invalid.
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Target;
>
> @Target({ElementType.TYPE_USE})
> @interface Foo {}
>
> @Foo
> @interface Marker {
>
> }

Again, a TYPE_USE annotation has always been allowed by JSR 308 wherever 
a TYPE annotation is allowed, which includes declarations of annotation 
types. So there is nothing wrong with this program.

Alex


More information about the type-annotations-spec-experts mailing list