Confused about the invokeMethod method in the Groovy MOP -
first @ following groovy code:
class car { def check() { system.out.println "check called..." } def start() { system.out.println "start called..." } } car.metaclass.invokemethod = { string name, args -> system.out.print("call $name intercepted... ") if (name != 'check') { system.out.print("running filter... ") car.metaclass.getmetamethod('check').invoke(delegate, null) } def validmethod = car.metaclass.getmetamethod(name, args) if (validmethod != null) { validmethod.invoke(delegate, args) } else { car.metaclass.invokemissingmethod(delegate, name, args) } } car = new car() car.start()
the output is:
call start intercepted... running filter... check called... start called...
according groovy method dispatching mechanism think start method in car should called directly instead of being intercepted invokemethod in car's metaclass. why start method intercepted invokemethod? how invokemethod invoked when method called on object?
if can give me detailed explanations groovy method dispatching mechanism(mop) appreciate that.
in short not using standard meta class, don't standard groovy mop.
car.metaclass.invokemethod = {
let car have expandometaclass meta class. meta class uses invokemethod give in open block (like do) intercept calls. different defining invokemethod in class itself.
Comments
Post a Comment