Invoking code generated by an annotation processor

Jonathan Gibbons jonathan.gibbons at oracle.com
Wed Feb 10 19:41:04 UTC 2016



On 02/10/2016 09:09 AM, Stéphane NICOLAS wrote:
> I am facing a problem with Javac : I got a sweet annotation processor 
> that generates a DSL and I invoke the generated code from my main code.
>
> Everything works fine except when there is an error in my main code : 
> if I got a compile error in the main code, not related to the 
> generated code, like a missing comma (parsing problem), or calling an 
> inexisting method (linkage problem), then all calls to the annotation 
> processor are marked as errors by the compiler.
>
> Ideally I am just looking for a way to get rid of those error 
> messages, and keep only the original one that fails to compile.
>
> AFAIK, javac should support this scenario but it doesn't support it in 
> a very convenient way and I don't understand exactly why..
>
> After reafing the javac code, open jdk 7 & 8, I realized that :
>
> Annotation processors are not called if there is any error to compile 
> the code. As the annotation processor are not triggered, the calls to 
> generated code get mark as an error.
>
> However, I could, by playing with shouldStopPolicy (+if error and if 
> no error) trigger the annotation processor and generate the code. But 
> still, I would receive in this case the errors related to calling the 
> generated code. Like if the generated code was not taken into account, 
> like if I was receiving errors from a previous compilation steps.
>
> I understand that javac parses the code, if it fails there, I will 
> receive only an error about the actual problem. And this is great. I 
> just want this error to show up, not any other, and it works fine. 
> (Though I am wondering if this is always true even I clean my build & 
> generated files before compiling)
>
> If there is a linkage problem in my original code, like calling an 
> non-existent method, javac will relax the error and trigger the 
> annotation processors and try to let them generate the code to see if 
> the generated code that satisfies the linkage problem. But then, 
> though there is a state in which javac will successfully satisfy all 
> linkage related to generated code but not the other linkage error, I 
> get all calls to generated code marked as errors. This is my problem. 
> It looks like this internal state of the compiler is never accessible 
> and errors returned do not match this state but more probably a 
> previous internal state in which all linkage errors are aggregated and 
> returned.
>
> I think there is a solution to achieve what I want but I couldn't find 
> one. And the situation is bad : it may actually cancel the development 
> of the annotation processor lib I am developing. Just because the 
> errors are flooding the actual problem in the code.
>
> Would you have some time to advise me, and help me to make the lib 
> work as expected.
> My goal is just to get only errors that are not related to calling 
> generated code if they are not those who fail the compilation. I don't 
> want to see my errors polluted by those calls if they are not 
> responsible for breaking the compilation.
>
> Stephane

Stephane,

In general, there is no convenient solution for you here, because there 
is no way for javac to predict what unresolved symbols may or may not be 
generated by the annotation processor that has yet to be run.  And in 
general, it is not desirable to run annotation processors if the input 
source code has significant errors, such as syntax errors.

-- Jon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160210/27b6eb69/attachment.html>


More information about the compiler-dev mailing list