@Generated requires java.compiler / what should my annotation processor do

Alan Bateman Alan.Bateman at oracle.com
Mon Nov 2 10:01:55 UTC 2020


On 02/11/2020 00:02, Rob Bygrave wrote:
> *>  Since the type javax.annotation.processing.Generated has source *
>
>
> * retention only, there are no @javax.annotation.processing.Generated
> annotations in any class files compiled from source code emitted by your
> processor*
>
>
> The annotation processor generates *source code*. It does not generate byte
> code. In this source generation case it does not seem to matter that the
> annotation is retention source - it will not compile but result in
> compilation errors [package javax.annotation.processing is not visible].
>
> That is, with tooling of maven (and intellij) both do not compile the
> generated source that includes javax.annotation.processing.Generated
> without java.compiler being added to the *project module path*. It is not
> sufficient that requires java.compiler; is in the module-info of the
> annotation processor.
>
> When the annotation processor generates java source code that includes
> javax.annotation.processing.Generated and when this generated source is
> compiled it fails to compile with compilation errors like:.
>
>
> [ERROR] /<my generated source file>.java:[4,24] package
> javax.annotation.processing is not visible
> [ERROR]   (package javax.annotation.processing is declared in module
> java.compiler, but module example does not read it)
>
>
> I believe the generated source is compiled along with the rest of the
> project source code using the *projects module path* which should not
> include the java.compiler module.
>
> To me this compile error is correct and reasonable.  I think it's telling
> me that the generated source should only include things that are in the
> project module path (and java.compiler is not in the project module path).
If I read your mails correctly then there are three invocations of javac 
here.

The first is to compile the annotation processor that is module 
io.avaje.inject.generator. Its module declaration `requires 
java.compiler` and `provides javax.annotation.processing.Processor with 
...`, all good.

The second compilation is done with module io.avaje.inject.generator on 
the processor module path (--processor-module-path). This generates a 
source file with @javax.annotation.processing.Generated in the source 
file. It sounds like this is working.

I think the third compilation is compiling the generated source file. 
Your first mail suggests that it is not compiled into a named module (no 
module-info.java). This should work as all modules that export an API 
are resolved. However the compiler error that you show above looks like 
it is arises when compiling module "example". Is that a surprise? The 
error suggests that the module declaration for module example doesn't 
have `requires static java.compiler` so maybe the issue you are running 
into is in the POM or a tooling issue?

-Alan


More information about the jigsaw-dev mailing list