I started learning Groovy a few days ago and one of the things that intrigues any beginning groovy developer (especially coming from a rails background) is how those extra operations get added. Its easy to see this is in Rails as its a scripting language and you could open up the source to see whats happening. Given that Groovy does compile code(though u dont see it) before running it, its interesting to see how those magic operations are added. Groovy parses your script and compiles them internally.
The parsing of the code is done using the Antlr parser (more on this in a later series). But if you just need to see how the internal operations of Groovy work, do the following.
- Download the Groovy source here (Link points to 1.5.4 version)
- Unzip the contents into a directory. Point Intellij or Eclipse to it
- Get the JAD Decompiler here
- Unpack JAD into a directory and add it to your PATH environment variable
- Write a small script similar to this (the script demonstrates the usage of the “plus” operator on integers). Groovy autoboxes the integer to java.lang.Integer but youd see that in action soon on your IDE.
def a = 1, b = 2
assert a.class == Integer.class
assert b.class == Integer.class
println a.plus(b)
- Compile this using the groovyc compiler to generate a test.class file (Assuming that your script was called test.groovy)
groovyc test.groovy
You should see a test.class in this directory after you run the above command - Test Run this first to see if it actually works. You need to add the groovy-.jar and asm-2.2.jar to your class path for this to work
java -cp /apps/groovy-1.5.1/lib/groovy-1.5.1.jar:/apps/groovy-1.5.1/lib/asm-2.2.jar:. test
The output of this should be (unsurprisingly) 3 - Decompile this code just to see whats going on using jad
jad test.class
You should see a pretty sweet t.jad in the same directory. If you need a .java suffix, use
jad -s java test.class
- Now try running this with an IDE. Set up a breakpoint like this when you run your Groovy code
java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 -cp /apps/groovy-1.5.1/lib/groovy-1.5.1.jar:/apps/groovy-1.5.1/lib/asm-2.2.jar:. test
Remember to set “suspend=y” on the above line, otherwise the code will run through even before you connect your debugger. - Connect your IDE to this port. For Intellij, click on “Edit Configurations”, add a new Remote debugging instance and set to port to 5005 (If its not set already)
- Put a breakpoint in ScriptBytecodeAdapter class’s invokeMethodN method and start the fun. Its going to be a pretty long debug session (Yes, for the “1+2″ operation) , but should be fun.
Happy Reverse Engineering!

