Java 调用 Groovy脚本
引入 groovy 依赖
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.8</version>
<type>pom</type>
</dependency>
方式一:执行字符串脚本,Invocable#invokeFunction
//自定义一些方法
public class DefaultFunction {
public int add(int a, int b) {
System.out.println("执行内部逻辑");
return a + b;
}
public int add() {
System.out.println("执行内部逻辑");
return 10;
}
}
public class TestScriptEngine {
public static void main(String[] args) {
TestScriptEngine engine = new TestScriptEngine();
engine.testByFunction();
}
public void testByFunction() {
//查找并创建指定脚本引擎
ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
/**
* 初始化Bindings
* bindings 中的对象,会被当做脚本全局对象
*/
Bindings bindings = engine.createBindings();
// 绑定参数
bindings.put("date", new Date());
bindings.put("option", new DefaultFunction());
/**
* 定义groovy脚本内容
* grovvy中的字符串可以识别${}和$占位符
*/
final String scriptContent = "def execute(param){" +
" println(\"param: $param\");" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" println(\"add() is: ${option.add()}\");" +
" println(\"add(1,2) is: ${option.add(1,2)}\");" +
" return date.getTime() > 0;" +
"}";
try {
// 执行脚本
engine.eval(scriptContent, bindings);
// 获取执行结果
Invocable invocable = (Invocable) engine;
// 方法入参
final String param = "方法入参";
/**
* 第一个参数:脚本方法名称
* 第二个参数:脚本方法入参
*/
Boolean flag = (Boolean) invocable.invokeFunction("execute", param);
System.out.println("---------------------------------------");
System.out.println("result is: " + flag);
} catch (ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
}
}
方式二:执行groovy对象,Invocable#invokeMethod
class TestGroovy {
def execute(date,option,param) {
println("param: $param");
println("now dateTime is: ${date.getTime()}");
println("add() is: ${option.add()}");
println("add(1,2) is: ${option.add(1,2)}");
return date.getTime() > 0;
}
}
public class TestScriptEngine {
// 查找并创建指定脚本引擎
private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
public static void main(String[] args) {
TestScriptEngine engine = new TestScriptEngine();
engine.testByMethod();
}
public class DefaultFunction {
public int add(int a, int b) {
System.out.println("执行内部逻辑");
return a + b;
}
public int add() {
System.out.println("执行内部逻辑");
return 10;
}
}
public void testByMethod(){
try {
// 初始化groovy脚本对象
final TestGroovy testGroovy = new TestGroovy();
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义参数
final Date arg_1 = new Date();
final DefaultFunction arg_2 = new DefaultFunction();
final String arg_3 = "groovy";
// 执行脚本并获取结果
Invocable invocable = (Invocable) engine;
/**
* 1 脚本对象
* 2 脚本方法名
* 3 ...脚本方法参数
*/
Boolean flag = (Boolean) invocable.invokeMethod(testGroovy, scriptName, arg_1, arg_2, arg_3);
System.out.println("---------------------------------------");
System.out.println("result is: " + flag);
} catch (ScriptException |NoSuchMethodException e) {
e.printStackTrace();
}
}
}
方式三:GroovyClassLoader
public class TestScriptEngine {
public class DefaultFunction {
public int add(int a, int b) {
System.out.println("执行内部逻辑");
return a + b;
}
public int add() {
System.out.println("执行内部逻辑");
return 10;
}
}
// groovy 脚本
private final static String scriptContent = "def execute(date,param){" +
" println(\"param: $param\");" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" println(\"add() is: ${option.add()}\");" +
" println(\"add(1,2) is: ${option.add(1,2)}\");" +
" return date.getTime() > 0;" +
"}";
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
TestScriptEngine engine = new TestScriptEngine();
engine.groovyClassLoaderTest();
}
public void groovyClassLoaderTest(){
GroovyClassLoader classLoader = new GroovyClassLoader();
//解析成Java类
Class groovyClass = classLoader.parseClass(scriptContent);
try {
//创建实例
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
//设置脚本中的全局类
groovyObject.setProperty("option",new DefaultFunction());
//执行指定脚本方法
Object o = groovyObject.invokeMethod("execute",new Object[]{new Date(), "param参数"} );
System.out.println("结果:"+(boolean) o);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
方式四:ScriptEngine
public class TestScriptEngine {
public class DefaultFunction {
public int add(int a, int b) {
System.out.println("执行内部逻辑");
return a + b;
}
public int add() {
System.out.println("执行内部逻辑");
return 10;
}
}
// groovy 脚本
private final static String scriptContent = "def execute(date,param){" +
" println(\"param: $param\");" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" println(\"add() is: ${option.add()}\");" +
" println(\"add(1,2) is: ${option.add(1,2)}\");" +
" return date.getTime() > 0;" +
"}";
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
TestScriptEngine engine = new TestScriptEngine();
engine.scriptEngineTest();
}
public void scriptEngineTest() throws ScriptException, NoSuchMethodException {
GroovyScriptEngineFactory scriptEngineFactory = new GroovyScriptEngineFactory();
ScriptEngine scriptEngine = scriptEngineFactory.getScriptEngine();
Bindings bindings = new SimpleBindings();
bindings.put("option", new DefaultFunction());
scriptEngine.setBindings(bindings,200);
scriptEngine.eval(scriptContent);
boolean execute = (boolean) ((Invocable) scriptEngine).invokeFunction("execute", new Date(), "param参数");
System.out.println("结果:"+execute);
}
}
方式五:GroovyShell
public class TestScriptEngine {
public class DefaultFunction {
public int add(int a, int b) {
System.out.println("执行内部逻辑");
return a + b;
}
public int add() {
System.out.println("执行内部逻辑");
return 10;
}
}
// groovy 脚本
private final static String scriptContent = "def execute(date,param){" +
" println(\"param: $param\");" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" println(\"add() is: ${option.add()}\");" +
" println(\"add(1,2) is: ${option.add(1,2)}\");" +
" return date.getTime() > 0;" +
"}";
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
TestScriptEngine engine = new TestScriptEngine();
engine.groovyShellTest();
}
public void groovyShellTest() throws ScriptException, NoSuchMethodException {
GroovyShell groovyShell = new GroovyShell();
groovyShell.setProperty("option", new DefaultFunction());
Script script= groovyShell.parse(scriptContent);
boolean execute = (boolean) InvokerHelper.invokeMethod(script, "execute",new Object[] {new Date(), "param参数"});
System.out.println("结果:"+execute);
}
}