IDEA 调试自定义AbstractProcessor

IDEA 调试自定义AbstractProcessor

本人需要自定义Processor代码编译插件,在开发过程中API不熟悉,需要多次Debugger调试。搜索了很久才找到怎么对自定义Processor进行调试。

参考地址:https://stackoverflow.com/questions/31345893/debug-java-annotation-processors-using-intellij-and-maven
示例开发:https://blog.mythsman.com/2017/12/19/1/

  1. First of all, download and install Maven, then download and install IntelliJ IDEA (referred to as IDEA from here on). (If you don’t know how to use Windows CMD, here is a short tutorial for it, also: how to open the command prompt)
  2. Create a Maven project in IDEA without any Archetype. Then create some some package in src > main > java
  3. Create a Class which extends javax.annotation.processing.AbstractProcessor.
    Insert some minimal code, just to make it work. (Don’t forget the Annotation at the top of the class declaration!)
  4. Assuming that the annotation full path 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 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();
    }
}
  1. Create an annotation in the same package.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Factory {
}
  1. 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. 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, for annotation processors to work, they have to have some file in META-INF. To achieve that, we’ll use another annotation processor called autoservice. So in the pom.xml file insert it’s dependency.

com.google.auto.service auto-service 1.0-rc2

7.1 Side-note: For some reason, if I don’t specify it explicitly, the Maven project uses Java 1.5. To force it to work with Java 1.8, insert this into the pom.xml file.



maven-compiler-plugin
3.3

1.8
1.8




Annotate our Processor Class with @AutoService(Processor.class).
Now, we have to set up a remote debugger configuration in 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.
Set a break point in the process method in our Processor.
Open up the Windows command prompt, navigate to your projects directory, where the pom.xml resides. 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”.
Go back to IDEA and execute the mvnDebug configuration we’ve just made. If everything has been set up right, it should say something like “Connected to the target VM, address: ‘localhost:8000’, transport: ‘socket’”.
Go back to the Command Prompt and if nothing is happening press some key to wake it up.
If everything was set up right, IDEA will stop at the breakpoint, suspending javac’s (the Java compiler) execution.

你可能感兴趣的:(JAVA随笔)