Search in sources :

Example 26 with CompilationUnit

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

the class MetaClassImpl method getClassNode.

/**
     * Obtains a reference to the original AST for the MetaClass if it is available at runtime
     *
     * @return The original AST or null if it cannot be returned
     */
public ClassNode getClassNode() {
    if (classNode == null && GroovyObject.class.isAssignableFrom(theClass)) {
        // let's try load it from the classpath
        String groovyFile = theClass.getName();
        int idx = groovyFile.indexOf('$');
        if (idx > 0) {
            groovyFile = groovyFile.substring(0, idx);
        }
        groovyFile = groovyFile.replace('.', '/') + ".groovy";
        //System.out.println("Attempting to load: " + groovyFile);
        URL url = theClass.getClassLoader().getResource(groovyFile);
        if (url == null) {
            url = Thread.currentThread().getContextClassLoader().getResource(groovyFile);
        }
        if (url != null) {
            try {
                /**
                     * todo there is no CompileUnit in scope so class name
                     * checking won't work but that mostly affects the bytecode
                     * generation rather than viewing the AST
                     */
                CompilationUnit.ClassgenCallback search = new CompilationUnit.ClassgenCallback() {

                    public void call(ClassVisitor writer, ClassNode node) {
                        if (node.getName().equals(theClass.getName())) {
                            MetaClassImpl.this.classNode = node;
                        }
                    }
                };
                CompilationUnit unit = new CompilationUnit();
                unit.setClassgenCallback(search);
                unit.addSource(url);
                unit.compile(Phases.CLASS_GENERATION);
            } catch (Exception e) {
                throw new GroovyRuntimeException("Exception thrown parsing: " + groovyFile + ". Reason: " + e, e);
            }
        }
    }
    return classNode;
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassVisitor(org.objectweb.asm.ClassVisitor) URL(java.net.URL) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) IntrospectionException(java.beans.IntrospectionException) PrivilegedActionException(java.security.PrivilegedActionException) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException)

Example 27 with CompilationUnit

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

the class ProxyGeneratorAdapter method adjustSuperClass.

private Class adjustSuperClass(Class superClass, final Class[] interfaces) {
    boolean isSuperClassAnInterface = superClass.isInterface();
    if (!isSuperClassAnInterface) {
        return superClass;
    }
    Class result = Object.class;
    Set<ClassNode> traits = new LinkedHashSet<ClassNode>();
    // check if it's a trait
    collectTraits(superClass, traits);
    if (interfaces != null) {
        for (Class anInterface : interfaces) {
            collectTraits(anInterface, traits);
        }
    }
    if (!traits.isEmpty()) {
        String name = superClass.getName() + "$TraitAdapter";
        ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(new ClassNode[traits.size()]), null);
        CompilationUnit cu = new CompilationUnit(loader);
        CompilerConfiguration config = new CompilerConfiguration();
        SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
        cu.addSource(su);
        cu.compile(Phases.CONVERSION);
        su.getAST().addClass(cn);
        cu.compile(Phases.CLASS_GENERATION);
        @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
        for (GroovyClass groovyClass : classes) {
            if (groovyClass.getName().equals(name)) {
                return loader.defineClass(name, groovyClass.getBytes());
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GroovyObject(groovy.lang.GroovyObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with CompilationUnit

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

the class Compiler method compile.

/**
    *  Compiles a single File.
    */
public void compile(File file) throws CompilationFailedException {
    CompilationUnit unit = new CompilationUnit(configuration);
    unit.addSource(file);
    unit.compile();
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit)

Example 29 with CompilationUnit

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

the class Compiler method compile.

/**
    *  Compiles a string of code.
    */
public void compile(String name, String code) throws CompilationFailedException {
    CompilationUnit unit = new CompilationUnit(configuration);
    unit.addSource(new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()));
    unit.compile();
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 30 with CompilationUnit

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

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