当前位置:网站首页>[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@5c45d770
Groovy.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); }}
边栏推荐
- Network security - record web vulnerability fixes
- What are the building energy-saving software
- 指针函数(基础)
- Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
- Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
- Neural network and deep learning Chapter 1: introduction reading questions
- Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
- The difference between bundle, chunk and module
- Invalid bound statement (not found) in idea -- problem solving
- Here comes the Lantern Festival red envelope!
猜你喜欢
[uniapp] system hot update implementation ideas
线上故障突突突?如何紧急诊断、排查与恢复
[phantom engine UE] the difference between running and starting, and the analysis of common problems
Solution of circular dependency
[finebi] the process of making custom maps using finebi
2022-2028 global and Chinese FPGA prototype system Market Research Report
[thingsboard] how to replace the homepage logo
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
揭秘技术 Leader 必备的七大清奇脑回路
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
随机推荐
This is an age of uncertainty
PR video clip (project packaging)
可观测|时序数据降采样在Prometheus实践复盘
jmeter -- 分布式压测
SPI read / write flash principle + complete code
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link
[phantom engine UE] the difference between running and starting, and the analysis of common problems
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
Debug insights
机器学习 --- 神经网络
Serpentine matrix
[crampon game] MC tutorial - first day of survival
Sword finger offer 07 Rebuild binary tree
托管式服务网络:云原生时代的应用体系架构进化
首席信息官如何利用业务分析构建业务价值?
CSDN正文自动生成目录
Error statuslogger log4j2 could not find a logging implementation
windows下Redis-cluster集群搭建
Wenet: E2E speech recognition tool for industrial implementation
函数(易错)