Debugging Java annotation processors using Intellij and Maven

I am trying to learn how to create a custom annotation processor, and I am stuck trying to debug it.

I already managed to run the javac compiler in debug mode (with mvnDebug clean install ) (with another project with an annotation processor), connect to it with IntelliJ IDEA and stop its breakpoints in the annotation processor.


If we have something like this in some package in our project, then, like any other class (for example, no special configuration or anything else):

public class MyProcessor extends AbstractProcessor {...} 

Is there any way to connect it to the maven build process as an annotation processor? To compile it first, the entire project is compiled with an active annotation processor.

In addition, as far as I know, annotation processors require a kind of META INF file that can be generated using google autoservices annotation processor.

So, maybe the maven build process, where we first run autoservices , then a class extending AbstractProcessor compiled as an annotation processor, and finally the whole project is compiled with our own annotation processor (and with the javac compiler in debug mode).

+5
source share
1 answer

Here is the prescription.

Sidenote: in some cases I did it in great detail, skipping those parts that you already know how to do.

  • First of all, download and install Maven , then download and install IntelliJ IDEA (link as IDEA from here). (If you don't know how to use Windows CMD, here is a short tutorial for it: how to open a command prompt )

  • Create a Maven project in IDEA without an archetype. Then create some package in src> main> java

  • Create a class that extends javax.annotation.processing.AbstractProcessor.

  • Paste some minimal code to make it work. (Don't forget the annotation at the top of the class declaration!)

    Assuming the full path annotation is core.Factory , the code will look like

     @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(); } } 
  • Create the annotation in the same package.

     public @interface Factory { } 
  • There is probably a src> test> java directory in the project, create another package there with the same name as the package you created earlier. Then create a class in it with a name ending with "Test" (for example: MyProcessorTest). Then annotate this class with the new annotation type you created earlier (@ Factory).

     @Factory public class MyProcessorTest { } 
  • Now, to process annotation handlers, they must have a file in META-INF. To do this, we will use another annotation processor called auto repair . So in the pom.xml file , paste it into the dependency.

     <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, unless I specify it explicitly, the Maven project uses Java 1.5. To make it work with Java 1.8, paste it into the pom.xml file.

     <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> 
  • Annotate our processor class using @AutoService(Processor.class) .

  • Now we need to configure the remote debugger configuration in IDEA. To do this, go to Run> Change Configurations , click the green + button in the upper left corner, select the remote control. Name it something like "mvnDebug", set Host to localhost and port 8000 , click ok, and that's good to go.

  • Set a breakpoint in the process method in our Process.

  • Open a Windows command prompt, go to the projects directory where pom.xml is located. Then type mvnDebug clean install . If everything is configured correctly, it should say something like "Listening to the dt_socket transport at address: 8000."

  • Return to IDEA and complete the mvnDebug configuration we just created. If everything is configured correctly, it should say something like "Connected to the target virtual machine, address:" localhost: 8000 ", transport:" socket ".

  • Return to the command line, and if nothing happens, press the key to wake it.

  • If everything was set up correctly, IDEA will stop at the breakpoint, pausing javac (Java compiler).


Additional annotation processing guides

+8
source

All Articles