Groovy:当用闭包覆盖invokeMethod时,得到了StackOverflowerError
我正试图用Groovy AOP方法增强我的Grails项目。然而,如果我用闭包覆盖invokeMethod,我总是会得到StackOverflowerError。这是我的测试代码,我可以用groovy 2.1.3重现错误,谢谢Groovy:当用闭包覆盖invokeMethod时,得到了StackOverflowerError,groovy,aop,Groovy,Aop,我正试图用Groovy AOP方法增强我的Grails项目。然而,如果我用闭包覆盖invokeMethod,我总是会得到StackOverflowerError。这是我的测试代码,我可以用groovy 2.1.3重现错误,谢谢 class A implements GroovyInterceptable { void foo(){ System.out.println( "A.foo"); } } class B extends A { void foo
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();
看起来,如果调用了
super()
,那么元类将使用缓存来查找方法,并最终抛出StackOverflow。在这种情况下,如果您使用元类
A而不是B,则一切正常
defmc=A.metaClass
我可以这样推断,直接实现GroovyInterceptable
的类应该重写invokeMethod
@Source它就像一个符咒!根据你指出的MetaClassImpl的源代码,我想你是对的,我们应该在实现GroovyInterceptable的类中重写invokeMethod。