简体   繁体   English

获取 IntelliJ 以识别 AnnotationProcessor 生成的类

[英]Get IntelliJ to recognize classes generated by AnnotationProcessor

I'm using a java annotation processor to generates additional classes at compile time.我正在使用 java 注释处理器在编译时生成其他类。 It works fine when building with gradle, but I can't get IntelliJ to recognize the generated classes.使用 gradle 构建时它工作正常,但我无法让 IntelliJ 识别生成的类。 Whenever I try to build the project in IntelliJ, it errors saying that it can't find the symbols that refer to the generated class.每当我尝试在 IntelliJ 中构建项目时,它都会出错,说它找不到引用生成的类的符号。 In the same vein, since it doesn't know about the classes, it's not giving me any help in writing code that uses the classes, and just highlights it all as an error.同样,由于它不知道这些类,因此在编写使用这些类的代码时并没有给我任何帮助,只是将其全部突出显示为错误。

I have two sibling modules: the "processor" module implements the annotation processor and defines the annotations.我有两个兄弟模块:“处理器”模块实现注释处理器并定义注释。 The "demo" module is just some JUnit tests to try out the annotation processor. “演示”模块只是一些用于试用注释处理器的 JUnit 测试。 I can build the "processor" module in IntelliJ fine, but the "demo" one gives me the errors as described above.我可以在 IntelliJ 中很好地构建“处理器”模块,但是“演示”模块给了我如上所述的错误。 I've got "processor" as a dependency of the "demo" module, at the "Test" scope (I've also tried "Compile" scope).我在“测试”范围内(我也尝试过“编译”范围)将“处理器”作为“演示”模块的依赖项。

How can I get IntelliJ to recognize the classes automatically?如何让 IntelliJ 自动识别类?

Update更新

I've been able to get it to build by creating a new profile under "Annotation Processors" in the settings dialog, moving the "demo" module under that profile, enabling annotation processing for that profile, and specifying the FQCN of the annotation processor under the "Annotation Processors" list box.我已经能够通过在设置对话框中的“注释处理器”下创建一个新的配置文件来构建它,移动该配置文件下的“演示”模块,启用该配置文件的注释处理,并指定注释处理器的 FQCN在“注释处理器”列表框下。

However, the live code help still isn't working, the editor just tells me it can't find the class, which is really more important (because I could always build from gradle).但是,实时代码帮助仍然不起作用,编辑器只是告诉我它找不到类,这确实更重要(因为我总是可以从 gradle 构建)。

In IntelliJ, find the folder that the generated code goes in. This is controlled by settings on the same dialog as where you set up the annotation processing.在 IntelliJ 中,找到生成代码所在的文件夹。这由设置注释处理的同一对话框中的设置控制。 Right click on that folder, find "Mark Directory As" in the context menu, and choose "Generated Sources Root".右键单击该文件夹,在上下文菜单中找到“将目录标记为”,然后选择“生成的源根目录”。

After doing this, IntelliJ will recognize and handle the generated classes normally.这样做之后,IntelliJ 将正常识别和处理生成的类。 It will also give a compile warning that an output path intersects with a source root, but that's reasonably ignorable.它还会发出编译警告,指出输出路径与源根目录​​相交,但这是可以忽略的。 I haven't been able to find a way to get rid of that warning without also leaving the generated classes unrecognized.我一直无法找到一种方法来消除该警告而不会使生成的类无法识别。

You can go on pom/gradle file and Reload it again, it will work.您可以继续使用 pom/gradle 文件并重新加载它,它会起作用。 example :例子 :

pom.xml(right-click)-> Maven-> Reload pom.xml(右键单击)-> Maven-> 重新加载

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

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