了解 Java 注释处理上下文中的增量环境
我对 Java 注释处理上下文中的增量环境到底是什么感到困惑。高度赞赏一个例子。
以下是 Filer 的 Javadoc 的引用:
文件创建方法采用可变数量的参数来允许 作为工具提示提供的原始元素 更好地管理依赖关系的基础设施。起源元素 是类型或包(代表包信息文件) 导致注释处理器尝试创建新文件。为了 例如,如果注释处理器尝试创建源文件, generatedFromUserSource,响应处理
@Generate 公共类用户源{}
UserSource 的类型元素应作为 创建方法调用如下:
filer.createSourceFile("GenerateFromUserSource", eltUtils.getTypeElement("UserSource"));
如果没有原始元素,则不需要传递任何元素。 这个 信息可用于增量环境中以确定 需要重新运行处理器或删除生成的文件。非增量式 环境可能会忽略原始元素信息。
我理解,例如,在上面的示例中,如果删除 @Generate
,则下次运行编译时将使用先前生成的 GenerateFromUserSource< /code> 将被删除。 (据说,显然,一些实现,例如
com.sun.tools.javac .processing.JavacFiler 完全忽略原始元素。)这是否会使其增量?我没有想到任何需要重新运行处理器的示例。
为了让事情变得更加混乱,我偶然发现了这个 问题,显然,存在三种类型的处理器:
- 动态、2.隔离、3.聚合
和增量处理器需要在 META-INF 中与非增量处理器不同的位置注册。我目前正在使用 @AutoService
,我认为不支持本次报名。
由于(大部分)Filer 中不允许覆盖,因此我希望能够澄清是什么使处理器增量。
I am confused about what exactly is incremental environment in the context of annotation processing in Java. An example is highly appreciated.
The following is a quote from the Javadoc of Filer:
The file creation methods take a variable number of arguments to allow
the originating elements to be provided as hints to the tool
infrastructure to better manage dependencies. The originating elements
are the types or packages (representing package-info files) which
caused an annotation processor to attempt to create a new file. For
example, if an annotation processor tries to create a source file,
GeneratedFromUserSource, in response to processing@Generate public class UserSource {}
the type element for UserSource should be passed as part of the
creation method call as in:filer.createSourceFile("GeneratedFromUserSource", eltUtils.getTypeElement("UserSource"));
If there are no originating elements, none need to be passed. This
information may be used in an incremental environment to determine the
need to rerun processors or remove generated files. Non-incremental
environments may ignore the originating element information.
I understand, for example, in the above example, if @Generated
is removed, the next time compilation is run the previously generated GeneratedFromUserSource
will be removed. (Supposedly, apparently, some implementations like com.sun.tools.javac.processing.JavacFiler
ignore originating elements altogether.) Does this make it incremental? I do not have any example in mind for a need to rerun the processor.
To make matters a bit more confusing, I have stumbled upon this question and apparently, there are three types of processors:
- dynamic, 2. isolating, 3. aggregation
and incremental processors need to be registered in a different place in META-INF than non-incremental ones. I am currently using @AutoService
which I think does not support this registration.
Since (mostly) overwriting is not allowed in Filer
, I would appreciate a clarification on what makes a processor incremental.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论