How can we provide an annotation processor with a Java 11 module?
To register the annotation provider we need the following module-info entry:
import javax.annotation.processing.Processor;
import com.mycompany.mylib.impl.MyAnnotationProcessor;
module com.mycompany.mylib {
provides Processor with MyAnnotationProcessor;
}
Now, unfortunately, this is not enough since the packages javax.annotation.processing
, javax.lang.model.*
and javax.tools
are not in the java.base
module but in the java.compiler
module.
With Java SE 8 everything was just available in the JRE, but with Java 11 we get the option to use only a subset. With jlink
we then can create smaller runtime images.
Now, of course, I could just add the following to the module-info:
requires java.compiler;
But this would cause java.compiler
to be part of the custom runtime image as well.
But annotation processing is something special: it is code run at compile time, not at runtime. Thus it should not be part of the runtime image. It should only be a compile-time requirement/ dependency.
Is there a way to solve this with the Java 11 module system?
Annotations provide information to a program at compile time or at runtime based on which the program can take further action. An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc.
Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.
The Processor class consists of methods that are invoked by the Java Integration stage. When a job that includes the Java Integration stage starts, the stage instantiates your Processor class and calls the logic within your Processor implementations.
The JDT-APT project provides plugins that add Java 5 annotation processing support to Eclipse. A Java annotation processor is a compiler plug-in that can gather information about source code as it is being compiled, generate additional Java types or other resource files, and post warnings and errors.
It seems that you should write
requires static java.compiler;
It's stated in the JLS 7.7.1 that
The requires keyword may be followed by the modifier
static
. This specifies that the dependence, while mandatory at compile time, is optional at run time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With