[英]How to dynamically create a Groovy Closure from a String in Java
我想知道如何在Java应用程序中在运行时创建Closure
对象,其中Closure的内容未提前知道。 我找到了解决方案,但我怀疑它是最佳的。
背景:我编写了一些解析域特定语言的Groovy代码。 解析代码是静态编译的,并包含在Java应用程序中。 在解析器实现中,我有类充当DSL的特定部分的委托。 使用以下模式调用这些类:
class DslDelegate {
private Configuration configuration
def section(@DelegatesTo(SectionDelegate) Closure cl) {
cl.delegate = new SectionDelegate(configuration)
cl.resolveStrategy = Closure.DELEGATE_FIRST
cl()
}
}
我希望直接从Java代码中调用这样的方法。 我能够创建一个新的DslDelegate
对象,然后调用section()
方法。 但是我需要创建并传递一个参数,该参数是Closure
一个实例。 我希望从String
对象初始化内容。
我的解决方案:以下Java代码(实用程序)正在运行,但我要求改进。 当然,这可以更清洁或更有效的方式完成吗?
/**
* Build a Groovy Closure dynamically
*
* @param strings
* an array of strings for the text of the Closure
* @return a Groovy Closure comprising the specified text from {@code strings}
* @throws IOException
*/
public Closure<?> buildClosure(String... strings) throws IOException {
Closure<?> closure = null;
// Create a method returning a closure
StringBuilder sb = new StringBuilder("def closure() { { script -> ");
sb.append(String.join("\n", strings));
sb.append(" } }");
// Create an anonymous class for the method
GroovyClassLoader loader = new GroovyClassLoader();
Class<?> groovyClass = loader.parseClass(sb.toString());
try {
// Create an instance of the class
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
// Invoke the object's method and thus obtain the closure
closure = (Closure<?>) groovyObject.invokeMethod("closure", null);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
} finally {
loader.close();
}
return closure;
}
您可以使用GroovyShell
从字符串创建Closure
:
public Closure<?> buildClosure(String... strings) {
String scriptText = "{ script -> " + String.join("\n", strings) + " }";
return (Closure<?>) new GroovyShell().evaluate(scriptText);
}
感谢@hzpz,我已经完成了类似的任务,但我让它变得更美观,更易于使用。 在我的情况下,闭包可能接受任何参数,所以我把参数列表放在闭包s code. Let
s code. Let
在String中动态创建闭包,看起来像这样:
script1 = 'out,a,b,c-> out.println "a=${a}; b=${b}; c=${c}"; return a+b+c;'
现在,在String
类中创建新方法
String.metaClass.toClosure = {
return (Closure) new GroovyShell().evaluate("{${delegate}}")
}
现在我可以从String或文件或其他任何东西调用闭包。
println script1.toClosure()(out,1,2,3)
要么
println (new File('/folder/script1.groovy')).getText('UTF-8').toClosure()(out,1,2,3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.