/** * Can be used to customize the environment in which the script runs. */ protected GroovyShell createShell() { return new GroovyShell(loader, bindings); }
public GroovyShell getShell() { if (shell == null) { shell = new GroovyShell(); } return shell; }
/** * @return a newly created GroovyShell using the same variable scope but a new class loader */ protected GroovyShell getEvalShell() { return new GroovyShell(shell); } }
/** * 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())); } }
private GroovyShell createShell() { CompilerConfiguration compilerSettings = new CompilerConfiguration(); compilerSettings.setScriptBaseClass(DelegatingScript.class.getName()); return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings); } }
@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(); } }
/** * Parses the bean definition groovy script by first exporting the given {@link Binding}. */ public void parse(InputStream script, Binding binding) { if (script==null) throw new IllegalArgumentException("No script is provided"); setBinding(binding); CompilerConfiguration cc = new CompilerConfiguration(); cc.setScriptBaseClass(ClosureScript.class.getName()); GroovyShell shell = new GroovyShell(classLoader,binding,cc); ClosureScript s = (ClosureScript)shell.parse(new InputStreamReader(script)); s.setDelegate(this); s.run(); }
public void testMe () { new GroovyShell().evaluate("groovy.bugs.Autobox.Util.printByte(\"1\", Byte.valueOf((byte)1));"); new GroovyShell().evaluate("groovy.bugs.Autobox.Util.printByte(\"1\", (byte)1);"); } }
public XmlTemplateEngine(String indentation, boolean validating) throws SAXException, ParserConfigurationException { this(new XmlParser(validating, true), new GroovyShell()); this.xmlParser.setTrimWhitespace(true); setIndentation(indentation); }
public static void main(String[] args) { try { GroovyShell shell = new GroovyShell(); //shell.run("src/main/org/codehaus/groovy/tools/DocGenerator.groovy", "org.codehaus.groovy.tools.DocGenerator.groovy", args); shell.run(new File("src/main/org/codehaus/groovy/tools/DocGenerator.groovy"), args); } catch (Exception e) { System.out.println("Failed: " + e); e.printStackTrace(); } } }
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 testScriptNameMangling() { String script = "this.getClass().getName()"; GroovyShell shell = new GroovyShell(); String name = (String) shell.evaluate(script,"a!b"); assertEquals("a_b", name); }
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()); } }
GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");
protected int run() throws Exception { // this allows the caller to manipulate the JVM state, so require the execute script privilege. Jenkins.getActiveInstance().checkPermission(Jenkins.RUN_SCRIPTS); Binding binding = new Binding(); binding.setProperty("out",new PrintWriter(stdout,true)); binding.setProperty("stdin",stdin); binding.setProperty("stdout",stdout); binding.setProperty("stderr",stderr); binding.setProperty("channel",channel); if (channel != null) { String j = getClientEnvironmentVariable("JOB_NAME"); if (j != null) { Item job = Jenkins.getActiveInstance().getItemByFullName(j); binding.setProperty("currentJob", job); String b = getClientEnvironmentVariable("BUILD_NUMBER"); if (b != null && job instanceof AbstractProject) { Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b)); binding.setProperty("currentBuild", r); } } } GroovyShell groovy = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, binding); groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[remaining.size()])); return 0; }
/** * 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); }
public void test_StaticImport1() throws Exception { //GROOVY-3711: The following call now results in a valid script class node, so foo.Bar needs to get resolved. GroovyShell groovyShell = new GroovyShell(); compile("package foo; class Bar{}", groovyShell); assertNotNull(compile("import static foo.Bar.mooky", groovyShell)); }
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]); }