import org.scripthelper.context.ScriptContext; import org.scripthelper.context.DefaultScriptContext; import <the_script_interface_classpath>;
class GroovyClass implements + className { <context_class_path> context;Where <context_class_path> is the classpath of the Script context class, and
className
is whatever name you want to use to create your Groovy class.
public void init(ScriptContext ctx) { context = <context_class_path>ctx; }Where <context_class_path> is the classpath of the Script context class.
protected Object createScriptObject() throws Exception { // compile the script with the InvokeDynamic optimization CompilerConfiguration config = new CompilerConfiguration(); Map<String, Boolean> optim = config.getOptimizationOptions(); config.setTargetBytecode(CompilerConfiguration.JDK8); optim.put("indy", Boolean.TRUE); GroovyClassLoader groovyLoader = new GroovyClassLoader(getClassLoader(), config); Class<?> groovyClass = groovyLoader.parseClass(scriptContent); GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); return groovyObject; }
Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence