Groovy - override invokeMethod method for one instance

I have an instance of a java object, say an ArrayList instance called myList.

For this particular instance, I want to override the invokeMethod method for (say) the log that called this method.

I could do something like this:

myList.metaclass.invokeMethod { name, args -> println "Called ${name} with ${args}" whatGoesHere.invokeMethod(name, args) } 

Note the second closing line - how can I call the original invokeMethod method? Am I doing it right?

+4
source share
1 answer

There may be a shorter path to the original method, but this should work:

 def myList = [ 1, 2, 3 ] myList.metaClass.invokeMethod { name, args -> println "Called ${name} with ${args}" delegate.class.metaClass.getMetaMethod( name, args )?.invoke( delegate, args ) } myList.sum() 
+5
source

All Articles