Accessing type annotations via annotation processor

Gunnar Morling gunnar at
Thu Aug 14 06:56:55 UTC 2014

Hi Alex,

2014-08-11 20:13 GMT+02:00 Alex Buckley <alex.buckley at>:

> 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'.

I tried what you suggested, but I only obtained a TypeMirror for
'List<String>' from the VariableElement, thus the TypeMirror for the type
argument represents 'String' rather than '@NotNull String'. Is there any
example or test in OpenJDK which shows the retrieval of type annotations
via the Language Model API?

Another issue is that the process() method of the annotation processor is
never invoked for the @NotNull annotation if it is only used as type
annotation (the processor will be invoked though if @NotNull is e.g. used
as a traditional field-level annotation). Is this an expected behavior?



> 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]
>> May/008756.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list