Search in sources :

Example 6 with CompilationFailedException

use of org.codehaus.groovy.control.CompilationFailedException in project spring-framework by spring-projects.

the class GroovyScriptFactory method getScriptedObject.

/**
	 * Loads and parses the Groovy script via the GroovyClassLoader.
	 * @see groovy.lang.GroovyClassLoader
	 */
@Override
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) throws IOException, ScriptCompilationException {
    synchronized (this.scriptClassMonitor) {
        try {
            Class<?> scriptClassToExecute;
            this.wasModifiedForTypeCheck = false;
            if (this.cachedResult != null) {
                Object result = this.cachedResult.object;
                this.cachedResult = null;
                return result;
            }
            if (this.scriptClass == null || scriptSource.isModified()) {
                // New script content...
                this.scriptClass = getGroovyClassLoader().parseClass(scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
                if (Script.class.isAssignableFrom(this.scriptClass)) {
                    // A Groovy script, probably creating an instance: let's execute it.
                    Object result = executeScript(scriptSource, this.scriptClass);
                    this.scriptResultClass = (result != null ? result.getClass() : null);
                    return result;
                } else {
                    this.scriptResultClass = this.scriptClass;
                }
            }
            scriptClassToExecute = this.scriptClass;
            // Process re-execution outside of the synchronized block.
            return executeScript(scriptSource, scriptClassToExecute);
        } catch (CompilationFailedException ex) {
            this.scriptClass = null;
            this.scriptResultClass = null;
            throw new ScriptCompilationException(scriptSource, ex);
        }
    }
}
Also used : GroovyObject(groovy.lang.GroovyObject) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException)

Example 7 with CompilationFailedException

use of org.codehaus.groovy.control.CompilationFailedException in project spock by spockframework.

the class ConfigurationScriptLoader method loadScriptFromClassPathLocation.

@Nullable
private DelegatingScript loadScriptFromClassPathLocation(String location) {
    URL url = this.getClass().getClassLoader().getResource(location);
    if (url == null)
        return null;
    GroovyShell shell = createShell();
    try {
        return (DelegatingScript) shell.parse(new GroovyCodeSource(url));
    } catch (IOException e) {
        throw new ConfigurationException("Error reading configuration script '%s'", location);
    } catch (CompilationFailedException e) {
        throw new ConfigurationException("Error compiling configuration script '%s'", location);
    }
}
Also used : ConfigurationException(spock.config.ConfigurationException) DelegatingScript(org.spockframework.builder.DelegatingScript) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) URL(java.net.URL) Nullable(org.spockframework.util.Nullable)

Example 8 with CompilationFailedException

use of org.codehaus.groovy.control.CompilationFailedException in project groovy-core by groovy.

the class TraitASTTransformation method registerASTTranformations.

private void registerASTTranformations(final ClassNode helper) {
    ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(unit, compilationUnit.getTransformLoader());
    collector.visitClass(helper);
    // Perform an additional phase which has to be done *after* type checking
    compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {

        @Override
        public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
            if (classNode == helper) {
                PostTypeCheckingExpressionReplacer replacer = new PostTypeCheckingExpressionReplacer(source);
                replacer.visitClass(helper);
            }
        }
    }, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber());
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) ASTTransformationCollectorCodeVisitor(org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)

Example 9 with CompilationFailedException

use of org.codehaus.groovy.control.CompilationFailedException in project grails-core by grails.

the class GroovyPagesTemplateEngine method compileGroovyPage.

/**
     * Attempts to compile the given InputStream into a Groovy script using the given name
     * @param in The InputStream to read the Groovy code from
     * @param name The name of the class to use
     * @param pageName The page name
     * @param metaInfo
     * @return The compiled java.lang.Class, which is an instance of groovy.lang.Script
     */
