我正在尝试使用 Groovy AOP 方法来增强我的 Grails 项目。但是,如果我用闭包覆盖 invokeMethod ,我总是会得到 StackOverflowError 。这是我的测试代码,我可以用 groovy 2.1.3 重现错误,谢谢!
class A implements GroovyInterceptable
{
    void foo(){
        System.out.println( "A.foo");
    }
}
class B extends A
{
    void foo(){
        System.out.println( "B.foo");
        super.foo();
    }
}
def mc = B.metaClass;
mc.invokeMethod = { String name, args ->
    // do "before" and/or "around" work here
    try {
        def value = mc.getMetaMethod(name, args).invoke(delegate, args)
        // do "after" work here
        return value // or another value
    }
    catch (e) {
        // do "after-throwing" work here
    }
}
B b = new B();
b.foo();