Accessing type annotations via annotation processor

Alex Buckley alex.buckley at
Mon Aug 11 18:13:31 UTC 2014

There are some known problems with retrieving type annotations on type 
parameter declarations, in both the Core Reflection API and the Language 
Model API. I believe type annotations on types used in member 
declarations work OK. If you have the VariableElement for 'values', you 
can get the TypeMirror for 'List<@NotNull String>', then visit it as a 
DeclaredType and call getTypeArguments() to get a TypeMirror for 
'@NotNull String'. TypeMirror is an AnnotatedConstruct so it can return 
the annotations on the use of 'String'.


On 8/11/2014 3:22 AM, Gunnar Morling wrote:
> Hi,
> I'm looking for a way to obtain annotations given on type uses using a
> JSR 269 annotation processor.
> E.g. I'd like to obtain the @NotNull annotation from the following field
> declaration:
>      ...
>      private List<@NotNull String> values;
> Based on a post of Joe [1] I assumed this to be possible, but a
> discussion on this list now makes me believe this cannot be done as of
> Java 8. Is this true? If so, will this be the case in SE 9?
> On a tangent, Joe referenced a diff of the JavaDocs of two versions of
> the JSR 269 API, containing nicely colored mark-up highlighting the
> changes. What's the tool used for creating this diff?
> Thanks,
> --Gunnar
> [1]
> [2]

More information about the compiler-dev mailing list