标签:style io ar color 使用 sp java 文件 on
package groovy;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
public class Java2GroovyShell {
public static void main(String[] args) {
// TODO Auto-generated method stub
Binding binding = new Binding();
binding.setVariable("var", 5);
GroovyShell gs = new GroovyShell(binding);
Object value = gs.evaluate("println ‘Hello Groovy !‘;abc=123;return var*10");//执行groovyshell脚本
System.out.println(value.equals(50));
System.out.println(binding.getVariable("abc").equals(123));
}
}
|
package groovy;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;
public class Java2GroovyDynamic {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ClassLoader parent = ClassLoader.getSystemClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("C:\\normandy_workspace\\groovy\\src\\groovy\\GroovyDemo.groovy"));
GroovyObject groovyObject = (GroovyObject)groovyClass.newInstance();
Object[] param = {123,321};
int res = (int) groovyObject.invokeMethod("add", param);
System.out.println("res="+res);
}
}
|
package groovy
class GroovyDemo {
static int add(int x,int y) {
return x+y;
}
static main(args) {
int z = add(123,321);
println("x+y="+z);
}
}
|
package groovy;
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
public class Java2GroovyEngine {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String path = "C:\\normandy_workspace\\groovy\\src\\groovy\\";
GroovyScriptEngine gse = new GroovyScriptEngine(path);
Binding binding = new Binding();
binding.setVariable("input", "Groovy");
gse.run("Test.groovy", binding);
System.out.println(binding.getVariable("output"));
}
}
|
package groovy;
output = "Hello ${input}";
|
标签:style io ar color 使用 sp java 文件 on
原文地址:http://blog.csdn.net/simonchi/article/details/41516053