Happy Birthday Groovy!

Groovy is now 1.0RC. Ok, it’s a little early to celebrate, but I’ve been using Groovy for the past year or so and I find it very stable and feature packed. If you haven’t heard about or seen Groovy yet you owe it to yourself to take a peep. Groovy is Java on Steroids. Its writing Java code and removing the shackles. It compiles to byte-code supports roughly 80-90% of classic Java syntax, supports dynamic (duck) typing, closures, functional programming, meta programming (or M.O.P.), quasi-programming, half-assed-programming, in language DSLs, works over your existing DSL (or comcast broadband cable), and it even managed to remove the build up on my shower head. With Groovy you gain the expressiveness of a diety. In groovy you say, “let there be web services” and the Groovy runtime responds, “thy will be done!” Here’s a breif example of something common you might hack in Java. You want a dump of the system (JVM) properties so you query the System class’ getProperties method to retrieve a map and iterate them printing each one.

In Java:
for(Map.Entry entry : System.getProperties().entrySet())
System.out.println(entry.getKey() + ": " + entry.getValue());

In Groovy (classic Java style):
for(Map.Entry entry in System.getProperties().entrySet())
System.out.println(entry.getKey() + ": " + entry.getValue());

In Groovy (groovy style):
System.properties.each { println "${it.key}: ${it.value}" }

While we’re looking at system properties, what if you wanted to print properties with a key name starting with something in particular? In Java you would need to write a method to accept a string and filter for properties starting with that string. Not too bad but let me show you what you can do with a little bit of Groovy and meta programming.

class SysProps {
   Object invokeMethod(String name, Object args) {
      def findQuery = 'findpropertiesfor'
         System.properties.findAll { it.key.toLowerCase().startsWith(name.substring(findQuery.length()).toLowerCase()) }.each { println "${it.key}: ${it.value}" }

def props = new SysProps() println "User properties" props.findPropertiesForUser() println "\n-----------" println "Groovy properties" props.findPropertiesForGroovy() println "\n-----------" println "Sun properties" props.findPropertiesForSun()

That’s my example of a mini in-language DSL. (Or better, a half-assed quasi-DSL useful for hardly anything.) I have a class, SysProps that defines one special method. That special method is an interceptor method (meta programming) that intercepts any method calls on the object. So writing one method actually defines thousands of virtual methods. The code following the class definition excercises those virtual methods. It prints the user properties followed by the groovy properties followed by all of the sun defined system properties. That’s part of the magic behind Grails that lets you write database queries in a Groovier friendlier syntax.

This ain’t s’posed to be no Groovy tutorial, yall. I just wanted to thank the Groovy development team for a job well done and a banging product. If you haven’t gotten in the groove yet get there.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s