Loading...

December 6, 2009

Groovy Goodness: Add Methods to Interfaces with ExpandoMetaClass

We can add new behaviour to classes with the ExpandoMetaClass, but we can do the same for interfaces. We can add new methods to interfaces through the metaClass property.

interface Nothing { }
class Simple implements Nothing { }

Nothing.metaClass.groovyShoutOut = { -> "Groovy is awesome!" }

def s = new Simple()
assert 'Groovy is awesome!' == s.groovyShoutOut()