简体   繁体   English

如何在 Eclipse 中配置 Java 注释处理器?

[英]How to configure Java Annotation Processor(s) in Eclipse?

I have successfully run annotation processor for CLASS level retention annotations via command prompt compiling using Java 8.我已经通过使用 Java 8 的命令提示符编译成功地运行了 CLASS 级别保留注释的注释处理器。

However, when I tried to configure the annotation processor in eclipse and tried to run it with "-proc:only" option, it didn't get effective.但是,当我尝试在 eclipse 中配置注释处理器并尝试使用"-proc:only"选项运行它时,它没有生效。

I have included the Jar file containing the custom annotation processor class file into the Project Properties -> Annotation Processing -> Factory Path .我已将包含自定义注释处理器类文件的 Jar 文件包含到Project Properties -> Annotation Processing -> Factory Path I have also provided the -proc:only option in Project Properties -> Annotation Processing -> Processor Options , still the annotation processor isn't getting called when a class containing my annotation is executed.我还在Project Properties -> Annotation Processing -> Processor Options提供了-proc:only选项,当包含我的注释的类被执行时,注释处理器仍然没有被调用。

Please help me identifying the required setting or mistake or additional step for running the annotation processor via eclipse.请帮助我确定通过 eclipse 运行注释处理器所需的设置或错误或附加步骤。

I was finally able to enable my own annotation processor by adding my jar file.我终于能够通过添加我的 jar 文件来启用我自己的注释处理器。

  1. Right click on the project and select Properties.右键单击项目并选择属性。
  2. Open Java Compiler -> Annotation Processing.打开 Java Compiler -> Annotation Processing。 Check "Enable annotation processing".选中“启用注释处理”。
  3. Open Java Compiler -> Annotation Processing -> Factory Path.打开Java Compiler -> Annotation Processing -> Factory Path。 Check "Enable project specific settings".选中“启用项目特定设置”。 Add your JAR file to the list.将您的 JAR 文件添加到列表中。
  4. Clean and build the project.清理并构建项目。

And it finally worked.它终于奏效了。

It is very much straightforward provided that whatever APT plug in you are trying to use is correctly configured.只要您尝试使用的任何 APT 插件都已正确配置,这将非常简单。

Follow the steps mentioned in above post and the reference image to visualize it.按照上面帖子中提到的步骤和参考图像对其进行可视化。 Image showing QueryDsl APT configuration for maven project.显示 Maven 项目的 QueryDsl APT 配置的图像。

在此处输入图片说明

While compiling via command prompt or terminal, you can see all the logs in same screen after the compilation command.通过命令提示符或终端编译时,您可以在编译命令后在同一屏幕上看到所有日志。 In eclipse, these logs can be seen at在eclipse中,这些日志可以在

Window->show view->General->Error Log窗口->显示视图->常规->错误日志

If you want the IDE(Eclipse) to point out the warning, error or other diagnostic message exactly at the particular element in the code editor, we have to call the printMessage method of javax.annotation.processing.Messager with 3 arguments as shown below.如果您希望 IDE(Eclipse) 在代码编辑器中的特定元素准确指出警告、错误或其他诊断消息,我们必须使用3 个参数调用javax.annotation.processing.MessagerprintMessage方法,如下所示.

messager.printMessage(Kind.ERROR, "Error Message", element); messager.printMessage(Kind.ERROR, "错误信息", element);

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

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