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<? 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();
    }
}
  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.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值