Groovy 1.1 beta 3: method missing

Groovy 1.1. beta 3 was announced a couple of days ago and includes some interesting additions to the groovy language, like a real method missing. Grails' lead developer Graeme Rocher has a simple example of the way it works in his blog:

GROOVY:
  1. Foo.metaClass.methodMissing = { String name, args ->
  2.     Foo.metaClass."$name" = { Object[] varArgs ->
  3.         "$name : ${varArgs.inspect()}"
  4.     }
  5.     delegate."$name"(args)
  6. }
  7.  
  8. def f = new Foo()
  9. f.sayHello('Fred')
  10. assert f.notARealMethod("boo")  == 'notARealMethod : [["boo"]]'
  11. assert f.notARealMethod("boo", "hoo") == 'notARealMethod : [["boo", "hoo"]]'

Much better (IMHO) then previous way to do this, which involved intercepting ALL calls to the object on which one would like to catch method missing calls.


0 Responses to “Groovy 1.1 beta 3: method missing”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


Java + You on a cab sea_lion Scribbled Sun Logo alcatraz Stretched Limo nearby hotel golden_gate_warning_sign javaone 2008 goodybag Charles Nutter & Guillaume Laforge smashmouth javaone2008 keynote Greenland Golden Gate Rudie pub Stage being build in the nearby park Cable Car line Acme Anvile at CommunityOne Keynote Community One Keynote Tim Bray introducing the (J)Ruby panel
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.