Groovy的脚本统一于类的世界

本文介绍Groovy脚本的编译过程,包括如何将脚本编译为类、方法定义、变量处理等内容,并解释了Groovy如何通过内部转换处理脚本结构。

 

http://groovy-lang.org/structure.html

 

3.2. Script class

A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run method. The previous example is therefore compiled as if it was the following:

Main.groovy
import org.codehaus.groovy.runtime.InvokerHelper class Main extends Script {  def run() {  println 'Groovy world!'  } static void main(String[] args) {  InvokerHelper.runScript(Main, args)  } }
 The Main class extends the groovy.lang.Script class
 groovy.lang.Script requires a run method returning a value
 the script body goes into the run method
 the main method is automatically generated
 and delegates the execution of the script on the run method

If the script is in a file, then the base name of the file is used to determine the name of the generated script class. In this example, if the name of the file is Main.groovy, then the script class is going to be Main.

3.3. Methods

It is possible to define methods into a script, as illustrated here:

int fib(int n) { n < 2 ? 1 : fib(n-1) + fib(n-2) } assert fib(10)==89

You can also mix methods and code. The generated script class will carry all methods into the script class, and assemble all script bodies into the run method:

println 'Hello'                                 

int power(int n) { 2**n }  println "2^6==${power(6)}"
 script begins
 a method is defined within the script body
 and script continues

This code is internally converted into:

import org.codehaus.groovy.runtime.InvokerHelper class Main extends Script { int power(int n) { 2** n}  def run() { println 'Hello'  println "2^6==${power(6)}"  } static void main(String[] args) { InvokerHelper.runScript(Main, args) } }
 the power method is copied as is into the generated script class
 first statement is copied into the run method
 second statement is copied into the run method
 Even if Groovy creates a class from your script, it is totally transparent for the user. In particular, scripts are compiled to bytecode, and line numbers are preserved. This implies that if an exception is thrown in a script, the stack trace will show line numbers corresponding to the original script, not the generated code that we have shown.

 

3.4. Variables

Variables in a script do not require a type definition. This means that this script:

int x = 1 int y = 2 assert x+y == 3

will behave the same as:

x = 1 y = 2 assert x+y == 3

However there is a semantic difference between the two:

  • if the variable is declared as in the first example, it is a local variable. It will be declared in the run method that the compiler will generate and will not be visible outside of the script main body. In particular, such a variable will not be visible in other methods of the script

  • if the variable is undeclared, it goes into the script binding. The binding is visible from the methods, and is especially important if you use a script to interact with an application and need to share data between the script and the application. Readers might refer to the integration guide for more information.

 If you want a variable to become a field of the class without going into the Binding, you can use the @Field annotation.

 

转载于:https://www.cnblogs.com/lightsong/p/8605515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值