【Groovy】Groovy 脚本调用 ( Groovy 脚本编译 | Groovy 脚本字节码文件分析 )





一、Groovy 脚本编译



Groovy 脚本编译后的字节码文件是一个继承 Script 的类 ;

编译下面的 Script.groovy 脚本 ,

/*
    使用 groovy Script.groovy arg0 arg1 命令
    执行该 Script.groovy 脚本
    在 Groovy 脚本后面带上 arg0 和 arg1 参数
    在 Groovy 脚本中 , 可以通过 args 获取参数
    这是一个字符串数组 String[] 类型的
 */

// 打印 Groovy 脚本参数
println "type : ${args.class} , args : ${args}"

使用

groovyc -d classes Script.groovy

命令 , 编译上面的 Groovy 脚本 ;





二、Groovy 脚本字节码文件分析



编译后的字节码文件如下 :

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import groovy.lang.Binding;
import org.codehaus.groovy.runtime.GStringImpl;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.callsite.CallSite;

public class Script extends groovy.lang.Script {
    public Script() {
        CallSite[] var1 = $getCallSiteArray();
        super();
    }

    public Script(Binding context) {
        CallSite[] var2 = $getCallSiteArray();
        super(context);
    }

    public static void main(String... args) {
        CallSite[] var1 = $getCallSiteArray();
        var1[0].callStatic(InvokerHelper.class, Script.class, args);
    }

    public Object run() {
        CallSite[] var1 = $getCallSiteArray();
        return var1[1].callCurrent(this, new GStringImpl(new Object[]{var1[2].callGetProperty(var1[3].callGroovyObjectGetProperty(this)), var1[4].callGroovyObjectGetProperty(this)}, new String[]{"type : ", " , args : ", ""}));
    }
}

Groovy 脚本编译后生成的是 groovy.lang.Script 类 ; 该类继承自 GroovyObjectSupport 类 ;

groovy.lang.Script 类中有一个 Binding binding 成员 ,


/**
 * This object represents a Groovy script
 */
public abstract class Script extends GroovyObjectSupport {
    private Binding binding;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值