groovy/groovy-core — pull request #151 by kohsuke (2013-03-30 00:08:26+00:00)
Title: Added ClosureScript class

This is a convenient `Script` subtype that performs method invocations and property access like `Closure` does.

For example, when the script is:

a = 1;
b(2);

Using `ClosureScript` as the base class would run it as:

delegate.a = 1;
delegate.b(2);

... whereas in plain `Script`, this will be run as:

binding.setProperty("a",1);
((Closure)binding.getProperty("b")).call(2);

This makes it a convenient basis for loading a custom-defined DSL as a `Script`, then execute it.
The following sample code illustrates how to do it:

class MyDSL {
public void foo(int x, int y, Closure z) { ... }
}

CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(ClosureScript.class.getName());
GroovyShell sh = new GroovyShell(cl,new Binding(),cc);
ClosureScript script = (ClosureScript)sh.parse(new File("my.dsl"))
script.setDelegate(new MyDSL());
script.run();

my.dsl can look like this:

foo(1,2) {
....
}

I find this useful in a number of places (for example in Jenkins where we define several Groovy DSLs), so I'm trying to push this back to the upstream.