简体   繁体   English

使用Intellij和Maven调试Java注释处理器

[英]Debug Java annotation processors using Intellij and Maven

I'm trying to learn how to make a custom annotation processor and I'm stuck with trying to debug it. 我正在尝试学习如何制作自定义注释处理器,而我却一直试图调试它。

I have already managed to run the javac compiler in debug mode (with mvnDebug clean install ) (with someone else's project with an annotation processor), connect to it with IntelliJ IDEA and have it stop at breakpoints in the annotation processor. 我已经设法在调试模式下运行javac编译器(使用mvnDebug clean install )(与其他人的项目一起使用注释处理器),使用IntelliJ IDEA连接到它并让它停在注释处理器的断点处。


If we have something like this in some package in our project, being just like any other class (eg. no special configuration or anything): 如果我们在项目的某些包中有这样的东西,就像任何其他类一样(例如,没有特殊配置或任何东西):

public class MyProcessor extends AbstractProcessor {...}

Can we somehow hook it into the build process of maven as an annotation processor? 我们能否以某种方式将其作为注释处理器挂钩到maven的构建过程中? So that it is compiled first, then the whole project is compiled with the annotation processor active. 因此首先编译它,然后整个项目在注释处理器处于活动状态时进行编译。

Also, as far as I know, annotation processors require some kind of META INF file, which can be generated with something like google autoservices annotation processor. 另外,据我所知,注释处理器需要某种META INF文件,可以使用google autoservices注释处理器生成。

So maybe a maven build process where we have autoservices run first, then the class extending the AbstractProcessor compiled as an annotation processor and finally have the whole project compile with our own annotation processor active (and with the javac compiler in debug mode). 所以也许是一个maven构建过程,我们首先运行自动服务 ,然后将扩展AbstractProcessor的类作为注释处理器进行编译,最后让整个项目使用我们自己的注释处理器进行编译(并在调试模式下使用javac编译器)。

Here is the recipe. 这是食谱。

Sidenote: I made it really detailed in some cases, skip the parts you already know how to do. 旁注:我在某些情况下非常详细,跳过你已经知道如何做的部分。

  1. First of all, download and install Maven , then download and install IntelliJ IDEA (referred to as IDEA from here on). 首先, 下载并安装Maven ,然后下载并安装IntelliJ IDEA (此处称为IDEA)。 (If you don't know how to use Windows CMD, here is a short tutorial for it, also: how to open the command prompt ) (如果你不知道如何使用Windows CMD,这里有一个简短的教程 ,还有: 如何打开命令提示符

  2. Create a Maven project in IDEA without any Archetype. 在IDEA中创建一个没有任何Archetype的Maven项目。 Then create some some package in src > main > java 然后在src> main> java中创建一些包

  3. Create a Class which extends javax.annotation.processing.AbstractProcessor. 创建一个扩展javax.annotation.processing.AbstractProcessor的类。

  4. Insert some minimal code, just to make it work. 插入一些最小的代码,只是为了使它工作。 (Don't forget the Annotation at the top of the class declaration!) (不要忘记类声明顶部的注释!)

    Assuming that the annotation full path is core.Factory , the code will look like 假设注释完整路径core.Factory ,代码将如下所示

     @SupportedAnnotationTypes("core.Factory") public class MyProcessor extends AbstractProcessor { Messager messager; @Override public void init(ProcessingEnvironment env) { messager = env.getMessager(); super.init(env); } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (TypeElement te : annotations) for (Element e : roundEnv.getElementsAnnotatedWith(te)) messager.printMessage(Diagnostic.Kind.NOTE, "Printing: " + e.toString()); return true; } @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); } } 
  5. Create an annotation in the same package. 在同一个包中创建注释。

     public @interface Factory { } 
  6. In the project there is probably a directory src > test > java , create there another package with the same name as the package you've created earlier. 在项目中可能有一个目录src> test> java ,在那里创建另一个与你之前创建的包同名的包。 Then create a Class in it with a name ending with "Test" (for example: MyProcessorTest). 然后在其中创建一个名为“Test”的类(例如:MyProcessorTest)。 Then annotate this class with the new annotation type you created earlier (@Factory). 然后使用您之前创建的新注释类型(@Factory)注释此类。

     @Factory public class MyProcessorTest { } 
  7. Now, for annotation processors to work, they have to have some file in META-INF. 现在,要使注释处理器工作,它们必须在META-INF中有一些文件。 To achieve that, we'll use another annotation processor called autoservice . 为此,我们将使用另一个名为autoservice的注释处理器。 So in the pom.xml file insert it's dependency. 所以在pom.xml文件中插入它的依赖项。

     <dependencies> <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc2</version> </dependency> </dependencies> 

    7.1 Side-note: For some reason, if I don't specify it explicitly, the Maven project uses Java 1.5. 7.1 旁注:由于某种原因,如果我没有明确指定它,Maven项目使用Java 1.5。 To force it to work with Java 1.8, insert this into the pom.xml file. 要强制它与Java 1.8一起使用,请将其插入到pom.xml文件中。

     <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> 
  8. Annotate our Processor Class with @AutoService(Processor.class) . 使用@AutoService(Processor.class)注释我们的处理器类。

  9. Now, we have to set up a remote debugger configuration in IDEA. 现在,我们必须在IDEA中设置远程调试器配置。 To do that, go to Run > Edit Configurations , click on the green + button on the top left, select remote. 为此,请转到“运行”>“编辑配置” ,单击左上角的绿色+按钮,选择“远程”。 Name it something like "mvnDebug", set the Host to localhost and the Port to 8000 , press ok and it's good to go. 将其命名为“mvnDebug”,将Host设置为localhost ,将Port设置为8000 ,按ok即可。

  10. Set a break point in the process method in our Processor. 在我们的处理器中的process方法中设置一个断点。

  11. Open up the Windows command prompt, navigate to your projects directory, where the pom.xml resides. 打开Windows命令提示符,导航到pom.xml所在的项目目录。 Then type in mvnDebug clean install .If everything has been set up right, it should say something like "Listening for transport dt_socket at address: 8000". 然后键入mvnDebug clean install 。如果一切设置正确,它应该说“在地址:8000处侦听传输dt_socket”。

  12. Go back to IDEA and execute the mvnDebug configuration we've just made. 回到IDEA并执行我们刚刚制作的mvnDebug配置。 If everything has been set up right, it should say something like "Connected to the target VM, address: 'localhost:8000', transport: 'socket'". 如果一切都设置正确,它应该说“连接到目标VM,地址:'localhost:8000',传输:'套接字'”。

  13. Go back to the Command Prompt and if nothing is happening press some key to wake it up. 返回命令提示符,如果没有发生任何事情,请按一些键将其唤醒。

  14. If everything was set up right, IDEA will stop at the breakpoint, suspending javac's (the Java compiler) execution. 如果一切设置正确,IDEA将在断点处停止,暂停javac(Java编译器)执行。


Additional tutorials on annotation processing 有关注释处理的其他教程

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

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