4

I'm working on a multi module maven based project in which one of the modules contains a few annotation processors for the custom annotations used by other modules. When I add a dependency of annotation processor module to any other module, the annotations of that modules are processed by those annotation processors.

But recently I integrated Checker Framework (for type annotations) and then all the custom annotation processors (I mentioned above) stopped working. Any idea on how to get them to work even with Checker Framework is greatly appreciated?


To clear the scenario,

Let's say I have a maven module named module_A. In this module I have a annotation (class level) called "@FoodItem". I need to enforce a rule that any class annotated with "@FoodItem" annotation should implement the interface "Food". So I wrote an annotation processor "FoodItemAnnotationProcessor" in the same module (module_A) which processes such classes and check for the compliance with that rule.

Then let's say I have another module named module_B which has a maven dependency to the module_A. In this module I have a class called "Pizza" which is annotated with "@FoodItem" annotation.

If a build the project (which has module_A and module_B) with the above configuration, the "FoodItemAnnotationProcessor" is executed at compile stage and validates the class "Pizza" for the rule mentioned above.

After that I integrated Checker framework to module_B (as mentioned here). Then checker framework related validations are executed at compile time as expected, but the "FoodItemAnnotationProcessor" ceased to work.

4

1 回答 1

1

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

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

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

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

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

这将显式设置--processorjavac 的参数(请参阅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 自动发现

“自动发现”使 javac 编译器始终运行检查器插件,即使您没有显式传递 -processor 命令行选项。这可以使您的命令行更短,并确保即使您忘记了命令行选项也会检查您的代码。

要启用自动发现,请将名为 META-INF/services/javax.annotation.processing.Processor 的配置文件放在您的类路径中。该文件包含要使用的检查器插件的名称,每行列出一个。例如,要自动运行 Nullness Checker 和 Interning Checker,配置文件应包含:

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

于 2017-09-04T08:23:31.900 回答