Java Code Examples for com.sun.tools.javac.tree.TreeInfo#symbolFor()

The following examples show how to use com.sun.tools.javac.tree.TreeInfo#symbolFor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StrictJavaDepsPlugin.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the compilation unit contains a single top-level class generated by an exempt
 * annotation processor (according to its {@link @Generated} annotation).
 *
 * <p>Annotation processors are expected to never generate more than one top level class, as
 * required by the style guide.
 */
public boolean isAnnotationProcessorExempt(JCTree.JCCompilationUnit unit) {
  if (unit.getTypeDecls().size() != 1) {
    return false;
  }
  Symbol sym = TreeInfo.symbolFor(getOnlyElement(unit.getTypeDecls()));
  if (sym == null) {
    return false;
  }
  for (String value : getGeneratedBy(sym)) {
    if (dependencyModule.getExemptGenerators().contains(value)) {
      return true;
    }
  }
  return false;
}
 
Example 2
Source File: JavacTrees.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        if (TreeInfo.isDeclaration(tree)) {
            for (TreePath p = path; p != null; p = p.getParentPath()) {
                JCTree t = (JCTree) p.getLeaf();
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl ct = (JCClassDecl) t;
                    if (ct.sym != null) {
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                            attr.attribClass(ct.pos(), ct.sym);
                            sym = TreeInfo.symbolFor(tree);
                        }
                        break;
                    }
                }
            }
        }
    }
    return sym;
}
 
Example 3
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        if (TreeInfo.isDeclaration(tree)) {
            for (TreePath p = path; p != null; p = p.getParentPath()) {
                JCTree t = (JCTree) p.getLeaf();
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl ct = (JCClassDecl) t;
                    if (ct.sym != null) {
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                            attr.attribClass(ct.pos(), ct.sym);
                            sym = TreeInfo.symbolFor(tree);
                        }
                        break;
                    }
                }
            }
        }
    }
    return sym;
}
 
Example 4
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        if (TreeInfo.isDeclaration(tree)) {
            for (TreePath p = path; p != null; p = p.getParentPath()) {
                JCTree t = (JCTree) p.getLeaf();
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl ct = (JCClassDecl) t;
                    if (ct.sym != null) {
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                            attr.attribClass(ct.pos(), ct.sym);
                            sym = TreeInfo.symbolFor(tree);
                        }
                        break;
                    }
                }
            }
        }
    }
    return sym;
}
 
Example 5
Source File: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public JCTree getTree(Element element) {
    Symbol symbol = (Symbol) element;
    TypeSymbol enclosing = symbol.enclClass();
    Env<AttrContext> env = enter.getEnv(enclosing);
    if (env == null)
        return null;
    JCClassDecl classNode = env.enclClass;
    if (classNode != null) {
        if (TreeInfo.symbolFor(classNode) == element)
            return classNode;
        for (JCTree node : classNode.getMembers())
            if (TreeInfo.symbolFor(node) == element)
                return node;
    }
    return null;
}
 
Example 6
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public JCTree getTree(Element element) {
    Symbol symbol = (Symbol) element;
    TypeSymbol enclosing = symbol.enclClass();
    Env<AttrContext> env = enter.getEnv(enclosing);
    if (env == null)
        return null;
    JCClassDecl classNode = env.enclClass;
    if (classNode != null) {
        if (TreeInfo.symbolFor(classNode) == element)
            return classNode;
        for (JCTree node : classNode.getMembers())
            if (TreeInfo.symbolFor(node) == element)
                return node;
    }
    return null;
}
 
Example 7
Source File: JavacTrees.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        if (TreeInfo.isDeclaration(tree)) {
            for (TreePath p = path; p != null; p = p.getParentPath()) {
                JCTree t = (JCTree) p.getLeaf();
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl ct = (JCClassDecl) t;
                    if (ct.sym != null) {
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                            attr.attribClass(ct.pos(), ct.sym);
                            sym = TreeInfo.symbolFor(tree);
                        }
                        break;
                    }
                }
            }
        }
    }
    return sym;
}
 
Example 8
Source File: JavacTrees.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        if (TreeInfo.isDeclaration(tree)) {
            for (TreePath p = path; p != null; p = p.getParentPath()) {
                JCTree t = (JCTree) p.getLeaf();
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl ct = (JCClassDecl) t;
                    if (ct.sym != null) {
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                            attr.attribClass(ct.pos(), ct.sym);
                            sym = TreeInfo.symbolFor(tree);
                        }
                        break;
                    }
                }
            }
        }
    }
    return sym;
}
 
