Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: renamed variable "compiler" to "configuration" as this reflects the behavior in a better way

...

You can evaluate any expression or script in Groovy using the GroovyShell.
The GroovyShell allows you to pass in and out variables via the Binding object.

Code Block
java
java

// call groovy expressions from Java code
Binding binding = new Binding();
binding.setVariable("foo", new Integer(2));
GroovyShell shell = new GroovyShell(binding);

Object value = shell.evaluate("println 'Hello World!'; x = 123; return foo * 10");
assert value.equals(new Integer(20));
assert binding.getVariable("x").equals(new Integer(123));

...

It is often useful to make your groovy scripts extend a base class of your choosing so that the script can access common methods. This can be achieved by setting the script base class property on the compile configuration and passing this new compiler configuration to the shell.

Code Block
java
java

class ScriptBaseTest {

  @Test
  void extend_groovy_script() {
    def compilerconfiguration = new CompilerConfiguration()
    compilerconfiguration.setScriptBaseClass("ScriptBaseTestScript")

    def shell = new GroovyShell(this.class.classLoader, new Binding(), compilerconfiguration)

    assertEquals shell.evaluate("foo()"), "this is foo"
  }
}

abstract class ScriptBaseTestScript extends Script {
  def foo() {
    "this is foo"
  }
}

...

You can use the GroovyClassLoader to load classes dynamically into a Java program and execute them (or use them) directly. The following Java code shows an example:

Code Block
java
java

ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("src/test/groovy/script/HelloWorld.groovy"));

// let's call some method on an instance
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object[] args = {};
groovyObject.invokeMethod("run", args);

If you have an interface you wish to use which you implement in the Groovy script you can use it as follows:

Code Block
java
java

GroovyClassLoader gcl = new GroovyClassLoader();
Class clazz = gcl.parseClass(myStringwithGroovyClassSource, "SomeName.groovy");
Object aScript = clazz.newInstance();
MyInterface myObject = (MyInterface) aScript;
myObject.interfaceMethod();
  ...

...

One thing to remember is that the parseClass will try to create an object from your String fileName.  Another way to do the gcl.parseClass is:

Code Block

Class clazz = gcl.parseClass(new File("SomeName.groovy");

Full Example:

Code Block

TestInterface.java
public interface TestInterface {
    public void printIt();
}

Tester.groovy
public class Tester implements TestInterface {
    public void printIt() {
        println "this is in the test class";
    }
}

TestClass.java -- inside of a method
String fileName = "Tester.groovy";
GroovyClassLoader gcl = new GroovyClassLoader();
Class clazz = gcl.parseClass(new File(fileName));
Object aScript = clazz.newInstance();

TestInterface ifc = (TestInterface) aScript;
ifc.printIt();

...

/my/groovy/script/path/hello.groovy:

Code Block
java
java

output = "Hello, ${input}!"
Code Block
java
java

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;

String[] roots = new String[] { "/my/groovy/script/path" };
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
Binding binding = new Binding();
binding.setVariable("input", "world");
gse.run("hello.groovy", binding);
System.out.println(binding.getVariable("output"));

...