Search in sources :

Example 1 with CompilationFailedException

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

the class XmlTemplateEngine method createTemplate.

public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
    Node root;
    try {
        root = xmlParser.parse(reader);
    } catch (SAXException e) {
        throw new RuntimeException("Parsing XML source failed.", e);
    }
    if (root == null) {
        throw new IOException("Parsing XML source failed: root node is null.");
    }
    StringWriter writer = new StringWriter(1024);
    writer.write("/* Generated by XmlTemplateEngine */\n");
    new GspPrinter(new PrintWriter(writer), indentation).print(root);
    Script script;
    try {
        script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
    } catch (Exception e) {
        throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
    }
    return new XmlTemplate(script);
}
Also used : Script(groovy.lang.Script) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) StringWriter(java.io.StringWriter) Node(groovy.util.Node) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SAXException(org.xml.sax.SAXException) PrintWriter(java.io.PrintWriter)

Example 2 with CompilationFailedException

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

the class Groovy method parseAndRunScript.

private void parseAndRunScript(GroovyShell shell, String txt, Object mavenPom, String scriptName, File scriptFile, AntBuilder builder) {
    try {
        final Script script;
        if (scriptFile != null) {
            script = shell.parse(scriptFile);
        } else {
            script = shell.parse(txt, scriptName);
        }
        final Project project = getProject();
        script.setProperty("ant", builder);
        script.setProperty("project", project);
        script.setProperty("properties", new AntProjectPropertiesDelegate(project));
        script.setProperty("target", getOwningTarget());
        script.setProperty("task", this);
        script.setProperty("args", cmdline.getCommandline());
        if (mavenPom != null) {
            script.setProperty("pom", mavenPom);
        }
        script.run();
    } catch (final MissingMethodException mme) {
        // not a script, try running through run method but properties will not be available
        if (scriptFile != null) {
            try {
                shell.run(scriptFile, cmdline.getCommandline());
            } catch (IOException e) {
                processError(e);
            }
        } else {
            shell.run(txt, scriptName, cmdline.getCommandline());
        }
    } catch (final CompilationFailedException e) {
        processError(e);
    } catch (IOException e) {
        processError(e);
    }
}
Also used : Script(groovy.lang.Script) Project(org.apache.tools.ant.Project) MissingMethodException(groovy.lang.MissingMethodException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException)

Example 3 with CompilationFailedException

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

the class Main method loadScript.

private Class loadScript(String name) {
    Class scriptClass = null;
    GroovyClassLoader gcl = new GroovyClassLoader(this.getClass().getClassLoader());
    name = "src/test/" + getClass().getPackage().getName().replace(".", "/") + "/" + name;
    try {
        scriptClass = gcl.parseClass(new File(name));
    } catch (CompilationFailedException e) {
        throw new RuntimeException("Script compilation failed: " + e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException("Script file not found: " + name);
    }
    return scriptClass;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) File(java.io.File)

Example 4 with CompilationFailedException

use of org.codehaus.groovy.control.CompilationFailedException in project DataX by alibaba.

the class GroovyTransformer method initGroovyTransformer.

private void initGroovyTransformer(String code, List<String> extraPackage) {
    GroovyClassLoader loader = new GroovyClassLoader(GroovyTransformer.class.getClassLoader());
    String groovyRule = getGroovyRule(code, extraPackage);
    Class groovyClass;
    try {
        groovyClass = loader.parseClass(groovyRule);
    } catch (CompilationFailedException cfe) {
        throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, cfe);
    }
    try {
        Object t = groovyClass.newInstance();
        if (!(t instanceof Transformer)) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, "datax bug! contact askdatax");
        }
        this.groovyTransformer = (Transformer) t;
    } catch (Throwable ex) {
        throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, ex);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Transformer(com.alibaba.datax.transformer.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException)

Example 5 with CompilationFailedException

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

the class GroovyScriptFactory method getScriptedObjectType.

@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException {
    synchronized (this.scriptClassMonitor) {
        try {
            if (this.scriptClass == null || scriptSource.isModified()) {
                // New script content...
                this.wasModifiedForTypeCheck = true;
                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);
                    this.cachedResult = new CachedResultHolder(result);
                } else {
                    this.scriptResultClass = this.scriptClass;
                }
            }
            return this.scriptResultClass;
        } catch (CompilationFailedException ex) {
            this.scriptClass = null;
            this.scriptResultClass = null;
            this.cachedResult = null;
            throw new ScriptCompilationException(scriptSource, ex);
        }
    }
}
Also used : GroovyObject(groovy.lang.GroovyObject) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException)

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