Groovy Bar & Grill

July 8, 2006

Groovy in Action Available Now! (sort of)

Filed under: Groovy Bar — jawild59 @ 8:34 am

Manning Publications has added Groovy in Action to its Manning Early Access Program which allows you to “read early chapters now and receive the print book when it is released. Because these are ″early″ chapters, with your feedback you can also help shape the final manuscript.”

June 28, 2006

Groovy JSR-06 Released

Filed under: Groovy Bar — jawild59 @ 8:52 pm

The folks at codehaus have been busy. They’ve just announced the release of JSR-06, the last JSR release before RC-1. Some of the more interesting features, which as of yet, I have not tested:

  • All-Groovy mock support.
  • Add toXXX methiods to GPath expressions for objects produced by XMLSlurper
  • Full stored procedure for groovy.sql.Sql
  • Removing @Property Syntax

Mock Support

Mock is supported in pure Groovy, via ProxyMetaClass and Interceptors. External libraries are no longer needed.

toXXX Methods

It’s no longer necessary to call text() before calling a toXXX method. For example, instead of a.b.c.text().toInteger(), use a.b.c.toInteger()

Stored Procedures

Previously, Groovy didn’t support registering output parameters for a SP call. This has evidently been reversed.

@Property banned

Welcome news, IMHO. The default modifier for the def keyword is now private (previously public) and accessors are provided. The other change is that final is now private (also previously public) and a getter is provided. @Property is still available but I guess it will disappear in RC-1.

Here is a page that spells out the @Property/def changes and here are the full release notes.

June 25, 2006

Groovy Template to Java Code

Filed under: Groovy Grill — jawild59 @ 10:26 am

Enums are one of the nice new features in Java 5. For various reasons many of us don't have access to Java 5 at work, so we make do with homegrown versions of enums. Joshua Bloch's Effective Java has a nice section (Item 21) about using enum classes in Java. If you follow Mr. Bloch's advice and write enum classes, you've no doubt noticed there is some boilerplate code involved. For example, enum classes that implement Comparable need to implement a compareTo method. The differences between two classes is slight but (obviously) significant:

// class Day
  public int compareTo(Object obj) {
    return this.ordinal - ((Day) obj).ordinal;
  }

// class Week
  public int compareTo(Object obj) {
    return this.ordinal - ((Week) obj).ordinal;
  }

It would be nice to have a code generator that produces Java code with the proper values where needed from a template that contains all the tedious code. My first inclination was to read in a template file and replace certain paramters using regular expressions:

(line =~ /#classname/).replaceAll(classname)

It worked fine, but I wanted to use $ to designate a parameter but it is a special character in regex I couldn't use it in the template. (Sidebar: I don't use regex often so I'm no expert, but I thought that /\$classname/ should have worked but it didn't.)

Enter the Groovy Template Engine. The template engine alleviates the pain of typing boilerplate code by processing a template that contains the repetitive code and replaces parameters with unique values. To demonstrate, let's write some Groovy code.

First the template for the compareTo method is a GString that looks like:

def compareTo = '''
  public int compareTo(Object obj) {
    return this.ordinal - (($classname) obj).ordinal;
  }
  '''

Next we'll create a template engine:

def engine = new groovy.text.SimpleTemplateEngine

Now the fun part:

["Day", "Week"].each {
    def binding = ["classname":it]
    println engine.createTemplate(compareTo).make(binding)
  }

The engine uses a map of key-value pairs where the key name matches the name of the template parameter, in this case, "classname" and the value is the value that replaces the template parameter. Running this little snippet produces the following:

public int compareTo(Object obj) {
    return this.ordinal - ((Day) obj).ordinal;
  }

public int compareTo(Object obj) {
    return this.ordinal - ((Week) obj).ordinal;
  }

That's all there is to it. You can download a complete enum generator here.

June 24, 2006

Hoisting Grails to Your Legacy DB

Filed under: Grails — jawild59 @ 6:31 pm

A nice tutorial explains how to hook up a legacy db to Grails.

« Newer Posts

Blog at WordPress.com.