Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: correction in example : s/Person/Stuff

...

Code Block
class Stuff {
   static invokeMe() { "foo" }
}

Stuff.metaClass.'static'.invokeMethod = { String name, args ->
   def metaMethod = PersonStuff.metaClass.getStaticMetaMethod(name, args)
    def result
   if(metaMethod) result = metaMethod.invoke(delegate,args)
   else {
      result = "bar"
   }
   result
}

assert "foo" == Stuff.invokeMe()
assert "bar" == Stuff.doStuff()

So what is happening here? Well firstly we've overridden invokeMethod using the 'static' qualifier and by assigning it an appropriate closure, but in addition we first look-up a MetaMethod with the line:

Code Block

def metaMethod = delegate.class.metaClass.getStaticMetaMethod(name)

...