Constraining type annotations to occur just before type

Alex Buckley alex.buckley at
Wed Mar 20 11:53:32 PDT 2013

To be precise, the possibility of mixed TYPE_USE and non-TYPE_USE 
annotations occurs only where a production has been changed to use 
UnannType (or UnannReferenceType) because the production is preceded by 
a Modifier list (or VariableModifier list).

Productions which continue to use Type (or ReferenceType) obviously 
allow only TYPE_USE annotations in the new [Annotations] non-terminal of 

I agree that a TYPE_USE annotation is not a general modifier of a 
declaration. Semantically, it belongs with the type. The question is, 
should placing it among the declaration modifiers be considered a 
semantic error? I think the answer is no, because nothing bad happens at 
run time if you order modifiers unconventionally:

   final @NonNull static private String s;

A bigger problem as I see it is the longstanding JLS advice to write 
annotations as the first modifiers, before 'public', 'static', etc. This 
advice is definitely out of step with type annotations, because placing 
a TYPE_USE annotation at the front is obtuse:

   @NonNull private static final String s;

The advice (not the Modifier production) should change to give an 
exception for TYPE_USE annotations, recommending they be placed last:

   private final @NonNull String s;

I think this solves the problem of mixing TYPE_USE and non-TYPE_USE 
annotations, because the advice would imply:

   @Deprecated private final @NonNull String s;

Mike, Doug, any comments?


On 3/20/2013 11:28 AM, Srikanth S Adayapalam wrote:
> 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