gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:20:20 25 4
gpt4 key购买 nike

我们如何为注释处理器提供 Java 11 模块?

要注册注解提供者,我们需要以下模块信息条目:

import javax.annotation.processing.Processor;
import com.mycompany.mylib.impl.MyAnnotationProcessor;

module com.mycompany.mylib {

provides Processor with MyAnnotationProcessor;

}

现在,不幸的是,这还不够,因为软件包 javax.annotation.processing , javax.lang.model.*javax.tools不在 java.base模块,但在 java.compiler模块。

在 Java SE 8 中,一切都在 JRE 中可用,但在 Java 11 中,我们可以选择只使用一个子集。与 jlink然后我们可以创建更小的运行时镜像。

现在,当然,我可以将以下内容添加到模块信息中:
requires java.compiler;

但这会导致 java.compiler也成为自定义运行时镜像的一部分。

但是注解处理有些特殊:它是在编译时运行的代码,而不是在运行时。因此,它不应该是运行时镜像的一部分。它应该只是一个编译时要求/依赖项。

有没有办法用 Java 11 模块系统解决这个问题?

最佳答案

看来你应该写

requires static java.compiler;


JLS 7.7.1 中指出

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.

关于Java 11 模块信息和注释处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59673063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com