gpt4 book ai didi

java - 将自定义注释处理器与 Checker Framework 一起使用

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:52 27 4
gpt4 key购买 nike

我正在开发一个基于多模块 Maven 的项目,其中一个模块包含一些注释处理器,用于其他模块使用的自定义注释。当我将注释处理器模块的依赖项添加到任何其他模块时,该模块的注释将由这些注释处理器处理。

但最近我整合了Checker Framework (用于类型注释)然后所有自定义注释处理器(我上面提到的)停止工作。非常感谢关于如何让它们甚至使用 Checker Framework 工作的想法?


清除场景,

假设我有一个名为 module_A 的 Maven 模块。在这个模块中,我有一个名为“@FoodItem”的注释(类级别)。我需要强制执行一条规则,任何用“@FoodItem”注释注释的类都应该实现接口(interface)“Food”。因此,我在同一个模块 (module_A) 中编写了一个注解处理器“FoodItemAnnotationProcessor”,它处理这些类并检查是否符合该规则。

然后假设我有另一个名为 module_B 的模块,它对 module_A 具有 Maven 依赖性。在这个模块中,我有一个名为“Pizza”的类,它用“@FoodItem”注释进行注释。

如果使用上述配置构建项目(包含 module_A 和 module_B),“FoodItemAnnotationProcessor”将在编译阶段执行,并根据上述规则验证类“Pizza”。

之后,我将 Checker 框架集成到 module_B(如前所述 here )。然后在编译时按预期执行与检查器框架相关的验证,但“FoodItemAnnotationProcessor”停止工作。

最佳答案

要理解这个问题,您必须知道如何javac找到您的注解处理器。

如果您没有为 javac 提供 --processor 参数(请参阅 doc-javac-options ),则会激活注释处理器自动发现功能(请参阅 javac-doc: Annotation processing )。这意味着,javac 将在您的 classpath(或 processorpath,如果您已指定)中搜索所有可用的注释处理器。
包含 META-INF/services/javax.annotation.processing.Processor 文件的 Jar 可以指定它们的注解处理器类,javac 将自动使用它们。

“问题”是检查器框架有多个用于检查的注释处理器,但您可能只想使用其中的一些:因此无法使用注释发现过程,您必须手动指定所有注释处理器在您的构建文件中运行。

对于 Maven 构建,您可以这样做:checker-framework doc for Maven

<annotationProcessors>
<!-- Add all the checkers you want to enable here -->
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
</annotationProcessors>

这将为 javac 显式设置 --processor 参数(参见 doc-javac-options ),这会禁用默认的注释发现过程。

所以解决方案是手动添加所有要运行的注释处理器(除了检查器框架检查器)。

例如当你想运行 NullnessCheckerDagger ,您必须同时指定:

<annotationProcessors>
<!-- Add all the checkers you want to enable here -->
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
<!-- Add all your other annotation processors here -->
<annotationProcessor>dagger.internal.codegen.ComponentProcessor</annotationProcessor>
</annotationProcessors>

提示:
要找出您当前使用的注释处理器,请运行您的构建并将非标准选项 -XprintProcessorInfo 传递给 javac。

更新:

检查器还支持某种自动发现 ( doc-ref ) - 注意:我还没有使用过。

2.2.3 Checker auto-discovery

“Auto-discovery” makes the javac compiler always run a checker plugin, even if you do not explicitly pass the -processor command-line option. This can make your command line shorter, and ensures that your code is checked even if you forget the command-line option.

To enable auto-discovery, place a configuration file named META-INF/services/javax.annotation.processing.Processor in your classpath. The file contains the names of the checker plugins to be used, listed one per line. For instance, to run the Nullness Checker and the Interning Checker automatically, the configuration file should contain:

org.checkerframework.checker.nullness.NullnessChecker
org.checkerframework.checker.interning.InterningChecker

关于java - 将自定义注释处理器与 Checker Framework 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38347443/

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