简体   繁体   English

你如何使用intellij调试java注释处理器?

[英]how do you debug java annotation processors using intellij?

How do you debug java annotation processors using intellij? 如何使用intellij调试java注释处理器?

Preferably using IDEA IntelliJ. 最好使用IDEA IntelliJ。 I tried setting a breakpoint inside the processor and running but it did not break. 我尝试在处理器内部设置一个断点并运行,但它没有破坏。

If you really need to debug an annotation processor, it might be better to run the annotation processor from the command line rather than within your IDE with debugging enabled and attach to that using your IDE's debugger. 如果您确实需要调试注释处理器,最好从命令行运行注释处理器,而不是在启用了调试的IDE中运行注释处理器,并使用IDE的调试器附加到该处理器。


If running javac directly, you can debug this by specifying the following extra parameters: 如果直接运行javac ,可以通过指定以下额外参数来调试它:

javac -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 ... (usual javac parameters go here)

If running Maven, use mvndebug instead of the standard mvn command - Maven runs the compiler in-process. 如果运行Maven,请使用mvndebug而不是标准的mvn命令 - Maven在进程中运行编译器。


If running Ant, add the following to the ANT_OPTS environment variable before running: 如果运行Ant,请在运行之前将以下内容添加到ANT_OPTS环境变量中:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000

With all these executions, the javac, Ant or Maven process will wait for you to attach your debugger before it actually starts executing. 通过所有这些执行,javac,Ant或Maven进程将等待您在实际开始执行之前附加调试器。 IntelliJ instructions for this are here . IntelliJ的说明就在这里 For Eclipse, here . 对于Eclipse, 这里

Follow these steps, These worked for me on android studio for gradle project:- 按照这些步骤,这些在android studio for gradle项目上为我工作: -

1).In gradle.properties add following lines 1)。在gradle.properties中添加以下行

org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

2).Edit Build COnfiguration and add Remote Configration 2)。编辑构建配置并添加远程配置 在此输入图像描述

3).Run recently created run configuration APT. 3).Run最近创建了运行配置APT。

4).Add break point in annotation processing code and build overall project 4)。在注释处理代码中添加断点并构建整个项目

This tutorial is written for an Android project . 本教程是为Android项目编写的。 Main module name is "app" as usual. 主模块名称像往常一样是“app” The project contains a submodule called "annotation" which is subdependency of "app". 该项目包含一个名为“annotation”的子模块,它是“app”的子顺序。 "app" module runs annotation processing with gradle declaration apt project(':annotation') . “app”模块使用gradle声明apt project(':annotation')运行注释处理。

SIMPLE VERSION (run compilation from terminal and attach from IDE) SIMPLE VERSION (从终端运行编译并从IDE连接)

  1. [REQUIRED] Add a new project configuration "+" -> "Remote" . [必需]添加新项目配置“+” - >“远程” Check "Single instance only" . 选中“仅限单个实例” All other settings are generated automatically. 所有其他设置都会自动生成。 Leave <whole project> as the classpath. <whole project>保留为类路径。 Port should be left as the default 5005 . 端口应保留为默认值5005

在此输入图像描述

  1. [REQUIRED] Make sure you stop all gradle instances by calling: ./gradlew --stop [必需]确保通过调用: ./gradlew --stop停止所有gradle实例
  2. [REQUIRED] Run the command : ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac [必需]运行命令: ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac

在此输入图像描述

  1. Run the APT project configuration in debug mode as fast as possible :) 尽可能快地以调试模式运行APT项目配置:)

