Java中执行groovy脚本的方式
2020-10-16 07:36:01.0
作为groovy类执行
作为groovy类执行:加载groovy类之后,通过反射的方式实例化,并调用指定的方法,返回结果。实例如下
private Object execGroovy(String groovyCode, String methodName, Object... params) {
try (GroovyClassLoader classLoader = new GroovyClassLoader()) {
Class groovyClass = classLoader.parseClass(groovyCode);
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Method method = null;
try {
method = groovyClass.getDeclaredMethod(methodName);
} catch (NoSuchMethodException e) {
LOGGER.warn("Groovy 脚本中不存在" + methodName + "方法");
}
if (method == null) {
method = groovyClass.getDeclaredMethods()[2];
}
return method.invoke(groovyObject, params);
} cat
最后
以上就是等待往事最近收集整理的关于java groovy脚本_Java中执行groovy脚本的方式的全部内容,更多相关java内容请搜索靠谱客的其他文章。
发表评论 取消回复