Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you debug java annotation processors using intellij?

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 not break.

like image 344
akula1001 Avatar asked Dec 21 '11 08:12

akula1001


People also ask

What are annotation processors in IntelliJ?

The annotation processor can validate, generate, and modify your code based on the annotations, which help you significantly reduce the amount of code you need to write. The annotation processor could be stored inside your project. In this case IntelliJ IDEA obtains it from the classpath.

How do I Debug Java code in IntelliJ?

Run the program in debug modeClick the Run icon in the gutter, then select Modify Run Configuration. Enter arguments in the Program arguments field. Click the Run button near the main method. From the menu, select Debug.

How do I run an annotation processor?

Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.

How do I see annotations in IntelliJ?

Enable annotationsRight-click the gutter in the editor or in the Differences Viewer and select Annotate with Git Blame from the context menu. You can assign a custom shortcut to the Annotate command: go to the Keymap page of the IDE settings Ctrl+Alt+S and look for Version Control Systems | Git | Annotate.


2 Answers

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.


If running javac directly, you can debug this by specifying the following extra parameters:

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.


If running Ant, add the following to the ANT_OPTS environment variable before running:

-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. IntelliJ instructions for this are here. For Eclipse, here.

like image 81
prunge Avatar answered Sep 30 '22 16:09

prunge


This tutorial is written for an Android project. Main module name is "app" as usual. The project contains a submodule called "annotation" which is subdependency of "app". "app" module runs annotation processing with gradle declaration apt project(':annotation') .

SIMPLE VERSION (run compilation from terminal and attach from 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. Port should be left as the default 5005.

enter image description here

  1. [REQUIRED] Make sure you stop all gradle instances by calling: ./gradlew --stop
  2. [REQUIRED] Run the command : ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac

enter image description here

  1. Run the APT project configuration in debug mode as fast as possible :)

enter image description here


  1. [HINT] We start with an EMPTY gradle.properties file
  2. [HINT] DO NOT USE gradle daemon ( --no-daemon / org.gradle.daemon=false option )
  3. [HINT] Run gradle in debug mode ( org.gradle.debug=true option )
  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 (i.e. File -> other settings -> Default settings)

EXTENDED VERSION (use gradle.properties)

  1. Add the following to your gradle.properties file:
      org.gradle.daemon=false     org.gradle.debug=true  
  1. Run the compilation from terminal:

./gradlew :app:clean :app:compileDebugJavaWithJavac

enter image description here


ADVANCED VERSION (just press debug in IDE)

  1. Add a bash script to your project main dir (e.g. 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. Select Run external tool.

enter image description here

  1. Add the path to the compile.sh script we created earlier.

enter image description here


Warning

Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). If you cannot stop gradle from the IDE then use this command in the terminal:

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.

like image 20
kosiara - Bartosz Kosarzycki Avatar answered Sep 30 '22 16:09

kosiara - Bartosz Kosarzycki