how do you debug java annotation processors using intellij?

后端 未结 8 950
春和景丽
春和景丽 2020-12-07 13:47

How do you debug java annotation processors using intellij?

Preferably using IDEA IntelliJ. I tried setting a breakpoint inside the processor and running but it did

8条回答
  •  一生所求
    2020-12-07 14:21

    It is possible to run javac and debug it, as indicated higher. However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. So in the module where I wanted to apply my annotation processor, just create a class with main method.

    public static void main(String[] args) {
        com.sun.tools.javac.Main.main("-proc:only",
            "-processor", "my.pkgs.MyAnnotationProcessor",
            "my/pkgs/any/ClassIWantProcess.java");
    }
    

    For this to work you need to add $JAVA_HOME/lib/tools.jar to your SDK's JARs (by default it is not there). This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler.

    Then just set proper working directory for your run configuration (so the relative path to the java file is correct), set your break-point into the processor and debug at your will!

    Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. I believe it's possible to translate it to other IDEs too.

提交回复
热议问题