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

zolder_oud _MG_8015 javaone2008 keynote IMG_4567 IMG_4609.JPG nearby hotel IMG_6149 zed shaw stoomlocomotief (bw) crab MGA Engine sjoerd op klimrek kleine libelle Stretched Limo IMG_4603 train staatsloten IMG_4606.JPG
View more photos >

Categories



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