private Class<?> compileGroovyPage(InputStream in, String name, String pageName, GroovyPageMetaInfo metaInfo) {
    GroovyClassLoader groovyClassLoader = findOrInitGroovyClassLoader();
    // Compile the script into an object
    Class<?> scriptClass;
    try {
        String groovySource = IOGroovyMethods.getText(in, GroovyPageParser.GROOVY_SOURCE_CHAR_ENCODING);
        //System.out.println(groovySource);
        scriptClass = groovyClassLoader.parseClass(groovySource, name);
    } catch (CompilationFailedException e) {
        LOG.error("Compilation error compiling GSP [" + name + "]:" + e.getMessage(), e);
        int lineNumber = ExceptionUtils.extractLineNumber(e);
        final int[] lineMappings = metaInfo.getLineNumbers();
        if (lineNumber > 0 && lineNumber < lineMappings.length) {
            lineNumber = lineMappings[lineNumber - 1];
        }
        String relativePageName = DefaultErrorsPrinter.makeRelativeIfPossible(pageName);
        throw new GroovyPagesException("Could not parse script [" + relativePageName + "]: " + e.getMessage(), e, lineNumber, pageName);
    } catch (IOException e) {
        String relativePageName = DefaultErrorsPrinter.makeRelativeIfPossible(pageName);
        throw new GroovyPagesException("IO exception parsing script [" + relativePageName + "]: " + e.getMessage(), e);
    }
    GroovyPagesMetaUtils.registerMethodMissingForGSP(scriptClass, tagLibraryLookup);
    return scriptClass;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException)

Example 10 with CompilationFailedException

use of org.codehaus.groovy.control.CompilationFailedException in project gradle by gradle.

the class DefaultScriptCompilationHandler method compileScript.

private void compileScript(final ScriptSource source, ClassLoader classLoader, CompilerConfiguration configuration, File metadataDir, final CompileOperation<?> extractingTransformer, final Action<? super ClassNode> customVerifier) {
    final Transformer transformer = extractingTransformer != null ? extractingTransformer.getTransformer() : null;
    logger.info("Compiling {} using {}.", source.getDisplayName(), transformer != null ? transformer.getClass().getSimpleName() : "no transformer");
    final EmptyScriptDetector emptyScriptDetector = new EmptyScriptDetector();
    final PackageStatementDetector packageDetector = new PackageStatementDetector();
    GroovyClassLoader groovyClassLoader = new GroovyClassLoader(classLoader, configuration, false) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration compilerConfiguration, CodeSource codeSource) {
            CompilationUnit compilationUnit = new CustomCompilationUnit(compilerConfiguration, codeSource, customVerifier, this);
            if (transformer != null) {
                transformer.register(compilationUnit);
            }
            compilationUnit.addPhaseOperation(packageDetector, Phases.CANONICALIZATION);
            compilationUnit.addPhaseOperation(emptyScriptDetector, Phases.CANONICALIZATION);
            return compilationUnit;
        }
    };
    groovyClassLoader.setResourceLoader(NO_OP_GROOVY_RESOURCE_LOADER);
    String scriptText = source.getResource().getText();
    String scriptName = source.getClassName();
    GroovyCodeSource codeSource = new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script");
    try {
        try {
            groovyClassLoader.parseClass(codeSource, false);
        } catch (MultipleCompilationErrorsException e) {
            wrapCompilationFailure(source, e);
        } catch (CompilationFailedException e) {
            throw new GradleException(String.format("Could not compile %s.", source.getDisplayName()), e);
        }
        if (packageDetector.hasPackageStatement) {
            throw new UnsupportedOperationException(String.format("%s should not contain a package statement.", StringUtils.capitalize(source.getDisplayName())));
        }
        serializeMetadata(source, extractingTransformer, metadataDir, emptyScriptDetector.isEmptyScript(), emptyScriptDetector.getHasMethods());
    } finally {
        ClassLoaderUtils.tryClose(groovyClassLoader);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Transformer(org.gradle.groovy.scripts.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyCodeSource(groovy.lang.GroovyCodeSource) CodeSource(java.security.CodeSource) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyCodeSource(groovy.lang.GroovyCodeSource) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Aggregations

CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)26 IOException (java.io.IOException)14 GroovyClassLoader (groovy.lang.GroovyClassLoader)7 Script (groovy.lang.Script)5 ClassNode (org.codehaus.groovy.ast.ClassNode)5 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)5 File (java.io.File)4 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)4 SourceUnit (org.codehaus.groovy.control.SourceUnit)4 GroovyShell (groovy.lang.GroovyShell)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)3 Binding (groovy.lang.Binding)2 Closure (groovy.lang.Closure)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 MissingMethodException (groovy.lang.MissingMethodException)2 Node (groovy.util.Node)2 InputStreamReader (java.io.InputStreamReader)2