Script interface, and run a Groovy Script as a class which implements this interface from Java.
public interface Script { public int execute(); }
public class GroovyScriptWrapperImpl extends GroovyScriptWrapper<Script> { }With this definition, we have defined that our wrapper wrap scripts which implement the
Script interface.
execute() method. This method has an int return value, so we will extend the AbstractScriptMethodProxy class as follows:public class MethodWrapperImpl extends AbstractScriptMethodWrapper<Script, Integer> { public MethodWrapperImpl(ScriptWrapper<Script> wrapper) { super(wrapper); } protected Integer executeScriptImpl(Object[] arguments) { return script.execute(); } }
ScriptWrapper<Script> wrapper = new GroovyScriptWrapperImpl();Then we create the wrapper around the
execute() method[1]
ScriptMethodWrapper after the script has been installed, you can do it as soon as you have created your ScriptWrapperScriptMethodWrapper<Script, Integer> methodWrapper = new MethodWrapperImpl(wrapper);We have the following Groovy script:
public int execute() { return 10; }We can now install the Script:
File file = new File(<our script file>); wrapper.installScript(file);And now we can run the method:
int value = methodWrapper.executeMethod(); // value is 10
ScriptMethodWrapper after the script has been installed, you can do it as soon as you have created your ScriptWrapperCopyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence