Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Excerpt
hiddentrue

Overriding invokeMethod for static methods

ExpandoMetaClass - Overriding invokeMethod for static

...

methods

It is also possible to override invokeMethod for static methods.

...

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

Stuff.metaClass.'static'.invokeMethod = { String name, args ->
   def metaMethod = Person.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 overriden 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:

...