在此输入图像描述


  1. [HINT] We start with an EMPTY gradle.properties file [提示]我们从EMPTY gradle.properties文件开始
  2. [HINT] DO NOT USE gradle daemon ( --no-daemon / org.gradle.daemon=false option ) [提示]不要使用gradle 守护程序 (--no-daemon / org.gradle.daemon = false选项)
  3. [HINT] Run gradle in debug mode ( org.gradle.debug=true option ) [提示]在调试模式下运行gradle(org.gradle.debug = true选项)
  4. [HINT] Run app's module compilation not the processor's module compilation (app's compilation runs annotation processing!) [提示]运行应用程序的模块编译而不是处理器的模块编译(应用程序的编译运行注释处理!)
  5. We DO NOT normally add any Java compiler settings in Android Studio (ie File -> other settings -> Default settings) 我们通常不会在Android Studio中添加任何Java编译器设置(即文件 - >其他设置 - >默认设置)

EXTENDED VERSION (use gradle.properties ) 扩展版 (使用gradle.properties

  1. Add the following to your gradle.properties file: 将以下内容添加到gradle.properties文件中:
org.gradle.daemon=false
    org.gradle.debug=true
  1. Run the compilation from terminal: 从终端运行编译:

./gradlew :app:clean :app:compileDebugJavaWithJavac

在此输入图像描述


ADVANCED VERSION (just press debug in IDE) 高级版本 (只需按IDE中的调试)

  1. Add a bash script to your project main dir (eg compile.sh ) 将bash脚本添加到项目主目录(例如compile.sh
#!/bin/bash
./gradlew :app:clean :app:compileDebugJavaWithJavac &

Remember about the '&' sign for background processing. 请记住后台处理的“&”符号。

  1. Go to APT configuration settings we created in step 1 and add a Before launch configuration . 转到我们在步骤1中创建的APT配置设置,并添加Before启动配置 Select Run external tool . 选择运行外部工具

在此输入图像描述

  1. Add the path to the compile.sh script we created earlier. 添加我们之前创建的compile.sh脚本的路径。

在此输入图像描述


Warning 警告

Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). 搞乱gradle编译,编译期间的NullPointer异常等有时会导致AndroidStudio卡住(在gradle刷新阶段冻结)。 If you cannot stop gradle from the IDE then use this command in the terminal: 如果您无法从IDE 停止gradle ,请在终端中使用此命令:

ps -A | grep gradle | awk '{ print $1; }' | xargs kill -9

Turning off debug option during project refresh sometimes helps Android Studio to come back to the right track. 在项目刷新期间关闭调试选项有时可以帮助Android Studio回到正确的轨道。

It is possible to run javac and debug it, as indicated higher. 可以运行javac并对其进行调试,如图所示。 However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. 但是在我的情况下编写完整的类路径是很乏味的,所以我想把它留给IDEA。 So in the module where I wanted to apply my annotation processor, just create a class with main method. 因此,在我想要应用我的注释处理器的模块中,只需使用main方法创建一个类。

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). 为此,您需要将$JAVA_HOME/lib/tools.jar到SDK的JAR中(默认情况下不存在)。 This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler. 这就是编译JSP的appservers在其类路径上需要这个JAR的原因 - 他们需要编译器。

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! 然后只需为您的运行配置设置正确的工作目录(因此java文件的相对路径是正确的),将您的断点设置为处理器并根据您的意愿进行调试!

Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. 好处 - 类路径已由IDEA设置并由“内部”javac使用,因为这里它不是一个单独的过程。 I believe it's possible to translate it to other IDEs too. 我相信也可以将它翻译成其他IDE。

Annotation processing occurs during compilation, so normal debugging won't work. 注释处理在编译期间发生,因此正常调试将不起作用。 If you want to debug it in the context of you project, you can use IntelliJ remote debugging, while having Gradle or Maven in debug mode. 如果要在项目的上下文中调试它,可以使用IntelliJ远程调试,同时在调试模式下使用Gradle或Maven。 Then you can put breakpoints in the Annotation Processor's files. 然后,您可以在Annotation Processor的文件中放置断点。

See Debugging an Annotation Processor in any project . 请参阅在任何项目中调试注释处理器

Disclaimer: I wrote the post. 免责声明:我写了这篇文章。

I found the following resource that can help you: http://code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse 我找到了以下可以帮助您的资源: http//code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse

The guy explains step-by-step how to debug annotation processors using Eclipse. 这家伙逐步解释了如何使用Eclipse调试注释处理器。

For a Maven project, this post , which explains the following steps in a little more detail, worked for me: 对于Maven项目, 这篇文章更详细地解释了以下步骤,对我有用:

  1. Add "Remote" run configuration and set "port" to 8000. 添加“远程”运行配置并将“端口”设置为8000。

  2. Issue the command mvnDebug clean install from the project's directory (on the command line). 从项目目录(在命令行上)发出命令mvnDebug clean install

  3. Run the run configuration. 运行运行配置。 In order to start a new session after the processes quit, repeat from (2). 要在进程退出后启动新会话,请从(2)开始重复。

Remember to run mvn install on the project's dependencies when they change (eg if the annotation processor is in a different artifact than the project you are debugging it from). 记得在项目更改时对项目的依赖项运行mvn install (例如,如果注释处理器与您正在调试它的项目位于不同的工件中)。

Debugging an annotation processor with IntelliJ IDEA and Gradle 使用IntelliJ IDEA和Gradle调试注释处理器

  1. Set a custom VM option -Dcompiler.process.debug.port=5005 : press Ctrl + Shift + A and select Edit Custom VM Options... in the list of actions to add a custom VM option then restart the IDE. 设置自定义VM选项-Dcompiler.process.debug.port=5005 :按Ctrl + Shift + A并在操作列表中选择编辑自定义VM选项...以添加自定义VM选项,然后重新启动IDE。
  2. Create a remote debug configuration with default parameters: Run -> Edit Configurations... -> Add New Configuration (Alt + Insert) -> Remote . 使用默认参数创建远程调试配置: 运行 - >编辑配置... - >添加新配置(Alt + Insert) - >远程 在此输入图像描述
  3. Set breakpoints. 设置断点。
  4. Build with Gradle from the terminal: $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build (it's okay if the execution of the command is frozen, don't terminate a process). 从终端使用Gradle构建: $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build (如果命令的执行被冻结,则不行,不要终止进程)。
  5. Debug the remote debug configuration within the IDE (see step 3): select a suitable remote debug configuration and press Shift + F9 . 在IDE中调试远程调试配置(请参阅步骤3):选择合适的远程调试配置,然后按Shift + F9 在此输入图像描述

Hope it helps somebody :) 希望它有助于某人:)

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

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