A simple code snippet which uses invokeMethod in combination with GroovyInterceptable. If the nested simpleMethod2 has an argument, it works – otherwise not.
I have not found an explanation, so I assume it should work with nested methods without arguments as well?
class SimpleFlow implements GroovyInterceptable {
def invokeMethod(String name, args) {
System.out.println("time before ${name} called: ${new Date()}")
def calledMethod = SimpleFlow.metaClass.getMetaMethod(name, args)
calledMethod?.invoke(this, args)
System.out.println("time after ${name} called: ${new Date()}\n")
}
void simpleMethod1(){
System.out.println("simpleMethod1() called")
simpleMethod2Nested()
}
// works well when using an argument
void simpleMethod2Nested(){
System.out.println("simpleMethod2Nested")
}
public static void main(String[] args) {
def flow = new SimpleFlow()
flow.simpleMethod1()
}
}
Output without argument:
time before simpleMethod1 called: Tue Jun 21 04:16:41 CEST 2011
simpleMethod1() called
simpleMethod2Nested
time after simpleMethod1 called: Tue Jun 21 04:16:41 CEST 2011`
Not yet sure if it’s a bug or a feature 🙂 (most probably a bug because method with args do work as expected – please do raise a bug in groovy jira). Meanwhile you can fix it by adding a variable
selfwhich references tothisand use it to call the nested method:Output: