是否有可能在groovy中获取闭包的调用者对象?

Dav*_*ulo 9 groovy closures

是否有可能获得在执行上下文Object中调用a 的引用?ClosureClosure

例如:

public class Example {

    public Example(){
        def a = {return this};
        def b = [];

        b.metaClass.a = a;

        println b.a();
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这个执行返回b而不是实例Example.

ata*_*lor 12

调用闭包的对象可以引用为delegate.例:

def a = { return delegate }
def b = []

b.metaClass.a = a

assert b.a() == b
Run Code Online (Sandbox Code Playgroud)