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);
}
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);
}
}
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;
}
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);
}
}
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);
}
}
}
Aggregations