How to access elements outside an Annotation Processor
Jonathan Gibbons
jonathan.gibbons at oracle.com
Sat Jul 8 19:56:14 UTC 2017
If you want to do work before annotation processing, you can use a
combination of the
Compiler Tree API (com.sun.source.*). As well as proving access to
the AST, it also provides API to supply TaskListeners to be invoked at
different points in the compilation pipeline. There is also a command
line option that allows you to specify a "plugin" which will be invoked
after option decoding, and which can register task listeners to be
called later on.
Language Model API (javax.lang.model.*). But note, while you can
walk the AST as soon as its available, you won't be able to access the
Elements to which names refer until later in the compilation pipeline.
And, for annotations, that is round about the time that annotation
processing occurs.
-- Jon
On 7/8/17 11:00 AM, Stéphane NICOLAS wrote:
> Hi there,
>
> my last question about elements (de)serialization didn't really catch
> your attention, but I try again !
> :)
>
> I would like to have the advises of people on this list to access
> elements outside an AP.
>
> Context: I am developing a build tool and would need, given a list of
> files, to access all elements annotated with a given set of annotations.
>
> I read the javac's compiler code and found out that javac does it via
> the JavaProcessingEnvironment, that create a RoundEnvironment that in
> turn uses a TypeUtil instance and a visitor to scan all elements and
> return those which are annotated.
>
> But the internals of javac are a bit complex, is there a simpler way
> to do this ? What would you recommend ?
> Please note that I want to do this BEFORE annotation processing
> itself, hence I can't use an Annotation Processor to achieve it.
>
> Thanks in advance and thanks for this amazing compiler !
>
> S.
>
More information about the compiler-dev
mailing list