Search in sources :

Example 76 with TreePath

use of com.sun.source.util.TreePath in project bazel by bazelbuild.

the class TreeUtils method enclosingOfClass.

/**
     * Gets the first enclosing tree in path, of the specified class
     *
     * @param path  the path defining the tree node
     * @param treeClass the class of the desired tree
     * @return the enclosing tree of the given type as given by the path
     */
public static <T extends Tree> T enclosingOfClass(final TreePath path, final Class<T> treeClass) {
    TreePath p = path;
    while (p != null) {
        Tree leaf = p.getLeaf();
        if (treeClass.isInstance(leaf))
            return treeClass.cast(leaf);
        p = p.getParentPath();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree)

Example 77 with TreePath

use of com.sun.source.util.TreePath in project bazel by bazelbuild.

the class TreeUtils method enclosingTopLevelBlock.

public static /*@Nullable*/
BlockTree enclosingTopLevelBlock(TreePath path) {
    TreePath parpath = path.getParentPath();
    while (parpath != null && parpath.getLeaf().getKind() != Tree.Kind.CLASS) {
        path = parpath;
        parpath = parpath.getParentPath();
    }
    if (path.getLeaf().getKind() == Tree.Kind.BLOCK) {
        return (BlockTree) path.getLeaf();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) BlockTree(com.sun.source.tree.BlockTree)

Example 78 with TreePath

use of com.sun.source.util.TreePath in project bazel by bazelbuild.

the class TreeUtils method pathTillOfKind.

/**
     * Gets path to the the first enclosing tree with any one of the specified kinds.
     *
     * @param path  the path defining the tree node
     * @param kinds  the set of kinds of the desired tree
     * @return the path to the enclosing tree of the given type
     */
public static TreePath pathTillOfKind(final TreePath path, final Set<Tree.Kind> kinds) {
    TreePath p = path;
    while (p != null) {
        Tree leaf = p.getLeaf();
        assert leaf != null;
        /*nninvariant*/
        if (kinds.contains(leaf.getKind()))
            return p;
        p = p.getParentPath();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree)

Example 79 with TreePath

use of com.sun.source.util.TreePath in project buck by facebook.

the class TreeBackedTreesTest method testGetTreeNullGetPathRoundtripTypeParameterElement.

@Test
public void testGetTreeNullGetPathRoundtripTypeParameterElement() throws IOException {
    compile("class Foo<T, U> { }");
    TypeParameterElement tElement = elements.getTypeElement("Foo").getTypeParameters().get(0);
    Tree tTree = trees.getTree(tElement);
    TreePath tPath = trees.getPath(tElement);
    // Odd behavior by javac, but we'll match it
    assertNull(tTree);
    assertSame(tElement, trees.getElement(tPath));
}
Also used : TreePath(com.sun.source.util.TreePath) Tree(com.sun.source.tree.Tree) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 80 with TreePath

use of com.sun.source.util.TreePath in project j2objc by google.

the class JavadocConverter method convertJavadoc.

/**
   * Returns an AST node for the javadoc comment of a specified class,
   * method, or field element.
   */
static Javadoc convertJavadoc(Element element, String source, JavacEnvironment env, boolean reportWarnings) {
    DocTrees docTrees = DocTrees.instance(env.task());
    TreePath path = docTrees.getPath(element);
    if (path == null) {
        throw new AssertionError("could not find tree path for element");
    }
    DCTree.DCDocComment docComment = (DCTree.DCDocComment) docTrees.getDocCommentTree(path);
    if (docComment == null) {
        // Declaration does not have a javadoc comment.
        return null;
    }
    JavadocConverter converter = new JavadocConverter(element, docComment, source, docTrees, path.getCompilationUnit(), reportWarnings);
    Javadoc result = new Javadoc();
    // First tag has no name.
    TagElement newTag = new TagElement();
    converter.scan(docComment.getFirstSentence(), newTag);
    converter.scan(docComment.getBody(), newTag);
    if (!newTag.getFragments().isEmpty()) {
        List<TreeNode> fragments = newTag.getFragments();
        int start = fragments.get(0).getStartPosition();
        TreeNode lastFragment = fragments.get(fragments.size() - 1);
        int end = start + lastFragment.getLength();
        converter.setPos(newTag, start, end);
        result.addTag(newTag);
    }
    for (DocTree tag : docComment.getBlockTags()) {
        if (tag.getKind() != DocTree.Kind.ERRONEOUS) {
            newTag = new TagElement();
            converter.scan(tag, newTag);
            result.addTag(newTag);
        }
    }
    return result;
}
Also used : Javadoc(com.google.devtools.j2objc.ast.Javadoc) TreePath(com.sun.source.util.TreePath) DocTrees(com.sun.source.util.DocTrees) TreeNode(com.google.devtools.j2objc.ast.TreeNode) TagElement(com.google.devtools.j2objc.ast.TagElement) DocTree(com.sun.source.doctree.DocTree) DCTree(com.sun.tools.javac.tree.DCTree)

Aggregations

TreePath (com.sun.source.util.TreePath)151 ExpressionTree (com.sun.source.tree.ExpressionTree)60 Tree (com.sun.source.tree.Tree)60 VariableTree (com.sun.source.tree.VariableTree)50 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)46 MethodTree (com.sun.source.tree.MethodTree)46 ClassTree (com.sun.source.tree.ClassTree)45 MemberSelectTree (com.sun.source.tree.MemberSelectTree)39 IdentifierTree (com.sun.source.tree.IdentifierTree)37 BlockTree (com.sun.source.tree.BlockTree)36 NewClassTree (com.sun.source.tree.NewClassTree)32 StatementTree (com.sun.source.tree.StatementTree)32 JCTree (com.sun.tools.javac.tree.JCTree)31 AssignmentTree (com.sun.source.tree.AssignmentTree)27 BinaryTree (com.sun.source.tree.BinaryTree)27 TypeCastTree (com.sun.source.tree.TypeCastTree)26 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)25 LiteralTree (com.sun.source.tree.LiteralTree)25 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)23