Search in sources :

Example 11 with SourceUnit

use of org.codehaus.groovy.control.SourceUnit in project gradle by gradle.

the class GradleResolveVisitor method resolveToOuter.

private boolean resolveToOuter(ClassNode type) {
    String name = type.getName();
    // method again we set a NO_CLASS for this name
    if (type instanceof LowerCaseClass) {
        classNodeResolver.cacheClass(name, NO_CLASS);
        return false;
    }
    if (name.contains("$")) {
        return false;
    }
    if (currentClass.getModule().hasPackageName() && name.indexOf('.') == -1) {
        return false;
    }
    ClassNodeResolver.LookupResult lr = null;
    lr = classNodeResolver.resolveName(name, compilationUnit);
    if (lr != null) {
        if (lr.isSourceUnit()) {
            SourceUnit su = lr.getSourceUnit();
            currentClass.getCompileUnit().addClassNodeToCompile(type, su);
        } else {
            type.setRedirect(lr.getClassNode());
        }
        return true;
    }
    return false;
}
Also used : ClassNodeResolver(org.codehaus.groovy.control.ClassNodeResolver) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 12 with SourceUnit

use of org.codehaus.groovy.control.SourceUnit 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)

Example 13 with SourceUnit

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

the class LocatedMessage method write.

public void write(PrintWriter writer, Janitor janitor) {
    if (owner instanceof SourceUnit) {
        SourceUnit source = (SourceUnit) owner;
        String name = source.getName();
        int line = context.getStartLine();
        int column = context.getStartColumn();
        String sample = source.getSample(line, column, janitor);
        if (sample != null) {
            writer.println(source.getSample(line, column, janitor));
        }
        writer.println(name + ": " + line + ": " + this.message);
        writer.println("");
    } else {
        writer.println("<No Relevant Source>: " + this.message);
        writer.println("");
    }
}
Also used : SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 14 with SourceUnit

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

the class DependencyTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    cu = new CompilationUnit();
    cache = new StringSetMap();
    cu.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {

        @Override
        public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            DependencyTracker dt = new DependencyTracker(source, cache);
            dt.visitClass(classNode);
        }
    }, Phases.CLASS_GENERATION);
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext)

Example 15 with SourceUnit

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

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)

Aggregations

SourceUnit (org.codehaus.groovy.control.SourceUnit)26 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)10 ClassNode (org.codehaus.groovy.ast.ClassNode)8 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)8 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)6 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)5 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)4 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 GroovyObject (groovy.lang.GroovyObject)2 IncorrectTypeHintException (groovy.transform.stc.IncorrectTypeHintException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AntlrParserPlugin (org.codehaus.groovy.antlr.AntlrParserPlugin)2 GroovyLexer (org.codehaus.groovy.antlr.parser.GroovyLexer)2 GroovyRecognizer (org.codehaus.groovy.antlr.parser.GroovyRecognizer)2 GenericsType (org.codehaus.groovy.ast.GenericsType)2