5

我有 3 个关于在 java 中使用 Groovy 的问题。它们都是相关的,所以我在这里只提出一个问题。

1)有:GroovyClassLoader、GroovyShell、GroovyScriptEngine。但是使用它们有什么区别呢?

例如对于此代码:

static void runWithGroovyShell() throws Exception {
    new GroovyShell().parse(new File("test.groovy")).invokeMethod("hello_world", null);
}

static void runWithGroovyClassLoader() throws Exception {
    Class scriptClass = new GroovyClassLoader().parseClass(new File("test.groovy"));
    Object scriptInstance = scriptClass.newInstance();
    scriptClass.getDeclaredMethod("hello_world", new Class[]{}).invoke(scriptInstance, new Object[]{});
}

static void runWithGroovyScriptEngine() throws Exception {
    Class scriptClass = new GroovyScriptEngine(".").loadScriptByName("test.groovy");
    Object scriptInstance = scriptClass.newInstance();
    scriptClass.getDeclaredMethod("hello_world", new Class[]{}).invoke(scriptInstance, new Object[]{});
}

2)加载groovy脚本的最佳方法是什么,以便它以编译形式保留在内存中,然后我可以在需要时调用该脚本中的函数。

3)如何将我的 java 方法/类公开给 groovy 脚本,以便它可以在需要时调用它们?

4

1 回答 1

2

方法 2 和 3 都返回解析class后的返回值。map因此,一旦解析并成功加载它们,您就可以使用 a将它们保存在内存中。

Class scriptClass = new GroovyClassLoader().parseClass(new File("test.groovy"));
map.put("test.groovy",scriptClass);

更新:

GroovyObject链接到 groovy 对象文档。

这也可以直接将对象转换为 GroovyObject 和其他 java 类是无法区分的。

Object aScript = clazz.newInstance();
MyInterface myObject = (MyInterface) aScript;
myObject.interfaceMethod();
 //now here you can also cache the object if you want to

无法评论效率。但我想如果你把加载的类保存在内存中,一次解析不会有太大的伤害。

更新为了提高效率:您应该使用GroovyScriptEngine,它在内部使用脚本缓存

这是链接:Groovy 脚本引擎

否则,您始终可以自己使用一些性能基准测试它,您会得到粗略的想法。例如:在三个不同的循环中使用所有三种方法编译 groovy 脚本,看看哪个性能更好。尝试使用相同和不同的脚本,以查看缓存是否以某种方式启动。

向脚本传递参数和从脚本传递参数的更新 绑定类将帮助您向脚本发送参数和从脚本发送参数。

示例链接

// setup binding
def binding = new Binding()
binding.a = 1
binding.setVariable('b', 2)
binding.c = 3
println binding.variables

// setup to capture standard out
def content = new StringWriter()
binding.out = new PrintWriter(content)

// evaluate the script
def ret = new GroovyShell(binding).evaluate('''
def c = 9
println 'a='+a
println 'b='+b
println 'c='+c 
retVal = a+b+c
a=3
b=2
c=1
''')

// validate the values
assert binding.a == 3
assert binding.getVariable('b') == 2
assert binding.c == 3 // binding does NOT apply to def'd variable
assert binding.retVal == 12 // local def of c applied NOT the binding!

println 'retVal='+binding.retVal
println binding.variables
println content.toString()
于 2012-12-07T16:59:24.180 回答