What's New
Quick Links
Groovy greatness - scripting, Java, debugging and update it all on the fly!
Groovy!
The dynamic scripting language for Java I've been meaning to learn and start using. I'm a bit embarrassed its taken me this long, but now that I have I'm a bit floored at how productive it is starting to make me.
One thing essential for me personally to use any programming construct with confidence is to be able to hook it into a debugger. I know of people who love their println statements, but having to sprinkle those into an app to understand what's happening is seems silly at best....incredibly inefficient, time consuming, mind numbing at worst.
Here are a couple quick videos of what I discovered. While experimenting with mixing in some Groovy to an existing Java application, debugging it, and then changing things around it all just worked. Without needing to stop the JVM or recompile-hotswap the debugger still picked up everything. The JVM and debugger even picked up signature changes like new methods and method params. Such somersaults usually makes the JVM hotswap laugh at you like you're a penguin trying to fly.
here are two quick amateur videos showing this:
Labels:
Archives
|
|
May 2008 |
|
||||
|---|---|---|---|---|---|---|
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |

4 Comments
comments.show.hideMay 23, 2008
Anonymous
May 25, 2008
Quinton Dolan
Brendon, I am not sure if you realise this or not, but what you are seeing is not dynamic class redefinition, but a very good debugger paired with plain old classloader replacement.
When your code passes through main and re-evaluates the script again a few things are happening that aren't really obvious because it's such a simple example.
What is happening with your breakpoint is that intellij is smart enough to be able to find this newly loaded class and instrument a break point in the desired location.
Try doing a println(getClass().getClassLoader()) in your groovy method and see what happens each time you pass through the loop.
Your class signature changes aren't being dynamically pushed into the running jvm hotswap style, instead the shell.evaluate(..) call throws the previous class away and creates a new one.
If you want real runtime class redefinition take a look at JavaRebel at www.zeroturnaround.com, I recently started using it and it is the only solution currently available that does exactly what your were hoping was happening, runtime signature changes without restarting. And it works for real java too, so you can use it with your existing codebase.
May 25, 2008
Brendan Patterson
Hi Quinton,
Thanks for the great explanation. After reading and playing more with Groovy plus getting some tips from Chris Broadfoot the way the Groovy shell is creating a new class loader did start to sink in (if slowly
I am definitely going to look at JavaRebel. If it does what I was indeed hoping was happening here - runtime signature and other changes it is indeed worth its weight in gold to me. I did look at it early last year but ran into a snafu where it didn't quite work its magic for me with Confluence / Tomcat webapp devel. There was a known Tomcat issue I can't quite recall but has likely been fixed by now.
I also got wrong the part about Groovy being as fast as Java in the video, but Groovy is getting faster.
Scala also seems to be gaining a lot of traction. It's exciting to see the momentum behind these targeted JVM scripting languages able to leverage Java's power.
May 26, 2008
Quinton Dolan
Actually, I had meant to mention that groovy's performance was not on par with pure java but forgot. It's the price you pay for having fully dynamic method dispatch unfortunately.
Scala has a lot of potential, but switching between Java and Scala is a complete context switch as it offers its greatest benefits when you stop thinking in java and develop your code in a functional way using language concepts that java simply doesn't have. Groovy swings both ways so you can take the functional path or do things the java'ish way but you aren't usually any worse off choosing one over the other.
Scala has historically had the issue of not being able to easily maintain a stable api when you recompile a class due to its type inference behaviour, I believe this is getting a lot better, or may have even been solved by now, I am not sure I haven't looked a scala in a while. It has always been easy to use java classes in scala but not always so easy to do things the other way around. The best thing about using scala is that it offers most of the benefits of the dynamic languages without any of the performance penalties. Scala is as fast as pure java. I think learning scala also makes you a better java programmer, even if you never actually use scala for anything.