Accessing type annotations via annotation processor

Joe Darcy joe.darcy at oracle.com
Thu Aug 14 17:25:29 UTC 2014


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 
> <mailto: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."

HTH,

-Joe

>
> 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/20140814/9a1f8588/attachment-0001.html>


More information about the compiler-dev mailing list