use of groovy.lang.GroovyShell in project qi4j-sdk by Qi4j.
the class GroovyMixin method invokeAsScript.
private Object invokeAsScript(Method method, Object[] args, URL groovySource) throws Throwable {
try {
Binding binding = new Binding();
binding.setVariable("This", me);
binding.setVariable("args", args);
GroovyShell shell = new GroovyShell(binding);
InputStream is = null;
try {
is = groovySource.openStream();
return shell.evaluate(new InputStreamReader(is));
} finally {
if (is != null) {
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
use of groovy.lang.GroovyShell in project hudson-2.x by hudson.
the class BeanBuilder method parse.
/**
* Parses the bean definition groovy script by first exporting the given {@link Binding}.
*/
public void parse(InputStream script, Binding binding) {
setBinding(binding);
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(ClosureScript.class.getName());
GroovyShell shell = new GroovyShell(classLoader, binding, cc);
ClosureScript s = (ClosureScript) shell.parse(script);
s.setDelegate(this);
s.run();
}
use of groovy.lang.GroovyShell in project hudson-2.x by hudson.
the class Combination method evalGroovyExpression.
/**
* Evaluates the given Groovy expression with values bound from this combination.
*
* <p>
* For example, if this combination is a=X,b=Y, then expressions like <tt>a=="X"</tt> would evaluate to
* true.
*/
public boolean evalGroovyExpression(AxisList axes, String expression) {
if (Util.fixEmptyAndTrim(expression) == null)
return true;
Binding binding = new Binding();
for (Map.Entry<String, String> e : entrySet()) binding.setVariable(e.getKey(), e.getValue());
binding.setVariable("index", toModuloIndex(axes));
binding.setVariable("uniqueId", toIndex(axes));
GroovyShell shell = new GroovyShell(binding);
Object result = shell.evaluate("use(" + BooleanCategory.class.getName().replace('$', '.') + ") {" + expression + "}");
return TRUE.equals(result);
}
use of groovy.lang.GroovyShell in project hudson-2.x by hudson.
the class GroovyInitScript method execute.
private static void execute(GroovyCodeSource initScript) throws IOException {
GroovyShell shell = new GroovyShell(Hudson.getInstance().getPluginManager().uberClassLoader);
shell.evaluate(initScript);
}
use of groovy.lang.GroovyShell in project ratpack by ratpack.
the class GroovySnippetExecuter method execute.
@Override
public void execute(TestCodeSnippet snippet) throws Exception {
CompilerConfiguration config = new CompilerConfiguration();
config.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
if (compileStatic) {
classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
}
}
});
ClassLoader classLoader = new URLClassLoader(new URL[] {}, getClass().getClassLoader());
GroovyShell groovyShell = new GroovyShell(classLoader, new Binding(), config);
List<String> importsAndSnippet = extractImports(snippet.getSnippet());
String imports = importsAndSnippet.get(0);
String snippetMinusImports = fixture.transform(importsAndSnippet.get(1));
String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
Script script;
try {
script = groovyShell.parse(fullSnippet, snippet.getClassName());
} catch (MultipleCompilationErrorsException e) {
Message error = e.getErrorCollector().getError(0);
if (error instanceof SyntaxErrorMessage) {
//noinspection ThrowableResultOfMethodCallIgnored
System.out.println(snippet.getSnippet());
throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
} else {
throw e;
}
}
ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
fixture.around(script::run);
} finally {
Thread.currentThread().setContextClassLoader(previousContextClassLoader);
}
}
Aggregations