Fork me on Github s

Posts Tagged groovy

Graffiti is a Sinatra-inspired Groovy Framework

Playing around with Sinatra the other day and realized I could really use something like this for Groovy. Thus, Graffiti was born. It’s basically a thin wrapper around Jetty. At first, I thought I might write my own server for it (everybody needs to do that once, don’t they?), but decided to invoke the ‘simplest thing that could possibly work’ principle.

Here is the requisite ‘Hello World’ example:

import graffiti.*

def hello() {
  'Hello World'

Graffiti.serve this

The code, plus more documentation is hosted under my github account.

Tags: , , ,


Intercept method calls in Groovy for automatic type conversion

One of the cooler things you can do with groovy is automatic type conversion.  If you want to convert an object to another type, many times all you have to do is invoke the ‘as’ keyword:

def letters = 'abcdefghijklmnopqrstuvwxyz' as List

But, what if you are wanting to do something a little fancier, like converting a String to a Date?

def christmas = '12-25-2010' as Date

ERROR org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '12-25-2010' with class java.lang.String' to class 'java.util.Date'

No bueno!

I want to be able to do custom type conversions so that my application can do a simple String to Date conversion. Enter the metaMethod. You can intercept method calls in Groovy using the following method:

def intercept(name, params, closure) {
  def original = from.metaClass.getMetaMethod(name, params)
  from.metaClass[name] = { Class clazz ->
    original.doMethodInvoke(delegate, clazz)

Using this method, and a little syntactic sugar, we create the following ‘Convert’ class:

// Convert.from( String ).to( Date ).using { }
class Convert {

    private from

    private to

    private Convert(clazz) { from = clazz }

    static def from(clazz) {
        new Convert(clazz)

    def to(clazz) {
        to = clazz
        return this

    def using(closure) {
        def originalAsType = from.metaClass.getMetaMethod('asType', [] as Class[])
        from.metaClass.asType = { Class clazz ->
            if( clazz == to ) {
                closure.setProperty('value', delegate)
            } else {
                originalAsType.doMethodInvoke(delegate, clazz)

Now, we can make the following statement to add the automatic date conversion:

Convert.from( String ).to( Date ).using { new java.text.SimpleDateFormat('MM-dd-yyyy').parse(value) }

def christmas = '12-25-2010' as Date

Groovy baby!

Tags: ,

No Comments