简体   繁体   English

Java 11 模块信息和注释处理器

[英]Java 11 module-info and annotation processors

How can we provide an annotation processor with a Java 11 module?我们如何提供带有 Java 11 模块的注释处理器?

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.现在,不幸的是,这还不够,因为包javax.annotation.processingjavax.lang.model.*javax.tools不在java.base模块中,而是在java.compiler模块中。

With Java SE 8 everything was just available in the JRE, but with Java 11 we get the option to use only a subset.在 Java SE 8 中,一切都只在 JRE 中可用,但在 Java 11 中,我们可以选择只使用一个子集。 With jlink we then can create smaller runtime images.使用jlink我们可以创建更小的运行时映像。

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.但这也会导致java.compiler成为自定义运行时映像的一部分。

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?有没有办法用 Java 11 模块系统解决这个问题?

It seems that you should write看来你应该写

requires static java.compiler;需要静态 java.compiler;

It's stated in the JLS 7.7.1 that JLS 7.7.1 中指出

The requires keyword may be followed by the modifier static . requires 关键字后面可以跟修饰符static This specifies that the dependence, while mandatory at compile time, is optional at run time.这指定了依赖,虽然在编译时是强制性的,但在运行时是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM