【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )
文章目錄
- 總結
- 一、閉包類 Closure 簡介
- 二、閉包類 Closure 中 this、owner、delegate 成員 源碼分析
- 三、分析編譯后的字節碼文件內容
總結
在閉包中 , 打印 this , owner , delegate , 打印結果都是閉包所在的類 ;
一、閉包類 Closure 簡介
在閉包 Closure 中有 333 個成員 , this , owner , delegate , 在閉包中打印這 333 個成員 ,
def closure = {println "this : ${this}"println "owner : ${owner}"println "delegate : ${delegate}" }執行閉包的 call() 方法 , 或者直接使用 閉包() 執行閉包 ;
closure()打印結果如下 , 打印的是閉包對象 ;
this : Groovy@5c45d770 owner : Groovy@5c45d770 delegate : Groovy@5c45d770Groovy.groovy 代碼編譯后的字節碼文件是 Groovy.class , 其中
二、閉包類 Closure 中 this、owner、delegate 成員 源碼分析
閉包類 Closure 中的 delegate , owner , thisObject 成員如下 , 在構造函數中 , 為 Object owner, Object thisObject 這 222 個成員賦值 ;
在閉包中 , 訪問 owner , 實際上是調用 getOwner 函數 , 訪問 delegate 實際上是調用 getDelegate 函數 , this 就是 thisObject ;
特別注意 , 在構造函數中 , 為這 333 個成員進行了賦值 ;
閉包類 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;// 閉包構造函數 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 方法調用將轉到的所有者對象,通常是構造閉包時的外部類*/public Object getOwner() {return this.owner;}/*** @return 構造閉包時,方法調用將轉到的委托對象通常是外部類*/public Object getDelegate() {return this.delegate;} }三、分析編譯后的字節碼文件內容
查看 Groovy 代碼編譯后的字節碼文件 Groovy.class ,
public class Groovy extends Script在該編譯后的字節碼文件中 , 聲明的閉包變量
def closure = {println "this : ${this}"println "owner : ${owner}"println "delegate : ${delegate}" }生成的對應的閉包類為 :
final class _run_closure1 extends Closure implements GeneratedClosure該閉包類的構造函數是在 public class Groovy extends Script 中的 run 方法中調用 , 將 Groovy 實例對象傳入到了閉包構造函數中 ;
// 創建閉包 , 傳入的參數 this 是 class Groovy extends Script 類實例對象 Object closure = new _run_closure1(this, this);在閉包類的構造函數中 , 調用了父類的構造函數 , 分別將 _outerInstance 賦值給 owner 成員 , 將 _thisObject 賦值給 thisObject 成員 , 而 _thisObject 和 _outerInstance 參數都是 this , 即 Groovy 腳本的生成類 , class Groovy extends Script ;
// 閉包構造函數 public _run_closure1(Object _outerInstance, Object _thisObject) {CallSite[] var3 = $getCallSiteArray();// 此處調用了父類的構造函數 , 分別// 將 _outerInstance 賦值給 owner 成員// 將 _thisObject 賦值給 thisObject 成員 // 而 _thisObject 和 _outerInstance 參數都是 this // 即 Groovy 腳本的生成類 , class Groovy extends Scriptsuper(_outerInstance, _thisObject);}編譯后的字節碼內容如下 :
// // 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 {// 閉包構造函數 public _run_closure1(Object _outerInstance, Object _thisObject) {CallSite[] var3 = $getCallSiteArray();// 此處調用了父類的構造函數 , 分別// 將 _outerInstance 賦值給 owner 成員// 將 _thisObject 賦值給 thisObject 成員 // 而 _thisObject 和 _outerInstance 參數都是 this // 即 Groovy 腳本的生成類 , class Groovy extends Scriptsuper(_outerInstance, _thisObject);}// 調用閉包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 : ", ""}));}// 調用閉包 @Generatedpublic Object doCall() {CallSite[] var1 = $getCallSiteArray();return this.doCall((Object)null);}}// 創建閉包 , 傳入的參數 this 是 class Groovy extends Script 類實例對象 Object closure = new _run_closure1(this, this);return var1[1].call(closure);} }總結
以上是生活随笔為你收集整理的【Groovy】闭包 Closure ( 闭包类 Closure 简介 | this、owner、delegate 成员赋值及源码分析 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】闭包 Closure (
- 下一篇: 【Groovy】闭包 Closure (