/** * A helper method to allow the dynamic evaluation of groovy expressions using this * scripts binding as the variable scope * * @param expression is the Groovy script expression to evaluate */ public Object evaluate(String expression) throws CompilationFailedException { GroovyShell shell = new GroovyShell(getClass().getClassLoader(), binding); return shell.evaluate(expression); }
/** * Loads a set of given beans * @param resources The resources to load * @throws IOException */ public void loadBeans(Resource[] resources) throws IOException { Closure beans = new Closure(this){ @Override public Object call(Object... args) { return beans((Closure)args[0]); } }; Binding b = new Binding(); b.setVariable("beans", beans); GroovyShell shell = classLoader != null ? new GroovyShell(classLoader,b) : new GroovyShell(b); for (Resource resource : resources) { shell.evaluate(new InputStreamReader(resource.getInputStream())); } }
protected void execute(GroovyCodeSource s) { try { createShell().evaluate(s); } catch (RuntimeException x) { LOGGER.log(WARNING, "Failed to run script " + s.getName(), x); } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { GroovyShell groovyShell = new GroovyShell( this.classLoader, new Binding(arguments), this.compilerConfiguration); try { String filename = (script instanceof ResourceScriptSource ? ((ResourceScriptSource) script).getResource().getFilename() : null); if (filename != null) { return groovyShell.evaluate(script.getScriptAsString(), filename); } else { return groovyShell.evaluate(script.getScriptAsString()); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access Groovy script", ex); } catch (GroovyRuntimeException ex) { throw new ScriptCompilationException(script, ex); } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { GroovyShell groovyShell = new GroovyShell( this.classLoader, new Binding(arguments), this.compilerConfiguration); try { String filename = (script instanceof ResourceScriptSource ? ((ResourceScriptSource) script).getResource().getFilename() : null); if (filename != null) { return groovyShell.evaluate(script.getScriptAsString(), filename); } else { return groovyShell.evaluate(script.getScriptAsString()); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access Groovy script", ex); } catch (GroovyRuntimeException ex) { throw new ScriptCompilationException(script, ex); } }
public String call() throws RuntimeException { // if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader. if (cl==null) cl = Thread.currentThread().getContextClassLoader(); CompilerConfiguration cc = new CompilerConfiguration(); cc.addCompilationCustomizers(new ImportCustomizer().addStarImports( "jenkins", "jenkins.model", "hudson", "hudson.model")); GroovyShell shell = new GroovyShell(cl,new Binding(),cc); StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out); shell.setVariable("out", pw); try { Object output = shell.evaluate(script); if(output!=null) pw.println("Result: "+output); } catch (Throwable t) { Functions.printStackTrace(t, pw); } return out.toString(); } }
public void testBug() throws Exception { String code = "for (i in 1..10) \n{\n println(i)\n}"; GroovyShell shell = new GroovyShell(); shell.evaluate(code); }
protected Object evaluate(String text) { String name = "Script" + counter++; try { return getShell().evaluate(text, name); } catch (Exception e) { handleException(text, e); return null; } }
/** * Execute a script. */ public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { try { // use evaluate to pass in the BSF variables source = convertToValidJavaClassname(source); getEvalShell().evaluate(script.toString(), source); } catch (Exception e) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); } }
/** * Evaluate an expression. */ public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException { try { source = convertToValidJavaClassname(source); return getEvalShell().evaluate(script.toString(), source); } catch (Exception e) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); } }
public void testScriptNameMangling() { String script = "this.getClass().getName()"; GroovyShell shell = new GroovyShell(); String name = (String) shell.evaluate(script,"a!b"); assertEquals("a_b", name); }
public void testClassLoader() { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(DerivedScript.class.getName()); GroovyShell shell = new GroovyShell(context, config); String script = "evaluate '''\n"+ "class XXXX{}\n"+ "assert evaluate('XXXX') == XXXX\n"+ "'''"; shell.evaluate(script); }
public void testExecuteScript() { GroovyShell shell = new GroovyShell(); try { Object result = shell.evaluate(script1, "Test.groovy"); assertEquals(new Integer(1), result); } catch (Exception e) { fail(e.toString()); } }
public void testWithGCSWithURL() throws Exception { String scriptFileName = "src/test/groovy/bugs/GROOVY3934Helper.groovy"; File helperScript = new File(scriptFileName); if(!helperScript.exists()) { fail("File " + scriptFileName + " does not exist"); } else { URL url = helperScript.toURI().toURL(); GroovyCodeSource gcs = new GroovyCodeSource(url); GroovyShell shell = new GroovyShell(); Object result = shell.evaluate(gcs); assertEquals("GROOVY3934Helper script called", result); } }
/** * Test for GROOVY-6615 * @throws Exception */ public void testScriptWithCustomBodyMethod() throws Exception { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(BaseScriptCustomBodyMethod.class.getName()); GroovyShell shell = new GroovyShell(context, config); Object result = shell.evaluate("'I like ' + cheese"); assertEquals("I like Cheddar", result); }
try { GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");
public void testExecuteScriptWithContext() { Binding context = new Binding(); context.setVariable("test", new PropertyHolder()); GroovyShell shell = new GroovyShell(context); try { Object result = shell.evaluate(script2, "Test.groovy"); assertEquals(new Integer(2), result); } catch (Exception e) { fail(e.toString()); } }
public void testScriptWithDerivedBaseClass() throws Exception { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(DerivedScript.class.getName()); GroovyShell shell = new GroovyShell(context, config); Object result = shell.evaluate("x = 'abc'; doSomething(cheese)"); assertEquals("I like Cheddar", result); assertEquals("abc", context.getVariable("x")); }
public void testClassPropsGroovy() { Object testObject = new GroovyShell().evaluate("class Test {def meth1(a,b){}}\nreturn new Test()"); Inspector insp = new Inspector(testObject); String[] classProps = insp.getClassProps(); assertEquals("package n/a", classProps[Inspector.CLASS_PACKAGE_IDX]); assertEquals("public class Test", classProps[Inspector.CLASS_CLASS_IDX]); assertEquals("implements GroovyObject ", classProps[Inspector.CLASS_INTERFACE_IDX]); assertEquals("extends Object", classProps[Inspector.CLASS_SUPERCLASS_IDX]); assertEquals("is Primitive: false, is Array: false, is Groovy: true", classProps[Inspector.CLASS_OTHER_IDX]); }