metaClass things to remember

So I just read a post where a kind of cool debug was posted by Jochen Theodorou (blackdrag) The Groovy Project Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ http://www.g2one.com/ Anyway, I thought this was a great example of something that could be done with a metaClass change. I want to debug something so I do object.debug() You're asking, …

metaClass tips

MetaClass fun... A metaClass is a way to add a method-like statement to an existing Class at runtime. I don't know another way to say it really. For the JavaScript gurus out there, this is the same as the prototype if you rewrote the code I provide swapping metaClass with prototype they'd be the same …

kind of MetaClass-y

I wanted to do something with a closure where my action listener didn't have to know what events to use... e.g. normally, you'd tell the implemented ActionListener that if (event.name) == "bob" or whatever then perform an action in this case, we're going to use a simple ActionListener that doesn't care about the event, just …

Delegate DSL-like

This one is a little different. We're using the same code (the printer closure) with two different classes... in this case delegates. Usage examples include the one below, but also things like implemented interfaces or say append in one case adds to a StringBuilder or StringBuffer and in another case it's a stub debug that …

Category DSL

Here's another example. This one using Categories... not quite the happy happy joy joy of other ways of doing DSLs, but it does work. class MyUtility { static Integer getDays(Integer self) { self } static Calendar getAgo(Integer self) { def cal = Calendar.instance cal.add(Calendar.DAY_OF_MONTH, -self) cal } static Date at(Calendar self, Double time) { def …

DND DSL

This comes from the Venkat DSL screencast. Not that you can't type yourself, but it shows a different way to do a DSL than most people think about thePlayers = [:] def players(String[] args) { args.each {player -> thePlayers[player] = 0 } } def getPrint_dice() { thePlayers.each {player, dice -> println "${player} rolled ${dice}" } …