[JSR308] Use of @Target meta-annotation

Michael Ernst mernst at csail.mit.edu
Fri May 18 23:35:37 EDT 2007


Background:  In the syntax currently used by the JSR 308 proposal, a single
source location for an annotation may correspond to multiple targets (say,
to both a method declaration and the return value, or to both a variable
declaration and to the type in the declaration).  There is no ambiguity in
the Java grammar, only in where the annotation should be placed for use by
annotation processors.  The annotation's @Target meta-annotation determines
this, and the compiler automatically places the annotation on the
appropriate target.


Eugene noted two arguments that could be made against this approach.

1. It may be a burden to compiler writers.

The compiler is already required to read and enforce the Target
meta-annotation.  The Target Javadocs state

  If such a meta-annotation is present, the compiler will enforce the
  specified usage restriction.

(See http://java.sun.com/javase/6/docs/api/java/lang/annotation/Target.html.)

Thus, the burden on the compiler writer of applying the annotation to the
correct target is minimal.

2. It may be confusing to programmers.

It's possible that annotations may be so poorly designed, named, and
documented that a competent programmer cannot determine to what they apply.
However, I believe it will be clear in normal usage.

The JSR 308 community has not yet found a single example of an annotation
that has different meaning depending on whether it is applied to one target
or another.  (It's hard to even come up with an example that can be applied
to multiple targets at a location in the current syntax.)

It would be easy for a compiler to issue a warning if an annotation is used
in a location where it applies to two program targets (because is has no
Target meta-annotation, or because it has both Target meta-annotations).


                    -Mike



More information about the JSR308 mailing list