Constraining type annotations to occur just before type
Srikanth S Adayapalam
srikanth_sankaran at in.ibm.com
Wed Mar 20 11:28:27 PDT 2013
Dear EG,
We have a request from the Eclipse JDT team that JSR308 should
constrain/require the position of TYPE_USE annotations in declaration
modifiers, so that all TYPE_USE annotations must be placed at the end
of the modifiers list. (Or in other words, where a type annotation features
in a place a declaration annotation aka a SE7 annotation can also feature,
the language should mandate that they be placed immediately preceding
the type which they are annotating.
Example:
package jsr308.bug;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE_USE)
@interface NonNull { }
public class Bug {
@Deprecated @NonNull String s = new @NonNull String();
@NonNull @Deprecated String s2 = new @NonNull String(); //
disallow
}
The rationale being:
A declaration like "@NonNull @Deprecated String" is hard to understand for
Java programmers. It doesn't make sense to allow the TYPE_USE annotation
to occur anywhere in the modifiers list. Unlike e.g. an ElementType.FIELD
annotation, a TYPE_USE annotation is not a general modifier of the
declaration.
A TYPE_USE annotation always belongs to the following type, so it should
also
be right in front of the type in the source.
The main concern is that from a language point of view, it doesn't make
sense to tear apart the TYPE_USE annotation and the type proper. This
was possible with annotations that abused other ElementTypes to
implement a restricted form of TYPE_USE annotations. But that doesn't
mean it needs to stay this way.
Thanks for considering this,
Srikanth.
More information about the type-annotations-spec-experts
mailing list