Bind a java class as a closure into a groovy-script

Posted by chrsk on Stack Overflow See other posts from Stack Overflow or by chrsk
Published on 2010-03-30T06:58:52Z Indexed on 2010/03/30 7:03 UTC
Read the original article Hit count: 390

Filed under:
|
|
|

Is it possible to bind a closure written in java into a groovy-script. Is there an interface or something to implement so i can provide a closure?

Something like this?

public class Example implements Closure {
   public void closure(Object... args) {
       System.out.println(args[0]);
   }
}

Bind this into the groovyscript.

Binding binding = new Binding();
binding.put("example", new Example());
groovyScriptEngine.run("foo.groovy", binding)

and use it in the foo.groovy like this:

example("Hello World")

© Stack Overflow or respective owner

Related posts about groovy

Related posts about binding