Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 11 module-info and annotation processors

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?

like image 473
Puce Avatar asked Jan 09 '20 22:01

Puce


People also ask

What is a Java annotation processor?

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.

How do I run an annotation processor?

Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.

What is processor method in Java?

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.

What is annotation processor in eclipse?

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.


1 Answers

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.

like image 169
Victor Nazarov Avatar answered Oct 03 '22 19:10

Victor Nazarov