[JSR308] annotations on types of the local variables

Eugene Kuleshov eu at javatx.org
Fri May 18 19:38:43 EDT 2007


  I'd like to raise one more discussion topic and collect motivated 
opinions on that.

  Section 3.2 of the current proposal [1] is saying the following:
>
> There is no need for new syntax for annotations on types of local 
> variables (or other variable declarations), because Java syntax 
> already permits an annotation there, as in
>
>     @NonNegative int balance;
>
> There is no ambiguity regarding whether the annotation applies to the 
> variable being declared or to its type, because the 
> @Target(ElementType.TYPE) meta-annotation on the NonNegative 
> annotation declaration indicates where @NonNegative may appear (i.e., 
> @NonNegative is an annotation on types, not on variables). As another 
> example, in
>
>     @Deprecated @NonNull Dimension getSize() { ... }
>   
>
> @Deprecated applies to the method (because Deprecated is 
> meta-annotated with ElementType.METHOD) and @NonNull applies to the 
> return type (because NonNull is meta-annotated with ElementType.TYPE); 
> again, there is no ambiguity for people or annotation processors. This 
> design assumes that there are few annotations that can apply equally 
> well to both methods and return value types, and that have different 
> semantics in the two situations. If such annotations are common, then 
> a different syntax may be warranted. We have not yet discovered even 
> one such annotation.
>
  Several members of the Java compiler team suggested that it is a bad 
idea to use @Target meta-annotation to guard against ambiguity of those 
annotations. I guess the primary concern is that grammar scanner can't 
tell what type of annotation it is and it will also require an extra 
steps in the AST processor. So, we should at least include this into the 
list of unresolved issues.

  I think that "postfix" syntax that Neal proposed don't have this 
issue, and neither syntax that uses generics-like tokens.

  regards,
  Eugene

[1] http://pag.csail.mit.edu/jsr308/java-annotation-design.html#htoc6





More information about the JSR308 mailing list