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:
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 scriptif 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. |
Groovy的脚本统一于类的世界的更多相关文章
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义 ...
- 远程shell脚本执行工具类
/** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = ...
- springboot统一异常处理类及注解参数为数组的写法
统一异常处理类 package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.commo ...
- Android 开发工具类 10_Toast 统一管理类
Toast 统一管理类: 1.短时间显示Toast: 2.长时间显示 Toast: 3.自定义显示 Toast 时间. import android.content.Context; import a ...
- Android 开发工具类 05_Logcat 统一管理类
Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...
- Log统一管理类
import android.util.Log; /** * Log统一管理类 */ public class L{ private L(){ /* cannot be instantiated */ ...
- IDEA使用 live template添加groovy脚本给方法,类,js方法添加注释(转载)
IDEA添加Live Template: File->Setting->Editor->Live Templates Abbreviation: * Template text: * ...
- 用 CallerMemberName Attribute 和 EqualityComparer 统一处理类的属性值变化
当需要实现类似 INotifyPropertyChanged 这样的接口的时候,每一个属性去判断值是否变化,然后触发事件什么的,太麻烦了,如果能用一个方法统一处理就好了. 好在真的可以做到.这个博文说 ...
- javascript王国的一次旅行,一个没有类的世界怎么玩转面向对象?
1. 前言 作为Java 帝国的未来继承人,Java小王子受到了严格的教育, 不但精通Java语言.Java虚拟机.java类库和框架,还对各种官方的Java规范了如指掌. 近日他听说一个叫做Java ...
随机推荐
- 每天一个linux命令(02):route命令
route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现. 在L ...
- [NOI2010]航空管制(拓扑排序+贪心)
题目描述 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上,小X不幸又一 ...
- Spring -- 自定义转换器
Spring 定义了 3 种类型的转换器接口,实现任意一个转换器接口都可以作为自定义转换器注册到 ConversionServiceFactoryBean 中: Converter<S,T> ...
- hbuilder 个推 问题记录
截止版本日期: 2017/09/19 一,安卓没有角标问题: Android - 2.2+ (支持): 目前仅支持小米(MIUI v5),其它设备调用后无任何效果.官方文档 二,ios角标混乱问题: ...
- C# winform TreeView中关于checkbox选择的完美类[转]
http://www.cnblogs.com/kingangWang/archive/2011/08/15/2139119.html public static class TreeViewCheck ...
- c++ sort
老是搞混 return bool eg. bool cmp(node a,node b) { if (a.score==b.score) ; else return a.score>b.scor ...
- SpringBoot整合Freemarker+Mybatis
开发工具 , 开始 新建工程 .选择Spring Initializr 下一步 下一步,选择需要的组件 ..改一下工程名,Finish ..目录结构 首先,修改pom文件 然后,将applicatio ...
- mysql5.7 on windows
1.下载zip包:https://dev.mysql.com/downloads/file/?id=476696 2.解压到E盘3.执行命令 初始化 E:/mysql-5.7.22-winx64/bi ...
- Spring入门初体验
Spring其实就是一个容器,让我们更方便的拿到想要的对象. 1.编写一个service // userService.java public interface userService { publ ...
- Spring Boot笔记五: Web开发之Webjar和静态资源映射规则
目录 Webjar /** 访问当前项目的任何资源 欢迎页 标签页图标 Webjar 开始讲到Spring Boot的Web开发了,先介绍Webjar,这个其实就是把一些前端资源以jar包的形式导入到 ...