2013-06-12 1 views
0

Groovy AOP 방식으로 Grails 프로젝트를 향상 시키려고합니다. 그러나 closure를 사용하여 invokeMethod를 재정의하면 항상 StackOverflowError가 발생합니다. 내 테스트 코드는 groovy 2.1.3으로 오류를 재현 할 수있다. 고마워!Groovy : 클로저로 invokeMethod를 오버라이드 할 때 StackOverflowError가 발생했습니다.

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(); 

답변

2

super()에 대한 호출이있는 경우 다음 메타 클래스는 방법을 찾기 위해 캐시를 사용하고 결국에 StackOverflow를 던져, 같은데. 이 경우 B 대신에 metaClass A가 있으면 모두 정상적으로 작동합니다.

def mc = A.metaClass

나는 클래스는 직접 invokeMethod를 오버라이드 (override) 할 필요가 GroovyInterceptable을 구현, 이런 식으로 추론 할 수있다.

@ 소스 MetaClassImpl

+0

그것은 매력처럼 작동합니다! 그리고 MetaClassImpl의 소스 코드에 따르면, 실제로 GroovyInterceptable을 구현하는 클래스에서 invokeMethod를 항상 오버라이드해야한다고 생각합니다. –

관련 문제