Search in sources :

Example 11 with CompilationUnit

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

the class CustomCodeVisitorSupport method createCompilationUnit.

protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
    CompilationUnit unit = super.createCompilationUnit(config, source);
    unit.addPhaseOperation(new CustomPrimaryClassNodeOperation(), Phases.SEMANTIC_ANALYSIS);
    return unit;
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit)

Example 12 with CompilationUnit

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

the class ScriptCompilationExecuter method execute.

public long execute() throws Exception {
    ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent());
    GroovyClassLoader gcl = new GroovyClassLoader(cl);
    CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl, new GroovyClassLoader(this.getClass().getClassLoader()));
    for (File source : sources) {
        cu.addSource(source);
    }
    long sd = System.nanoTime();
    cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber());
    long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd);
    return dur;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) URLClassLoader(java.net.URLClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 13 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit in project webpieces by deanhiller.

the class GroovyToJavaClassCompiler method compileImpl.

private void compileImpl(GroovyClassLoader groovyCl, ScriptOutputImpl scriptCode) {
    CompilationUnit compileUnit = new CompilationUnit();
    compileUnit.addSource(scriptCode.getFullClassName(), scriptCode.getScriptSourceCode());
    compileUnit.compile(Phases.CLASS_GENERATION);
    compileUnit.setClassLoader(groovyCl);
    for (Object compileClass : compileUnit.getClasses()) {
        GroovyClass groovyClass = (GroovyClass) compileClass;
        callbacks.compiledGroovyClass(groovyCl, groovyClass);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) GroovyClass(org.codehaus.groovy.tools.GroovyClass)

Example 14 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit 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 15 with CompilationUnit

use of org.codehaus.groovy.control.CompilationUnit in project groovy-core by groovy.

the class ASTTest method getAST.

public ModuleNode getAST(String source, int untilPhase) {
    SourceUnit unit = SourceUnit.create("Test", source);
    CompilationUnit compUnit = new CompilationUnit();
    compUnit.addSource(unit);
    compUnit.compile(untilPhase);
    return unit.getAST();
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Aggregations

CompilationUnit (org.codehaus.groovy.control.CompilationUnit)31 ClassNode (org.codehaus.groovy.ast.ClassNode)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 SourceUnit (org.codehaus.groovy.control.SourceUnit)8 File (java.io.File)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyClass (org.codehaus.groovy.tools.GroovyClass)5 ArrayList (java.util.ArrayList)4 BuildException (org.apache.tools.ant.BuildException)4 URL (java.net.URL)3 List (java.util.List)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 JavaAwareCompilationUnit (org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit)3 GroovyObject (groovy.lang.GroovyObject)2 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PrivilegedActionException (java.security.PrivilegedActionException)2 LinkedHashSet (java.util.LinkedHashSet)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2