Accessing type annotations via annotation processor

Gunnar Morling gunnar at hibernate.org
Fri Aug 15 10:53:30 UTC 2014


Hi,

2014- 8-14 19:25 GMT+02:00 Joe Darcy <joe.darcy at oracle.com>:

>  Hello,
>
>
> On 08/13/2014 11:56 PM, Gunnar Morling wrote:
>
> Hi Alex,
>
> 2014-08-11 20:13 GMT+02:00 Alex Buckley <alex.buckley at oracle.com>:
>
>> 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?
>
>
> Since the AnnotatedConstruct interface is now implemented by both elements
> and type mirrors, is *should* work go call the annotation retrieval methods
> on a TypeMirror.
>
>
>
>  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?
>
>
> From the revised discovery process documented in
> javax.annotation.processing.Processor:
>
> "Annotations on  type uses, as opposed to annotations on elements, are
> ignored when computing whether or not an annotation type is present."
>

Ah, I see. Thanks for the pointer.

Are there any plans to remove this restriction?

Our annotation processor for Bean Validation aims at detecting constraint
declaration errors such as "List<@Email Integer> email" (applying the
@Email constraint to Integers does not make sense), but as it stands its
not possible to implement such check for type annotations atm.


> HTH,
>
> -Joe
>

Thanks,

--Gunnar


>
>
>
>  Thanks,
>
>  --Gunnar
>
>  Alex
>>
>>
>> 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] https://blogs.oracle.com/darcy/entry/jsr_269_mr_for_java
>>> [2]
>>> http://mail.openjdk.java.net/pipermail/compiler-dev/2014-May/008756.html
>>>
>>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20140815/5e7e9ad7/attachment.html>


More information about the compiler-dev mailing list