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