当前位置:网站首页>[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
2022-07-05 04:40:00 【Programmer community】
List of articles
- summary
- One 、 Closure class Closure brief introduction
- Two 、 Closure class Closure in this、owner、delegate member Source code analysis
- 3、 ... and 、 Analyze the contents of the compiled bytecode file
summary
In closure , Print this , owner , delegate , The printing results are the class where the closure is located ;
One 、 Closure class Closure brief introduction
In closure Closure There is
3
3
3 Members , this , owner , delegate , Print this in the closure
3
3
3 Members ,
def closure = {
println "this : ${this}" println "owner : ${owner}" println "delegate : ${delegate}"}Execute closure call() Method , Or use it directly Closure () Execute closure ;
closure()The results are as follows , What is printed is the closure object ;
this : Groovy@5c45d770owner : Groovy@5c45d770delegate : Groovy@5c45d770Groovy.groovy The bytecode file after code compilation is Groovy.class , among
Two 、 Closure class Closure in this、owner、delegate member Source code analysis
Closure class Closure Medium delegate , owner , thisObject The members are as follows , In the constructor , by Object owner, Object thisObject this
2
2
2 Member assignments ;
In closure , visit owner , It's actually a call getOwner function , visit delegate It's actually a call getDelegate function , this Namely thisObject ;
Particular attention , In the constructor , For this
3
3
3 Members are assigned ;
Closure class Closure in this、owner、delegate member Source code :
public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {
private Object delegate; private Object owner; private Object thisObject; // Closure constructor 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 The owner object to which the method call will go , It is usually the external class when constructing closures */ public Object getOwner() {
return this.owner; } /** * @return When constructing closures , The delegate object to which the method call will go is usually an external class */ public Object getDelegate() {
return this.delegate; }}3、 ... and 、 Analyze the contents of the compiled bytecode file
see Groovy Bytecode file after code compilation Groovy.class ,
public class Groovy extends Script
In the compiled bytecode file , Declared closure variables
def closure = {
println "this : ${this}" println "owner : ${owner}" println "delegate : ${delegate}"}The generated corresponding closure class is :
final class _run_closure1 extends Closure implements GeneratedClosure The constructor of this closure class is in public class Groovy extends Script Medium run Call in method , take Groovy The instance object is passed into the closure constructor ;
// Create closures , Incoming parameter this yes class Groovy extends Script Class instance object Object closure = new _run_closure1(this, this); In the constructor of closure class , Constructor of parent class called , Separately _outerInstance Assign a value to owner member , take _thisObject Assign a value to thisObject member , and _thisObject and _outerInstance Parameters are this , namely Groovy Script generation class , class Groovy extends Script ;
// Closure constructor public _run_closure1(Object _outerInstance, Object _thisObject) {
CallSite[] var3 = $getCallSiteArray(); // The constructor of the parent class is called here , , respectively, // take _outerInstance Assign a value to owner member // take _thisObject Assign a value to thisObject member // and _thisObject and _outerInstance Parameters are this // namely Groovy Script generation class , class Groovy extends Script super(_outerInstance, _thisObject); }The contents of the compiled bytecode are as follows :
//// 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(); // Closure class final class _run_closure1 extends Closure implements GeneratedClosure {
// Closure constructor public _run_closure1(Object _outerInstance, Object _thisObject) {
CallSite[] var3 = $getCallSiteArray(); // The constructor of the parent class is called here , , respectively, // take _outerInstance Assign a value to owner member // take _thisObject Assign a value to thisObject member // and _thisObject and _outerInstance Parameters are this // namely Groovy Script generation class , class Groovy extends Script super(_outerInstance, _thisObject); } // Call closure 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 : ", ""})); } // Call closure @Generated public Object doCall() {
CallSite[] var1 = $getCallSiteArray(); return this.doCall((Object)null); } } // Create closures , Incoming parameter this yes class Groovy extends Script Class instance object Object closure = new _run_closure1(this, this); return var1[1].call(closure); }}边栏推荐
- Fonction (sujette aux erreurs)
- The principle of attention mechanism and its application in seq2seq (bahadanau attention)
- 【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
- Basic analysis of IIC SPI protocol
- 函数(易错)
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
- All in one 1413: determine base
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- 首席信息官如何利用业务分析构建业务价值?
- 介绍汉明距离及计算示例
猜你喜欢

level18
![[phantom engine UE] realize the animation production of mapping tripod deployment](/img/89/351641c3da7e2acdbf389bc298b75e.png)
[phantom engine UE] realize the animation production of mapping tripod deployment

线上故障突突突?如何紧急诊断、排查与恢复

How can CIOs use business analysis to build business value?

自动语音识别(ASR)研究综述

托管式服务网络:云原生时代的应用体系架构进化
![[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)](/img/eb/a93630aff7545c6c3b71dcc9f5aa61.png)
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)

Label exchange experiment

Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9

The principle of attention mechanism and its application in seq2seq (bahadanau attention)
随机推荐
概率论与数理统计考试重点复习路线
2022-2028 global and Chinese equipment as a Service Market Research Report
Introduction to RT thread kernel (5) -- memory management
A survey of automatic speech recognition (ASR) research
Stage experience
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
Debug insights
Decimal to hexadecimal
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
Machine learning -- neural network
This is an age of uncertainty
Sword finger offer 07 Rebuild binary tree
2022-2028 global and Chinese FPGA prototype system Market Research Report
Discussion on the dimension of confrontation subspace
机器学习 --- 决策树
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Function overloading
Neural networks and deep learning Chapter 3: linear model reading questions
函数(易错)
A solution to the problem that variables cannot change dynamically when debugging in keil5