Search in sources :

Example 61 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class JavacMessager method printMessage.

/**
     * Prints a message of the specified kind at the location of the
     * annotation value inside the annotation mirror of the annotated
     * element.
     *
     * @param kind the kind of message
     * @param msg  the message, or an empty string if none
     * @param e    the annotated element
     * @param a    the annotation containing the annotaiton value
     * @param v    the annotation value to use as a position hint
     */
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
    JavaFileObject oldSource = null;
    JavaFileObject newSource = null;
    JCDiagnostic.DiagnosticPosition pos = null;
    JavacElements elemUtils = processingEnv.getElementUtils();
    Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
    if (treeTop != null) {
        newSource = treeTop.snd.sourcefile;
        if (newSource != null) {
            oldSource = log.useSource(newSource);
            pos = treeTop.fst.pos();
        }
    }
    try {
        switch(kind) {
            case ERROR:
                errorCount++;
                boolean prev = log.multipleErrors;
                log.multipleErrors = true;
                try {
                    log.error(pos, "proc.messager", msg.toString());
                } finally {
                    log.multipleErrors = prev;
                }
                break;
            case WARNING:
                warningCount++;
                log.warning(pos, "proc.messager", msg.toString());
                break;
            case MANDATORY_WARNING:
                warningCount++;
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
                break;
            default:
                log.note(pos, "proc.messager", msg.toString());
                break;
        }
    } finally {
        if (oldSource != null)
            log.useSource(oldSource);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavacElements(com.sun.tools.javac.model.JavacElements) JCTree(com.sun.tools.javac.tree.JCTree)

Example 62 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class JavacElements method matchAnnoToTree.

/**
     * Returns the tree for an annotation given an Attribute to
     * search (recursively) and its corresponding tree.
     * Returns null if the tree cannot be found.
     */
private JCTree matchAnnoToTree(final Attribute.Compound findme, final Attribute attr, final JCTree tree) {
    if (attr == findme)
        return (tree.type.tsym == findme.type.tsym) ? tree : null;
    class Vis implements Attribute.Visitor {

        JCTree result = null;

        public void visitConstant(Attribute.Constant value) {
        }

        public void visitClass(Attribute.Class clazz) {
        }

        public void visitCompound(Attribute.Compound anno) {
            for (Pair<MethodSymbol, Attribute> pair : anno.values) {
                JCExpression expr = scanForAssign(pair.fst, tree);
                if (expr != null) {
                    JCTree match = matchAnnoToTree(findme, pair.snd, expr);
                    if (match != null) {
                        result = match;
                        return;
                    }
                }
            }
        }

        public void visitArray(Attribute.Array array) {
            if (tree.getTag() == JCTree.NEWARRAY && types.elemtype(array.type).tsym == findme.type.tsym) {
                List<JCExpression> elems = ((JCNewArray) tree).elems;
                for (Attribute value : array.values) {
                    if (value == findme) {
                        result = elems.head;
                        return;
                    }
                    elems = elems.tail;
                }
            }
        }

        public void visitEnum(Attribute.Enum e) {
        }

        public void visitError(Attribute.Error e) {
        }
    }
    Vis vis = new Vis();
    attr.accept(vis);
    return vis.result;
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Example 63 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class Pretty method visitTry.

public void visitTry(JCTry tree) {
    try {
        print("try ");
        if (tree.resources.nonEmpty()) {
            print("(");
            boolean first = true;
            for (JCTree var : tree.resources) {
                if (!first) {
                    println();
                    indent();
                }
                printStat(var);
                first = false;
            }
            print(") ");
        }
        printStat(tree.body);
        for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
            printStat(l.head);
        }
        if (tree.finalizer != null) {
            print(" finally ");
            printStat(tree.finalizer);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Example 64 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class JavacProcessingEnvironment method getTopLevelClasses.

private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) {
    List<ClassSymbol> classes = List.nil();
    for (JCCompilationUnit unit : units) {
        for (JCTree node : unit.defs) {
            if (node.getTag() == JCTree.CLASSDEF) {
                ClassSymbol sym = ((JCClassDecl) node).sym;
                Assert.checkNonNull(sym);
                classes = classes.prepend(sym);
            }
        }
    }
    return classes.reverse();
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Example 65 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class TreeCopier method visitImport.

public JCTree visitImport(ImportTree node, P p) {
    JCImport t = (JCImport) node;
    JCTree qualid = copy(t.qualid, p);
    return M.at(t.pos).Import(qualid, t.staticImport);
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

JCTree (com.sun.tools.javac.tree.JCTree)183 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)28 Symbol (com.sun.tools.javac.code.Symbol)22 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)22 Type (com.sun.tools.javac.code.Type)19 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)17 Tree (com.sun.source.tree.Tree)15 ExpressionTree (com.sun.source.tree.ExpressionTree)14 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)14 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)11 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)11 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)10 ArrayList (java.util.ArrayList)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)9 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)9 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)8 ListBuffer (com.sun.tools.javac.util.ListBuffer)8 Type (com.redhat.ceylon.model.typechecker.model.Type)7 ClassTree (com.sun.source.tree.ClassTree)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7