Friday, September 21, 2007

The new meta-programming APIs in Groovy 1.1 beta 3

As announced by Guillaume, Groovy 1.1 beta 3 is out and the final release is just round the corner. There is lots of good stuff in there, but I want to talk here primarily about the new meta-programming API improvements that have been introduced in this release.

Groovy has always had the underlying Meta Object Protocol (MOP) that allowed the same behaviour as languages such as Ruby and Small Talk, however the API onto these has, up until now, not been as elegant as it could have been. With Groovy 1.1 beta 3 there is a lot more at your finger tips including...

Runtime evaluation with respondsTo & hasProperty
It is now a lot easier to inspect the runtime thanks to the addition of hasProperty and respondsTo for meta classes so you can do:


class Foo {
String name = "Wilma"
def sayHello() { println "hello $name" }
def sayHello(String message) { println "hello $message" }
}
def f = new Foo()

if(f.metaClass.respondsTo(f,'sayHello') {
f.sayHello()
}


Since Groovy supports method overloading and typed arguments to support good Java interoperability you can also specify a type:


if(f.metaClass.respondsTo(f,'sayHello', String) {
f.sayHello("Fred")
}

Groovy also differentiates method and property access so you can do this:

if(f.metaClass.hasProperty(f,'name') {
println f.name
}



See the docs here for more info on hasProperty and respondsTo

Missing method/property interception with methodMissing & propertyMissing

As of beta 3, Groovy now supports the concept of "method missing". It has always been possible to intercept method dispatch using invokeMethod. However, this has the overhead of intercepting every method call instead of just the "missing" ones. As of Groovy 1.1 beta 3, Groovy now supports "method missing" and "property missing", which are only called when method dispatch fails (ie just before a MissingMethodException would be thrown anyway.)

A trivial example can be seen below, we use this feature in Grails to implement dynamic finders such as findByTitleAndAuthor("Groovy in Action", "Dierk Koenig"):


Foo.metaClass.methodMissing = { String name, args ->
Foo.metaClass."$name" = { Object[] varArgs ->
"$name : ${varArgs.inspect()}"
}
delegate."$name"(args)
}

def f = new Foo()
f.sayHello('Fred')
assert f.notARealMethod("boo")
== 'notARealMethod : [["boo"]]'
assert f.notARealMethod("boo", "hoo")
== 'notARealMethod : [["boo", "hoo"]]'


Notice how in the example above we can dynamically register a new method on the fly and then call that method. This also has the implicationthat the next dispatch to the same method is faster. See the docs here for more info and there are a whole load of docs on doing dynamic Groovy and meta-programming to be found here.

12 comments:

Anonymous said...

almost as cool as Ruby :-)

seriously: great stuff. Groovy is getting better and better.

Claus

Andres Almiray said...

Way to go Graemer! hasProperty() is a feature I was expecting for a quite a while. These changes surelly make Grails development a lot easier ;-)

Vadim Voituk said...

It is strange, that this functions was missed previously.

Peter said...

Hi Graeme,

Is there also a way to find out the properties of a class without having to create an instance? For example:

class User {
String firstname, lastname
}

println User.metaClass.properties

Also is there some way to 'identify' a property for example User.@firstname?

In Ruby I saw this example of binding:

model = Person.new('Julian Doherty', 'julian@example.com')

name_label = JLabel.new
name_label.bind_to model, :name

It would be cool if the Groovy SwingBuilder binding could work this way. The current binding examples I found bind components together I haven't found any model binding examples. Do you know of any plans to work on this?

Good luck on Groovy/Grails,

Peter

Graeme Rocher said...

Hi Peter

Your example

println User.metaClass.properties

Will work, the only thing to be aware of is that it returns a list of MetaProperty instances. So for example you can do this:

User.metaClass.properties.each { println p.name }

As for your binding example I know there is a lot of activity going on around SwingBuilder and bindng. Checkout Danno's blog for the info:

http://shemnon.com/speling/

Peter said...

Thanks Graeme indeed User.metaClass.properties works in the latest Groovy version :)

But if you need the type of firstname property I think you need to do:

User.metaClass.properties.grep { it.name == 'firstname'}[0].type

I think it would be a lot 'groovier' if you could do something like:

User.@firstname.type

Maybe I'm wrong and maybe I would better post this in the Groovy mailing list?

Best regards,

Peter

derfefww said...

I am so happy to get some Atlantica online Gold and the Atlantica Gold is given by my close friend who tells me that the cheap Atlantica online Gold is the basis to enter into the game. Therefore, I should buy Atlantica online Gold with the spare money and I gain some Atlantica online money from other players.

sm said...

網頁設計,情趣用品店,情趣用品專賣網

A片,色情A片,免費A片,成人影片,色情影片,a片免費看,情色貼圖,情色文學,情色小說,色情小說
AV,AV女優

辣妹視訊,美女視訊,視訊交友網,視訊聊天室,視訊交友,視訊美女,免費視訊,免費視訊聊天,視訊交友90739,免費視訊聊天室,成人聊天室,視訊聊天,視訊交友aooyy
哈啦聊天室,辣妺視訊,A片,色情A片,視訊,080視訊聊天室,視訊美女34c,視訊情人高雄網,視訊交友高雄網,0204貼圖區,sex520免費影片,情色貼圖,視訊ukiss

