如何从Java

时间:2016-04-21 13:43:20

标签: java groovy closures

我想知道如何在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;
}

2 个答案:

答案 0 :(得分:3)

您可以使用GroovyShell从字符串中创建Closure

public Closure<?> buildClosure(String... strings) {
    String scriptText = "{ script -> " + String.join("\n", strings) + " }";
    return (Closure<?>) new GroovyShell().evaluate(scriptText);
}

答案 1 :(得分:0)

感谢@hzpz,我已经完成了类似的任务,但是我让它变得更漂亮,更易于使用。在我的情况下,闭包可能会接受任何参数,所以我把参数列表放到闭包s code. Let s表示在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)
相关问题