Search in sources :

Example 76 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class GroovyTypeCheckingExtensionSupport method setup.

@Override
public void setup() {
    CompilerConfiguration config = new CompilerConfiguration();
    config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL");
    ImportCustomizer ic = new ImportCustomizer();
    ic.addStarImports("org.codehaus.groovy.ast.expr");
    ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper");
    ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport");
    config.addCompilationCustomizers(ic);
    final GroovyClassLoader transformLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : typeCheckingVisitor.getSourceUnit().getClassLoader();
    // since Groovy 2.2, it is possible to use FQCN for type checking extension scripts
    TypeCheckingDSL script = null;
    try {
        Class<?> clazz = transformLoader.loadClass(scriptPath, false, true);
        if (TypeCheckingDSL.class.isAssignableFrom(clazz)) {
            script = (TypeCheckingDSL) clazz.newInstance();
        } else if (TypeCheckingExtension.class.isAssignableFrom(clazz)) {
            // since 2.4, we can also register precompiled type checking extensions which are not scripts
            try {
                Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(StaticTypeCheckingVisitor.class);
                TypeCheckingExtension extension = (TypeCheckingExtension) declaredConstructor.newInstance(typeCheckingVisitor);
                typeCheckingVisitor.addTypeCheckingExtension(extension);
                extension.setup();
                return;
            } catch (InstantiationException e) {
                addLoadingError(config);
            } catch (IllegalAccessException e) {
                addLoadingError(config);
            } catch (NoSuchMethodException e) {
                context.getErrorCollector().addFatalError(new SimpleMessage("Static type checking extension '" + scriptPath + "' could not be loaded because it doesn't have a constructor accepting StaticTypeCheckingVisitor.", config.getDebug(), typeCheckingVisitor.getSourceUnit()));
            } catch (InvocationTargetException e) {
                addLoadingError(config);
            }
        }
    } catch (ClassNotFoundException e) {
    // silent
    } catch (InstantiationException e) {
        addLoadingError(config);
    } catch (IllegalAccessException e) {
        addLoadingError(config);
    }
    if (script == null) {
        ClassLoader cl = typeCheckingVisitor.getSourceUnit().getClassLoader();
        // cast to prevent incorrect @since 1.7 warning
        InputStream is = ((ClassLoader) transformLoader).getResourceAsStream(scriptPath);
        if (is == null) {
            // fallback to the source unit classloader
            is = cl.getResourceAsStream(scriptPath);
        }
        if (is == null) {
            // fallback to the compiler classloader
            cl = GroovyTypeCheckingExtensionSupport.class.getClassLoader();
            is = cl.getResourceAsStream(scriptPath);
        }
        if (is == null) {
            // if the input stream is still null, we've not found the extension
            context.getErrorCollector().addFatalError(new SimpleMessage("Static type checking extension '" + scriptPath + "' was not found on the classpath.", config.getDebug(), typeCheckingVisitor.getSourceUnit()));
        }
        try {
            GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config);
            script = (TypeCheckingDSL) shell.parse(new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()));
        } catch (CompilationFailedException e) {
            throw new GroovyBugError("An unexpected error was thrown during custom type checking", e);
        } catch (UnsupportedEncodingException e) {
            throw new GroovyBugError("Unsupported encoding found in compiler configuration", e);
        }
    }
    if (script != null) {
        script.extension = this;
        script.run();
        List<Closure> list = eventHandlers.get("setup");
        if (list != null) {
            for (Closure closure : list) {
                safeCall(closure);
            }
        }
    }
}
Also used : Closure(groovy.lang.Closure) GroovyBugError(org.codehaus.groovy.GroovyBugError) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClassLoader(groovy.lang.GroovyClassLoader) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) Constructor(java.lang.reflect.Constructor) InputStream(java.io.InputStream) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyShell(groovy.lang.GroovyShell)

Example 77 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class GroovyMain method processSockets.

/**
     * Process Sockets.
     */
private void processSockets() throws CompilationFailedException, IOException, URISyntaxException {
    GroovyShell groovy = new GroovyShell(conf);
    new GroovySocketServer(groovy, getScriptSource(isScriptFile, script), autoOutput, port);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 78 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class GroovyMain method processFiles.

/**
     * Process the input files.
     */
private void processFiles() throws CompilationFailedException, IOException, URISyntaxException {
    GroovyShell groovy = new GroovyShell(conf);
    setupContextClassLoader(groovy);
    Script s = groovy.parse(getScriptSource(isScriptFile, script));
    if (args.isEmpty()) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer = new PrintWriter(System.out);
        try {
            processReader(s, reader, writer);
        } finally {
            reader.close();
            writer.close();
        }
    } else {
        Iterator i = args.iterator();
        while (i.hasNext()) {
            String filename = (String) i.next();
            //TODO: These are the arguments for -p and -i.  Why are we searching using Groovy script extensions?
            // Where is this documented?
            File file = huntForTheScriptFile(filename);
            processFile(s, file);
        }
    }
}
Also used : Script(groovy.lang.Script) Iterator(java.util.Iterator) GroovyShell(groovy.lang.GroovyShell)

Example 79 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class Eval method xyz.

/**
     * Evaluates the specified String expression and makes the first three parameters available inside
     * the script bound to variables named 'x', 'y', and 'z' respectively, returning the result. For 
     * example, this code executes without failure: 
     * <pre class="groovyTestCase">
     * assert Eval.xyz(2, 4, 2, ' x * y + z') == 10
     * </pre>
     * @param expression the Groovy expression to evaluate
     * @return the result of the expression
     * @throws CompilationFailedException if expression is not valid Groovy
     */
public static Object xyz(final Object x, final Object y, final Object z, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable("x", x);
    b.setVariable("y", y);
    b.setVariable("z", z);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 80 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class InspectorTest method testClassPropsGroovy.

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

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