Rethinking TypeAnnotation vs Annotation

Alex Buckley alex.buckley at
Thu Feb 6 12:15:03 PST 2014

Hi Eric,

"Any given annotation is potentially a type annotation" doesn't smell 
right to me. For example, an annotation on a package declaration cannot 
reasonably be considered as a "type annotation".

In fact, we can be real precise about this: there are eight declaration 
contexts defined in JLS, and only five of them are called out in 
9.7.4 as being a syntactic opportunity to annotate a type. The three 
that aren't - package declarations, type declarations, and type 
parameter declarations - are where you "cannot" have a type annotation, 
by definition.

It may be helpful to review 9.7.4:

- A declaration annotation is an annotation that applies to a 
declaration, and whose own type is applicable in the declaration context 
(§ represented by that declaration.

- A type annotation is an annotation that applies to a type (or any part 
of a type), and whose own type is applicable in type contexts (§4.11).

- Whether an annotation applies to a declaration or to the type of the 
declared entity - and thus, whether the annotation is a declaration 
annotation or a type annotation - depends on the applicability of the 
annotation's type: ...


On 2/6/2014 11:59 AM, Eric McCorkle wrote:
> In the process of working on the next roadmap step for type annotations,
> I've realized something.
> The current implementation, and I think most of everyone's thinking on
> annotations treats type annotations as a special case of regular
> annotations.  Indeed, this is sort of the intuitive first thought.
> But looking at the actual implementation details, I think the reverse is
> actually true.  Any given annotation is potentially a type annotation,
> especially earlier in the javac pipeline (where I'm presently generating
> TypeAnnotationPositions in my new patch).  So, on the implementation
> front, it makes perfect sense to do things like store
> TypeAnnotationPositions in Attribute.Compound, as opposed to
> Attribute.TypeCompound.
> I think this would also go a long way towards creating the single
> codepath for attaching annotations that I want to create.
> Comments?

More information about the compiler-dev mailing list