Example 9
Source File: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        for (TreePath p = path; p != null; p = p.getParentPath()) {
            JCTree t = (JCTree) p.getLeaf();
            if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                JCClassDecl ct = (JCClassDecl) t;
                if (ct.sym != null) {
                    if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                        attr.attribClass(ct.pos(), ct.sym);
                        sym = TreeInfo.symbolFor(tree);
                    }
                    break;
                }
            }
        }
    }
    return sym;
}
 
Example 10
Source File: TestTrees.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void testElement(Trees trees, Element e) {
    trees.getClass();
    e.getClass();

    System.err.println("testElement: " + e);
    Tree tree = trees.getTree(e);
    //System.err.println(tree);

    if (TreeInfo.symbolFor((JCTree)tree) != e)
        error("bad result from getTree");

    TreePath path = trees.getPath(e);
    if (path == null) {
        error("getPath returned null");
        return;
    }
    if (path.getLeaf() != tree)
        error("bad result from getPath");

    Element e2 = trees.getElement(path);
    if (e2 == null) {
        error("getElement returned null");
        return;
    }
    if (e2 != e)
        error("bad result from getElement");

    // The TypeMirror is not available yet when annotation processing;
    // it is set up later during ANALYSE.
    TypeMirror t = trees.getTypeMirror(path);
    if (t != null && t.getKind() == TypeKind.DECLARED &&
            ((DeclaredType)t).asElement() != e2)
        error("bad result from getTypeMirror");

    for (AnnotationMirror m: e.getAnnotationMirrors()) {
        testAnnotation(trees, e, m);
    }
}
 
Example 11
Source File: DPrinter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void printTree(String label, JCTree tree) {
    if (tree == null) {
        printNull(label);
    } else {
        indent();
        String ext;
        try {
            ext = tree.getKind().name();
        } catch (Throwable t) {
            ext = "n/a";
        }
        out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext));
        if (showPositions) {
            // We can always get start position, but to get end position
            // and/or line+offset, we would need a JCCompilationUnit
            out.print(" pos:" + tree.pos);
        }
        if (showTreeTypes && tree.type != null)
            out.print(" type:" + toString(tree.type));
        Symbol sym;
        if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null)
            out.print(" sym:" + toString(sym));
        out.println();

        indent(+1);
        if (showSrc) {
            indent();
            out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength));
        }
        tree.accept(treeVisitor);
        indent(-1);
    }
}
 
Example 12
Source File: DPrinter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected void printTree(String label, JCTree tree) {
    if (tree == null) {
        printNull(label);
    } else {
        indent();
        String ext;
        try {
            ext = tree.getKind().name();
        } catch (Throwable t) {
            ext = "n/a";
        }
        out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext));
        if (showPositions) {
            // We can always get start position, but to get end position
            // and/or line+offset, we would need a JCCompilationUnit
            out.print(" pos:" + tree.pos);
        }
        if (showTreeTypes && tree.type != null)
            out.print(" type:" + toString(tree.type));
        Symbol sym;
        if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null)
            out.print(" sym:" + toString(sym));
        out.println();

        indent(+1);
        if (showSrc) {
            indent();
            out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength));
        }
        tree.accept(treeVisitor);
        indent(-1);
    }
}
 
Example 13
Source File: TestTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void testElement(Trees trees, Element e) {
    trees.getClass();
    e.getClass();

    System.err.println("testElement: " + e);
    Tree tree = trees.getTree(e);
    //System.err.println(tree);

    if (TreeInfo.symbolFor((JCTree)tree) != e)
        error("bad result from getTree");

    TreePath path = trees.getPath(e);
    if (path == null) {
        error("getPath returned null");
        return;
    }
    if (path.getLeaf() != tree)
        error("bad result from getPath");

    Element e2 = trees.getElement(path);
    if (e2 == null) {
        error("getElement returned null");
        return;
    }
    if (e2 != e)
        error("bad result from getElement");

    // The TypeMirror is not available yet when annotation processing;
    // it is set up later during ANALYSE.
    TypeMirror t = trees.getTypeMirror(path);
    if (t != null && t.getKind() == TypeKind.DECLARED &&
            ((DeclaredType)t).asElement() != e2)
        error("bad result from getTypeMirror");

    for (AnnotationMirror m: e.getAnnotationMirrors()) {
        testAnnotation(trees, e, m);
    }
}
 
Example 14
Source File: AssertCheckAnalyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitApply(JCMethodInvocation tree) {
    Symbol m = TreeInfo.symbolFor(tree);
    AssertOverloadKind ak = assertOverloadKind(m);
    if (ak != AssertOverloadKind.NONE &&
        !m.name.contentEquals("error")) {
        JCExpression lastParam = tree.args.last();
        if (isSimpleStringArg(lastParam) != ak.simpleArgExpected()) {
            messages.error(lastParam, ak.errKey);
        }
    }

    super.visitApply(tree);
}
 
