找到你要的答案

Q:GroovyInterceptable and java callback

Q:GroovyInterceptable和java回调

How can I have the normal behavior for a GroovyInterceptable object (id est always call invokeMethod) where methods are called from Groovy and from Java.

I have a Groovy code where invokeMethod is customized.

interface ITest {
    void test()
}
class Toto implements ITest, GroovyInterceptable {
    def invokeMethod(String name, Object args) {
        System.out.println 'invokeMethod'
    }
    void test() {
        System.out.println 'test'
    }
}
Toto toto = new Toto()
toto.test() // display invokeMethod 

If I have a callback that's still ok

class Tutu {
    def execute(ITest object) {
        object.test()
    }
}

Tutu tutu = new Tutu()
tutu.execute(toto)​ // display invokeMethod

But if the class Tutu is not in Groovy but in Java, the call to test() is direct, invokeMethod is never called. That's seem clear that Java doesn't take care of GroovyInterceptable…

Obviously, Toto is a class in my project and Tutu is a class from one of my dependency that I can't modify. So what to do on Toto to get the behavior in all cases.

The only solution that I see is some AST for each public/protected method

我怎么会有一个GroovyInterceptable对象的正常行为(即总是调用invokeMethod),方法是从Groovy和从java调用。

我有一个绝妙的代码中是定制的。

interface ITest {
    void test()
}
class Toto implements ITest, GroovyInterceptable {
    def invokeMethod(String name, Object args) {
        System.out.println 'invokeMethod'
    }
    void test() {
        System.out.println 'test'
    }
}
Toto toto = new Toto()
toto.test() // display invokeMethod 

如果我有一个回调仍然OK

class Tutu {
    def execute(ITest object) {
        object.test()
    }
}

Tutu tutu = new Tutu()
tutu.execute(toto)​ // display invokeMethod

But if the class Tutu is not in Groovy but in Java, the call to test() is direct, invokeMethod is never called. That's seem clear that Java doesn't take care of GroovyInterceptable…

Obviously, Toto is a class in my project and Tutu is a class from one of my dependency that I can't modify. So what to do on Toto to get the behavior in all cases.

我认为唯一的解决办法是每个公共/保护方法AST

java  groovy  callback