use of org.codehaus.groovy.control.CompilerConfiguration 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(source, 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);
}
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.
the class CompilerTest method setUp.
protected void setUp() throws Exception {
File dir = new File("target/test-generated-classes");
dir.mkdirs();
CompilerConfiguration config = new CompilerConfiguration();
config.setTargetDirectory(dir);
config.setDebug(dumpClass);
compiler = new Compiler(config);
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.
the class WriterController method init.
public void init(AsmClassGenerator asmClassGenerator, GeneratorContext gcon, ClassVisitor cv, ClassNode cn) {
CompilerConfiguration config = cn.getCompileUnit().getConfig();
Map<String, Boolean> optOptions = config.getOptimizationOptions();
boolean invokedynamic = false;
if (optOptions.isEmpty()) {
// IGNORE
} else if (Boolean.FALSE.equals(optOptions.get("all"))) {
optimizeForInt = false;
// set other optimizations options to false here
} else {
if (Boolean.TRUE.equals(optOptions.get("indy")))
invokedynamic = true;
if (Boolean.FALSE.equals(optOptions.get("int")))
optimizeForInt = false;
if (invokedynamic)
optimizeForInt = false;
// set other optimizations options to false here
}
this.classNode = cn;
this.outermostClass = null;
this.internalClassName = BytecodeHelper.getClassInternalName(classNode);
bytecodeVersion = chooseBytecodeVersion(invokedynamic, config.getTargetBytecode());
if (invokedynamic) {
try {
this.invocationWriter = (InvocationWriter) indyWriter.newInstance(this);
this.callSiteWriter = (CallSiteWriter) indyCallSiteWriter.newInstance(this);
this.binaryExpHelper = (BinaryExpressionHelper) indyBinHelper.newInstance(this);
} catch (Exception e) {
throw new GroovyRuntimeException("Cannot use invokedynamic, indy module was excluded from this build.");
}
} else {
this.callSiteWriter = new CallSiteWriter(this);
this.invocationWriter = new InvocationWriter(this);
this.binaryExpHelper = new BinaryExpressionHelper(this);
}
this.unaryExpressionHelper = new UnaryExpressionHelper(this);
if (optimizeForInt) {
this.fastPathBinaryExpHelper = new BinaryExpressionMultiTypeDispatcher(this);
// todo: replace with a real fast path unary expression helper when available
this.fastPathUnaryExpressionHelper = new UnaryExpressionHelper(this);
} else {
this.fastPathBinaryExpHelper = this.binaryExpHelper;
this.fastPathUnaryExpressionHelper = new UnaryExpressionHelper(this);
}
this.operandStack = new OperandStack(this);
this.assertionWriter = new AssertionWriter(this);
this.closureWriter = new ClosureWriter(this);
this.internalBaseClassName = BytecodeHelper.getClassInternalName(classNode.getSuperClass());
this.acg = asmClassGenerator;
this.sourceUnit = acg.getSourceUnit();
this.context = gcon;
this.compileStack = new CompileStack(this);
this.cv = cv;
if (optimizeForInt) {
this.statementWriter = new OptimizingStatementWriter(this);
} else {
this.statementWriter = new StatementWriter(this);
}
this.typeChooser = new StatementMetaTypeChooser();
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.
the class Groovy method configureCompiler.
private void configureCompiler() {
if (scriptBaseClass != null) {
configuration.setScriptBaseClass(scriptBaseClass);
}
if (indy) {
configuration.getOptimizationOptions().put("indy", Boolean.TRUE);
configuration.getOptimizationOptions().put("int", Boolean.FALSE);
}
if (configscript != null) {
Binding binding = new Binding();
binding.setVariable("configuration", configuration);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
File confSrc = new File(configscript);
try {
shell.evaluate(confSrc);
} catch (IOException e) {
throw new BuildException("Unable to configure compiler using configuration file: " + confSrc, e);
}
}
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.
the class FileSystemCompilerTest method setUp.
protected void setUp() throws Exception {
File dir = new File("target/test-generated-classes");
dir.mkdirs();
Map options = new HashMap();
options.put("stubDir", dir);
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.setTargetDirectory(dir);
configuration.setVerbose(dumpClass);
configuration.setJointCompilationOptions(options);
compiler = new FileSystemCompiler(configuration);
}
Aggregations