Search in sources :

Example 16 with GroovyShell

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;
    }
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) GroovyShell(groovy.lang.GroovyShell) MissingPropertyException(groovy.lang.MissingPropertyException)

Example 17 with GroovyShell

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();
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell)

Example 18 with GroovyShell

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);
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) GroovyShell(groovy.lang.GroovyShell)

Example 19 with GroovyShell

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);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 20 with GroovyShell

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);
    }
}
Also used : Binding(groovy.lang.Binding) ClassNode(org.codehaus.groovy.ast.ClassNode) Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) GroovyShell(groovy.lang.GroovyShell) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Aggregations

GroovyShell (groovy.lang.GroovyShell)86 Binding (groovy.lang.Binding)43 File (java.io.File)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Script (groovy.lang.Script)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)10 Closure (groovy.lang.Closure)9 IOException (java.io.IOException)8 List (java.util.List)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 AntBuilder (groovy.util.AntBuilder)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 BuildException (org.apache.tools.ant.BuildException)4 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)4 UrlMapping (grails.web.mapping.UrlMapping)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3