如何在 Java 中从字符串动态创建 Groovy 闭包

mle*_*ard 6 java groovy closures

我想知道如何Closure在 Java 应用程序中在运行时创建一个对象,其中闭包的内容事先是未知的。我找到了一个解决方案,但我怀疑它是否是最佳的。

背景:我编写了一些解析领域特定语言的 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()
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望直接从 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;
}
Run Code Online (Sandbox Code Playgroud)

hzp*_*zpz 3

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

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