【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )
文章目錄
- 總結(jié)
- 一、閉包類 Closure 簡介
- 二、閉包類 Closure 中 this、owner、delegate 成員 源碼分析
- 三、分析編譯后的字節(jié)碼文件內(nèi)容
總結(jié)
在閉包中 , 打印 this , owner , delegate , 打印結(jié)果都是閉包所在的類 ;
一、閉包類 Closure 簡介
在閉包 Closure 中有 333 個成員 , this , owner , delegate , 在閉包中打印這 333 個成員 ,
def closure = {println "this : ${this}"println "owner : ${owner}"println "delegate : ${delegate}" }執(zhí)行閉包的 call() 方法 , 或者直接使用 閉包() 執(zhí)行閉包 ;
closure()打印結(jié)果如下 , 打印的是閉包對象 ;
this : Groovy@5c45d770 owner : Groovy@5c45d770 delegate : Groovy@5c45d770Groovy.groovy 代碼編譯后的字節(jié)碼文件是 Groovy.class , 其中
二、閉包類 Closure 中 this、owner、delegate 成員 源碼分析
閉包類 Closure 中的 delegate , owner , thisObject 成員如下 , 在構(gòu)造函數(shù)中 , 為 Object owner, Object thisObject 這 222 個成員賦值 ;
在閉包中 , 訪問 owner , 實(shí)際上是調(diào)用 getOwner 函數(shù) , 訪問 delegate 實(shí)際上是調(diào)用 getDelegate 函數(shù) , this 就是 thisObject ;
特別注意 , 在構(gòu)造函數(shù)中 , 為這 333 個成員進(jìn)行了賦值 ;
閉包類 Closure 中 this、owner、delegate 成員 源碼 :
public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {private Object delegate;private Object owner;private Object thisObject;// 閉包構(gòu)造函數(shù) public Closure(Object owner, Object thisObject) {this.owner = owner;this.delegate = owner;this.thisObject = thisObject;final CachedClosureClass cachedClass = (CachedClosureClass) ReflectionCache.getCachedClass(getClass());parameterTypes = cachedClass.getParameterTypes();maximumNumberOfParameters = cachedClass.getMaximumNumberOfParameters();}/*** @return 方法調(diào)用將轉(zhuǎn)到的所有者對象,通常是構(gòu)造閉包時(shí)的外部類*/public Object getOwner() {return this.owner;}/*** @return 構(gòu)造閉包時(shí),方法調(diào)用將轉(zhuǎn)到的委托對象通常是外部類*/public Object getDelegate() {return this.delegate;} }三、分析編譯后的字節(jié)碼文件內(nèi)容
查看 Groovy 代碼編譯后的字節(jié)碼文件 Groovy.class ,
public class Groovy extends Script在該編譯后的字節(jié)碼文件中 , 聲明的閉包變量
def closure = {println "this : ${this}"println "owner : ${owner}"println "delegate : ${delegate}" }生成的對應(yīng)的閉包類為 :
final class _run_closure1 extends Closure implements GeneratedClosure該閉包類的構(gòu)造函數(shù)是在 public class Groovy extends Script 中的 run 方法中調(diào)用 , 將 Groovy 實(shí)例對象傳入到了閉包構(gòu)造函數(shù)中 ;
// 創(chuàng)建閉包 , 傳入的參數(shù) this 是 class Groovy extends Script 類實(shí)例對象 Object closure = new _run_closure1(this, this);在閉包類的構(gòu)造函數(shù)中 , 調(diào)用了父類的構(gòu)造函數(shù) , 分別將 _outerInstance 賦值給 owner 成員 , 將 _thisObject 賦值給 thisObject 成員 , 而 _thisObject 和 _outerInstance 參數(shù)都是 this , 即 Groovy 腳本的生成類 , class Groovy extends Script ;
// 閉包構(gòu)造函數(shù) public _run_closure1(Object _outerInstance, Object _thisObject) {CallSite[] var3 = $getCallSiteArray();// 此處調(diào)用了父類的構(gòu)造函數(shù) , 分別// 將 _outerInstance 賦值給 owner 成員// 將 _thisObject 賦值給 thisObject 成員 // 而 _thisObject 和 _outerInstance 參數(shù)都是 this // 即 Groovy 腳本的生成類 , class Groovy extends Scriptsuper(_outerInstance, _thisObject);}編譯后的字節(jié)碼內(nèi)容如下 :
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) //import groovy.lang.Binding; import groovy.lang.Closure; import groovy.lang.Script; import groovy.transform.Generated; import org.codehaus.groovy.runtime.GStringImpl; import org.codehaus.groovy.runtime.GeneratedClosure; import org.codehaus.groovy.runtime.InvokerHelper; import org.codehaus.groovy.runtime.callsite.CallSite;public class Groovy extends Script {public Groovy() {CallSite[] var1 = $getCallSiteArray();super();}public Groovy(Binding context) {CallSite[] var2 = $getCallSiteArray();super(context);}public static void main(String... args) {CallSite[] var1 = $getCallSiteArray();var1[0].call(InvokerHelper.class, Groovy.class, args);}public Object run() {CallSite[] var1 = $getCallSiteArray();// 閉包類final class _run_closure1 extends Closure implements GeneratedClosure {// 閉包構(gòu)造函數(shù) public _run_closure1(Object _outerInstance, Object _thisObject) {CallSite[] var3 = $getCallSiteArray();// 此處調(diào)用了父類的構(gòu)造函數(shù) , 分別// 將 _outerInstance 賦值給 owner 成員// 將 _thisObject 賦值給 thisObject 成員 // 而 _thisObject 和 _outerInstance 參數(shù)都是 this // 即 Groovy 腳本的生成類 , class Groovy extends Scriptsuper(_outerInstance, _thisObject);}// 調(diào)用閉包public Object doCall(Object it) {CallSite[] var2 = $getCallSiteArray();var2[0].callCurrent(this, new GStringImpl(new Object[]{this.getThisObject()}, new String[]{"this : ", ""}));var2[1].callCurrent(this, new GStringImpl(new Object[]{var2[2].callGroovyObjectGetProperty(this)}, new String[]{"owner : ", ""}));return var2[3].callCurrent(this, new GStringImpl(new Object[]{var2[4].callGroovyObjectGetProperty(this)}, new String[]{"delegate : ", ""}));}// 調(diào)用閉包 @Generatedpublic Object doCall() {CallSite[] var1 = $getCallSiteArray();return this.doCall((Object)null);}}// 創(chuàng)建閉包 , 傳入的參數(shù) this 是 class Groovy extends Script 類實(shí)例對象 Object closure = new _run_closure1(this, this);return var1[1].call(closure);} }總結(jié)
以上是生活随笔為你收集整理的【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】闭包 Closure (
- 下一篇: 【Groovy】闭包 Closure (