Anonymous said...

Many years ago, I already know the silkroad online game, but at that time I do not play, Recently due to my friends introduce, I began to play the game, but I need the silkroad gold, my friend look me the first time to play so send me some sro gold, but I still need more silkroad online gold, so I want to buy.

Anonymous said...

^^ nice blog!! ^@^

徵信, 徵信網, 徵信社, 徵信社, 感情挽回, 婚姻挽回, 挽回婚姻, 挽回感情, 徵信, 徵信社, 徵信, 徵信, 捉姦, 徵信公司, 通姦, 通姦罪, 抓姦, 抓猴, 捉猴, 捉姦, 監聽, 調查跟蹤, 反跟蹤, 外遇問題, 徵信, 捉姦, 女人徵信, 女子徵信, 外遇問題, 女子徵信, 外遇, 徵信公司, 徵信網, 外遇蒐證, 抓姦, 抓猴, 捉猴, 調查跟蹤, 反跟蹤, 感情挽回, 挽回感情, 婚姻挽回, 挽回婚姻, 外遇沖開, 抓姦, 女子徵信, 外遇蒐證, 外遇, 通姦, 通姦罪, 贍養費, 徵信, 徵信社, 抓姦, 徵信, 徵信公司, 徵信社, 徵信公司, 徵信社, 徵信公司, 女人徵信,

徵信, 徵信網, 徵信社, 徵信網, 外遇, 徵信, 徵信社, 抓姦, 徵信, 女人徵信, 徵信社, 女人徵信社, 外遇, 抓姦, 徵信公司, 徵信社, 徵信社, 徵信社, 徵信社, 徵信社, 女人徵信社, 徵信社, 徵信, 徵信社, 徵信, 女子徵信社, 女子徵信社, 女子徵信社, 女子徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社,

徵信, 徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 外遇, 抓姦, 離婚, 外遇,離婚,

徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信,徵信社, 徵信社, 徵信, 外遇, 抓姦, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社, 徵信社, 徵信社,徵信,徵信, 徵信, 外遇, 抓姦

Anonymous said...

By the way, I recommend you buy the cheapest Atlantica online Gold and Atlantica power leveling from us where the prices here are low. So if you have the Atlantica Gold is useful to your level spear and then you should buy Atlantica online Gold. In addition if you need to buy goods for your characters, the game provides you the cheap Atlantica online Gold. And the last, you can make the fun of the Atlantica online money in the Atlantica online world.
Follow the guide below, you will have a better experience in dofus kamas. Because of Bambooto attack with both earth and wind damage, with also heals kamas. There are some cheap kamas in the Dofus and that is very useful. The best part is that bulbiflor can drop bulbiflor magic bark, which sells for 50000 dofus gold. They sell well kamas anytime too, so you can buy dofus kamas at anytime.

cvxv said...

看房子,買房子,建商自售,自售,台北新成屋,台北豪宅,新成屋,豪宅,美髮儀器,美髮,儀器,髮型,EMBA,MBA,學位,EMBA,專業認證,認證課程,博士學位,DBA,PHD,在職進修,碩士學位,推廣教育,DBA,進修課程,碩士學位,網路廣告,關鍵字廣告,關鍵字,課程介紹,學分班,文憑,牛樟芝,段木,牛樟菇,日式料理, 台北居酒屋,日本料理,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,台北結婚,場地,住宿,訂房,HOTEL,飯店,造型系列,學位,SEO,婚宴,捷運,學區,美髮,儀器,髮型,看房子,買房子,建商自售,自售,房子,捷運,學區,台北新成屋,台北豪宅,新成屋,豪宅,學位,碩士學位,進修,在職進修, 課程,教育,學位,證照,mba,文憑,學分班,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,結婚,場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,居酒屋,燒烤,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,小套房,小套房,進修,在職進修,留學,證照,MBA,EMBA,留學,MBA,EMBA,留學,進修,在職進修,牛樟芝,段木,牛樟菇,關鍵字排名,網路行銷,PMP,在職專班,研究所在職專班,碩士在職專班,PMP,證照,在職專班,研究所在職專班,碩士在職專班,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,EMBA,MBA,PMP
,在職進修,專案管理,出國留學,EMBA,MBA,PMP
,在職進修,專案管理,出國留學,EMBA,MBA,PMP,在職進修,專案管理,出國留學,婚宴,婚宴,婚宴,婚宴,漢高資訊,漢高資訊,比利時,比利時聯合商學院,宜蘭民宿,台東民宿,澎湖民宿,墾丁民宿,花蓮民宿,SEO,找工作,汽車旅館,阿里山,日月潭,阿里山民宿,東森購物,momo購物台,pc home購物,購物

漢高資訊,漢高資訊,在職進修,漢高資訊,在職進修,民宿,民宿,整形,造型,室內設計,室內設計,漢高資訊,在職進修,漢高資訊,在職進修,民宿,美容,室內設計,在職進修,羅志祥,周杰倫,五月天,民宿,民宿,整形,整形,室內設計,室內設計,比利時聯合商學院,在職進修,比利時聯合商學院,在職進修,漢高資訊,找工作,找工作,找工作,找工作,找工作,蔡依林,林志玲