2011-06-21 8 views
1

GroovyInterceptableと組み合わせてinvokeMethodを使用する単純なコードスニペット。ネストされたsimpleMethod2に引数がある場合、それは機能します。引数なしのメソッドでGroovyInterceptableが機能しない

私は説明が見つからなかったので、引数のないネストされたメソッドでも動作するはずです。引数なし

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

出力:予想通り引数を持つメソッドが動作しないので、それはおそらくバグバグや機能:)(かどう

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` 

答えて

2

まだわからない - グルーヴィーでバグを上げるようにしてくださいジラ)。一方、あなたはどの参照thisに変数selfを追加することによって、それを修正して、ネストされたメソッドを呼び出すためにそれを使用することができます:

class SimpleFlow implements GroovyInterceptable { 

def self = this 

def invokeMethod(String name, Object args) { 
    // same as original 
} 

void simpleMethod1(){ 
    System.out.println("simpleMethod1() called") 
    self.simpleMethod2Nested() // note the use of self 
} 

// works well when using an argument 
void simpleMethod2Nested(){ 
    System.out.println("simpleMethod2Nested") 
} 

public static void main(String[] args) { 
    def flow = new SimpleFlow() 
    flow.simpleMethod1() 
} 

} 

出力:

time before simpleMethod1 called: Tue Jun 21 13:32:44 GMT+08:00 2011 
simpleMethod1() called 
time before simpleMethod2Nested called: Tue Jun 21 13:32:44 GMT+08:00 2011 
simpleMethod2Nested 
time after simpleMethod2Nested called: Tue Jun 21 13:32:44 GMT+08:00 2011 
time after simpleMethod1 called: Tue Jun 21 13:32:44 GMT+08:00 2011 
+0

グレート、それがあれば、これは答えたどのくらいの速本当に驚くべきことです私は、私は速度のためのポイントを追加する:)私はなぜちょうど 'これは動作しません - なぜ私は'自己 'に割り当てる必要があります理解できないのですか? Btw。このエラーはGroovy 1.8.0のみであり、下位バージョンは正しく動作します。今晩はバグを報告します。 – Ice09

+0

提出された問題:GROOVY-4892(http://jira.codehaus.org/browse/GROOVY-4892) – Ice09

+0

@ Ice09バグの素敵な良いキャッチ! –

関連する問題