Groovy超强的Java动态计算能力
程序员文章站
2022-05-31 16:03:47
...
//Groovy超强的Java动态计算能力 groovy-2.5.12.jar apache 表达式语言
public class GroovyTest {
public static void main(String[] args) {
GroovyShell shell = new GroovyShell();
Object result = shell.evaluate("(1+5)/2");
System.out.println(result);
Binding binding = new Binding();
binding.setVariable("p",3.1415926f);
binding.setVariable("r",5);
GroovyShell shell2 = new GroovyShell(binding);
result = shell2.evaluate("p*r*r");
System.out.println(result);
result = shell2.evaluate("Math.round(p*r*r)");
System.out.println(result);
String s = "2>3||3>4&&5<4";
result = shell.evaluate(s);
System.out.println(result);
// String javacode="SimpleDateFormat.getDateInstance().format(new Date())";
// result = shell.evaluate(javacode);
// System.out.println(result);
//shell.parse(new File(//))
Script script = shell2.parse("def join(String[] list) {return list.join('--');}");
String joinString = (String)script.invokeMethod("join", new String[]{"A1","B2","C3"});
System.out.println(joinString);
//反射动态调用groovy类
GroovyTest g=new GroovyTest();
// g.java2groovy();
g.grooyScript();
//自定义函数
}
private void java2groovy() {
//获取 groovy 类的反射对象
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass=null;
try {
groovyClass = loader.parseClass(new File("G:\\neweclipse\\workspace\\bsh\\src\\bsh\\GroovyDemo.groovy"));
//实例化 GroovyDemo 类
GroovyObject groovyObj = (GroovyObject) groovyClass.newInstance();
//调用 groovyDemo 成员方法
System.out.println( groovyObj.invokeMethod("sayHello",null));
} catch (CompilationFailedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//自定义函数
void grooyScript() {
String path = "G:\\neweclipse\\workspace\\bsh\\src\\bsh\\";
GroovyScriptEngine gse;
try {
gse = new GroovyScriptEngine(path);
Binding binding = new Binding();
binding.setVariable("input", "Groovy");
gse.run("GroovyDemo.groovy", binding);
System.out.println(binding.getVariable("output"));
} catch (Exception e) {
e.printStackTrace();
}
}
//自定义函数
void cusFunction() {
Binding binding = new Binding();
// if(vars!=null) {
// vars.keySet().forEach(name ->
// binding.setVariable(name, vars.get(name))
// );
// }
CompilerConfiguration cfg = new CompilerConfiguration();
cfg.setScriptBaseClass(DateUtils.class.getName());
GroovyShell shell = new GroovyShell(binding,cfg);
try {
Script script = shell.parse(buildScript());
return script.run();
} catch (Exception ex) {
logger.error("catch script execution error, script:"+buildScript(), ex);
return 0;
}
}
}
public class GroovyTest {
public static void main(String[] args) {
GroovyShell shell = new GroovyShell();
Object result = shell.evaluate("(1+5)/2");
System.out.println(result);
Binding binding = new Binding();
binding.setVariable("p",3.1415926f);
binding.setVariable("r",5);
GroovyShell shell2 = new GroovyShell(binding);
result = shell2.evaluate("p*r*r");
System.out.println(result);
result = shell2.evaluate("Math.round(p*r*r)");
System.out.println(result);
String s = "2>3||3>4&&5<4";
result = shell.evaluate(s);
System.out.println(result);
// String javacode="SimpleDateFormat.getDateInstance().format(new Date())";
// result = shell.evaluate(javacode);
// System.out.println(result);
//shell.parse(new File(//))
Script script = shell2.parse("def join(String[] list) {return list.join('--');}");
String joinString = (String)script.invokeMethod("join", new String[]{"A1","B2","C3"});
System.out.println(joinString);
//反射动态调用groovy类
GroovyTest g=new GroovyTest();
// g.java2groovy();
g.grooyScript();
//自定义函数
}
private void java2groovy() {
//获取 groovy 类的反射对象
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass=null;
try {
groovyClass = loader.parseClass(new File("G:\\neweclipse\\workspace\\bsh\\src\\bsh\\GroovyDemo.groovy"));
//实例化 GroovyDemo 类
GroovyObject groovyObj = (GroovyObject) groovyClass.newInstance();
//调用 groovyDemo 成员方法
System.out.println( groovyObj.invokeMethod("sayHello",null));
} catch (CompilationFailedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//自定义函数
void grooyScript() {
String path = "G:\\neweclipse\\workspace\\bsh\\src\\bsh\\";
GroovyScriptEngine gse;
try {
gse = new GroovyScriptEngine(path);
Binding binding = new Binding();
binding.setVariable("input", "Groovy");
gse.run("GroovyDemo.groovy", binding);
System.out.println(binding.getVariable("output"));
} catch (Exception e) {
e.printStackTrace();
}
}
//自定义函数
void cusFunction() {
Binding binding = new Binding();
// if(vars!=null) {
// vars.keySet().forEach(name ->
// binding.setVariable(name, vars.get(name))
// );
// }
CompilerConfiguration cfg = new CompilerConfiguration();
cfg.setScriptBaseClass(DateUtils.class.getName());
GroovyShell shell = new GroovyShell(binding,cfg);
try {
Script script = shell.parse(buildScript());
return script.run();
} catch (Exception ex) {
logger.error("catch script execution error, script:"+buildScript(), ex);
return 0;
}
}
}