Example 15
Source File: AssertCheckAnalyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
boolean isSimpleStringArg(JCExpression e) {
    switch (e.getTag()) {
        case LAMBDA:
            JCLambda lambda = (JCLambda)e;
            return (lambda.getBodyKind() == BodyKind.EXPRESSION) &&
                    isSimpleStringArg((JCExpression)lambda.body);
        default:
            Symbol argSym = TreeInfo.symbolFor(e);
            return (e.type.constValue() != null ||
                    (argSym != null && argSym.kind == Kinds.Kind.VAR));
    }
}
 
Example 16
Source File: TestTrees.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void testElement(Trees trees, Element e) {
    trees.getClass();
    e.getClass();

    System.err.println("testElement: " + e);
    Tree tree = trees.getTree(e);
    //System.err.println(tree);

    if (TreeInfo.symbolFor((JCTree)tree) != e)
        error("bad result from getTree");

    TreePath path = trees.getPath(e);
    if (path == null) {
        error("getPath returned null");
        return;
    }
    if (path.getLeaf() != tree)
        error("bad result from getPath");

    Element e2 = trees.getElement(path);
    if (e2 == null) {
        error("getElement returned null");
        return;
    }
    if (e2 != e)
        error("bad result from getElement");

    // The TypeMirror is not available yet when annotation processing;
    // it is set up later during ANALYSE.
    TypeMirror t = trees.getTypeMirror(path);
    if (t != null && t.getKind() == TypeKind.DECLARED &&
            ((DeclaredType)t).asElement() != e2)
        error("bad result from getTypeMirror");

    for (AnnotationMirror m: e.getAnnotationMirrors()) {
        testAnnotation(trees, e, m);
    }
}
 
Example 17
Source File: DPrinter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void printTree(String label, JCTree tree) {
    if (tree == null) {
        printNull(label);
    } else {
        indent();
        String ext;
        try {
            ext = tree.getKind().name();
        } catch (Throwable t) {
            ext = "n/a";
        }
        out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext));
        if (showPositions) {
            // We can always get start position, but to get end position
            // and/or line+offset, we would need a JCCompilationUnit
            out.print(" pos:" + tree.pos);
        }
        if (showTreeTypes && tree.type != null)
            out.print(" type:" + toString(tree.type));
        Symbol sym;
        if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null)
            out.print(" sym:" + toString(sym));
        out.println();

        indent(+1);
        if (showSrc) {
            indent();
            out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength));
        }
        tree.accept(treeVisitor);
        indent(-1);
    }
}
 
Example 18
Source File: TestTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void testElement(Trees trees, Element e) {
    trees.getClass();
    e.getClass();

    System.err.println("testElement: " + e);
    Tree tree = trees.getTree(e);
    //System.err.println(tree);

    if (TreeInfo.symbolFor((JCTree)tree) != e)
        error("bad result from getTree");

    TreePath path = trees.getPath(e);
    if (path == null) {
        error("getPath returned null");
        return;
    }
    if (path.getLeaf() != tree)
        error("bad result from getPath");

    Element e2 = trees.getElement(path);
    if (e2 == null) {
        error("getElement returned null");
        return;
    }
    if (e2 != e)
        error("bad result from getElement");

    // The TypeMirror is not available yet when annotation processing;
    // it is set up later during ANALYSE.
    TypeMirror t = trees.getTypeMirror(path);
    if (t != null && t.getKind() == TypeKind.DECLARED &&
            ((DeclaredType)t).asElement() != e2)
        error("bad result from getTypeMirror");

    for (AnnotationMirror m: e.getAnnotationMirrors()) {
        testAnnotation(trees, e, m);
    }
}
 
Example 19
Source File: DPrinter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected void printTree(String label, JCTree tree) {
    if (tree == null) {
        printNull(label);
    } else {
        indent();
        String ext;
        try {
            ext = tree.getKind().name();
        } catch (Throwable t) {
            ext = "n/a";
        }
        out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext));
        if (showPositions) {
            // We can always get start position, but to get end position
            // and/or line+offset, we would need a JCCompilationUnit
            out.print(" pos:" + tree.pos);
        }
        if (showTreeTypes && tree.type != null)
            out.print(" type:" + toString(tree.type));
        Symbol sym;
        if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null)
            out.print(" sym:" + toString(sym));
        out.println();

        indent(+1);
        if (showSrc) {
            indent();
            out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength));
        }
        tree.accept(treeVisitor);
        indent(-1);
    }
}
 
Example 20
Source File: NBJavacTrees.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Symbol getElement(TreePath path) {
    return TreeInfo.symbolFor((JCTree) path.getLeaf());
}