当前位置:网站首页>[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); }}边栏推荐
- level18
- Web开发人员应该养成的10个编程习惯
- Debug insights
- A solution to the problem that variables cannot change dynamically when debugging in keil5
- 10 programming habits that web developers should develop
- 包 类 包的作用域
- 介绍汉明距离及计算示例
- [goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
- Chapter 6 text processing tools for shell programming (awk)
- History of web page requests
猜你喜欢
![[phantom engine UE] package error appears! Solutions to findpin errors](/img/d5/6747e20da6a8a4ca461094bd27bbf0.png)
[phantom engine UE] package error appears! Solutions to findpin errors
![[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator](/img/cc/06580ce7b553182968d273841a78b4.jpg)
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator

2022-2028 global and Chinese video coding and transcoding Market Research Report

How to get the first few pieces of data of each group gracefully

American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed

Serpentine matrix

SQL set operation

Wenet: E2E speech recognition tool for industrial implementation

【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)

Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
随机推荐
机器学习 --- 决策树
Serpentine matrix
Machine learning -- neural network
直播預告 | 容器服務 ACK 彈性預測最佳實踐
[uniapp] system hot update implementation ideas
Function (error prone)
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
Machine learning decision tree
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Leetcode hot topic Hot 100 day 33: "subset"
取余操作是一个哈希函数
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
[phantom engine UE] the difference between running and starting, and the analysis of common problems
如何优雅的获取每个分组的前几条数据