简体   繁体   English

指定注释处理器的顺序

[英]Specifying order of annotation processors

I'm trying to run Dagger 2 as well as Lombok on my Java project.我正在尝试在我的 Java 项目上运行 Dagger 2 和 Lombok。 Lombok has to run first, of course, but whether it actually does seems to be up to chance.当然,龙目岛必须先运行,但它是否真的运行似乎取决于机会。 At first I suspected I could specify the order by the respective position of the library jars in the class path, but that order evidently gets ignored.起初我怀疑我可以通过类路径中库 jar 的相应位置来指定顺序,但该顺序显然被忽略了。

Is there a way to specify the order for them to run somehow, or do I simply have to live with not being able to combine two APs ?有没有办法指定它们以某种方式运行的顺序,或者我是否只能忍受无法组合两个 AP?

I have produced an SSCCE test case .我已经制作了一个SSCCE 测试用例

A simple git clone & mvn compile is enough to demonstrate the issue - if you comment line 18 and uncomment lines 20-21 in App.java, it will compile, even though the Lombok notation in line 18 creates an identical constructor.一个简单的git clone & mvn compile就足以说明这个问题——如果你在 App.java 中注释第 18 行并取消注释第 20-21 行,它将编译,即使第 18 行中的 Lombok 符号创建了一个相同的构造函数。 The problem is that Lombok seems to run after Dagger.问题是 Lombok 似乎在追赶 Dagger。

After a lot of research and having talked to one of the Lombok developers, it turns out that because javac does class loading based on hashCode(), the order of annotation processors running in this scenario is essentially random, and what's worse, random between multiple runs.经过大量研究并与一位 Lombok 开发人员交谈后,发现由于 javac 基于 hashCode() 进行类加载,因此在这种情况下运行的注解处理器的顺序本质上是随机的,更糟糕​​的是,多个运行。 There currently does not seem to be a solution to this issue.目前似乎没有解决此问题的方法。

I went with the lombok-maven plugin and delomboking the whole thing, which isn't perfect and somewhat hacky, but at least produces a working result.我使用了 lombok-maven 插件并对整个事情进行了 delomboking,这并不完美,而且有点笨拙,但至少产生了一个工作结果。 In hopes that it may aid future googlers coming here, I commited the working version to the repo.希望它可以帮助未来的谷歌员工来到这里,我将工作版本提交给了 repo。

Ideally the order shouldn't matter.理想情况下,顺序应该无关紧要。 Annotation processors should only create files - whenever a file is created, another processing round starts and other processors have the chance again to do their thing with the new file.注释处理器应该只创建文件 - 每当创建一个文件时,就会开始另一轮处理,其他处理器有机会再次对新文件进行处理。 In this case the order doesn't really matter, so I don't think there is an official way to force an order of processors.在这种情况下,顺序并不重要,所以我认为没有官方的方法来强制处理处理器的顺序。 The problem is that the Lombok processor manipulates existing files instead of creating new ones, which it is not supposed to do.问题在于 Lombok 处理器操作现有文件而不是创建新文件,这是它不应该做的。 Some compilers might have an option for ordering processors or use the order in which processors are loaded or appear in the command line arguments, but that will depend on the compiler's implementation.一些编译器可能有一个选项来排序处理器或使用处理器加载或出现在命令行参数中的顺序,但这将取决于编译器的实现。

You could try looking at Daggers and Lombok's build process and see which processors are invoked there.您可以尝试查看 Daggers 和Lombok 的构建过程,看看那里调用了哪些处理器。 Then explicitly set up those processors in your maven build in the correct order and test different compilers and see if any of them run them in this order.然后以正确的顺序在您的 Maven 构建中显式设置这些处理器并测试不同的编译器并查看它们中是否有任何一个按此顺序运行它们。

If necessary, you could split the compilation process and run Lombok with -proc:only first and after that another compilation step without Lombok and without overriding the manipulated files (if that is possible, I never tried that).如有必要,您可以拆分编译过程并首先使用-proc:only运行 Lombok,然后在没有 Lombok 的情况下执行另一个编译步骤并且不覆盖操作的文件(如果可能,我从未尝试过)。

It is possible to specify the order of annotation processors in javac using the -processor flag.可以使用-processor标志在 javac 中指定注释处理器的顺序。 However, I didn't get the compile even with this parameter set.但是,即使设置了这个参数,我也没有得到编译。 I suspect that dagger looks at the source code directly, or that the annotation processor API schedules the annotation processor in the same round and the modifications by lombok are not propagated.我怀疑dagger直接看源码,或者注解处理器API在同一轮调度注解处理器,lombok的修改没有传播。

I think the most robust solution now is to use delombok to force the order.我认为现在最强大的解决方案是使用delombok强制命令。

Disclosure: I am a Lombok developer.披露:我是龙目岛的开发人员。

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

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