分析生成的字节码文件 :
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // import groovy.lang.Closure; import groovy.lang.GroovyObject; import groovy.lang.MetaClass; import groovy.transform.Generated; import groovy.transform.Internal; import org.codehaus.groovy.runtime.GeneratedClosure; import org.codehaus.groovy.runtime.callsite.CallSite; public class Test2 implements GroovyObject { private Object closure2; @Generated public Test2() { CallSite[] var1 = $getCallSiteArray(); super(); // 创建外层闭包 Test2._closure1 var2 = new Test2._closure1(this, this); this.closure2 = var2; MetaClass var3 = this.$getStaticMetaClass(); this.metaClass = var3; } @Generated @Internal public MetaClass getMetaClass() { MetaClass var10000 = this.metaClass; if (var10000 != null) { return var10000; } else { this.metaClass = this.$getStaticMetaClass(); return this.metaClass; } } @Generated @Internal public void setMetaClass(MetaClass var1) { this.metaClass = var1; } @Generated public Object getClosure2() { return this.closure2; } @Generated public void setClosure2(Object var1) { this.closure2 = var1; } // 这是外层的 Closure 闭包 public final class _closure1 extends Closure implements GeneratedClosure { public _closure1(Object _outerInstance, Object _thisObject) { CallSite[] var3 = $getCallSiteArray(); super(_outerInstance, _thisObject); } public Object doCall(Object it) { CallSite[] var2 = $getCallSiteArray(); // 这是内层的 Closure 闭包 final class _closure2 extends Closure implements GeneratedClosure { public _closure2(Object _outerInstance, Object _thisObject) { CallSite[] var3 = $getCallSiteArray(); super(_outerInstance, _thisObject); } public Object doCall(Object it) { CallSite[] var2 = $getCallSiteArray(); var2[0].callCurrent(this, var2[1].call("this : ", this.getThisObject())); var2[2].callCurrent(this, var2[3].call("owner : ", var2[4].callGroovyObjectGetProperty(this))); return var2[5].callCurrent(this, var2[6].call("delegate : ", var2[7].callGroovyObjectGetProperty(this))); } @Generated public Object doCall() { CallSite[] var1 = $getCallSiteArray(); return this.doCall((Object)null); } } // 创建内层闭包时 , 传入的 this 是 外层闭包的 this.getThisObject() // 因此 this 值仍是 Test2 实例对象 // owner、delegate 变为外层的 Closure 闭包 ; Object closure3 = new _closure2(this, this.getThisObject()); return var2[0].call(closure3); } @Generated public Object doCall() { CallSite[] var1 = $getCallSiteArray(); return this.doCall((Object)null); } } }
三、 完整代码示例
完整代码示例 :
class Test2 { // 定义静态闭包 // 即可以通过类执行 // 又可以通过对象执行 def static closure = { println "this : " + this println "owner : " + owner println "delegate : " + delegate } // 闭包中定义闭包 def closure2 = { def closure3 = { println "this : " + this println "owner : " + owner println "delegate : " + delegate } closure3() } } println "通过类执行闭包 :" Test2.closure() println "\n通过对象执行闭包 :" new Test2().closure() println "\n闭包中定义闭包并执行 : " new Test2().closure2()
执行结果 :
通过类执行闭包 : this : class Test2 owner : class Test2 delegate : class Test2 通过对象执行闭包 : this : class Test2 owner : class Test2 delegate : class Test2 闭包中定义闭包并执行 : this : Test2@1f010bf0 owner : Test2$_closure1@40db2a24 delegate : Test2$_closure1@40db2a24