7

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?

Puce
  • 37,247
  • 13
  • 80
  • 152

1 Answers1

2

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.

Victor Nazarov
  • 825
  • 8
  • 11