Search in sources :

Example 16 with CompilerConfiguration

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

Example 17 with CompilerConfiguration

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);
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) File(java.io.File)

Example 18 with CompilerConfiguration

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();
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 19 with CompilerConfiguration

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);
        }
    }
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 20 with CompilerConfiguration

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);
}
Also used : HashMap(java.util.HashMap) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)42 GroovyShell (groovy.lang.GroovyShell)14 Binding (groovy.lang.Binding)10 File (java.io.File)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)8 GroovyClassLoader (groovy.lang.GroovyClassLoader)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 GroovyBugError (org.codehaus.groovy.GroovyBugError)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 GroovyClass (org.codehaus.groovy.tools.GroovyClass)4 Map (java.util.Map)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)3 Closure (groovy.lang.Closure)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2