Search in sources :

Example 1 with TreePath

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

the class TreeContextScanner method scan.

@Override
@Nullable
public R scan(Tree tree, @Nullable P p) {
    if (tree == null) {
        return null;
    }
    TreePath previousPath = currentPath;
    Element previousEnclosingElement = enclosingElement;
    currentPath = new TreePath(currentPath, tree);
    switch(tree.getKind()) {
        case ANNOTATION_TYPE:
        case CLASS:
        case COMPILATION_UNIT:
        case ENUM:
        case INTERFACE:
        case METHOD:
        case VARIABLE:
        case TYPE_PARAMETER:
            enclosingElement = Preconditions.checkNotNull(trees.getElement(currentPath));
            break;
        // $CASES-OMITTED$
        default:
            break;
    }
    try {
        // This super call will actually visit the tree, now with all the context set up
        return super.scan(tree, p);
    } finally {
        currentPath = previousPath;
        enclosingElement = previousEnclosingElement;
    }
}
Also used : TreePath(com.sun.source.util.TreePath) Element(javax.lang.model.element.Element) Nullable(com.facebook.buck.util.liteinfersupport.Nullable)

Example 2 with TreePath

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

the class InterfaceValidator method validate.

public void validate(List<? extends CompilationUnitTree> compilationUnits) {
    try (BuckTracing.TraceSection trace = BUCK_TRACING.traceSection("buck.abi.validate")) {
        new InterfaceTypeAndConstantReferenceFinder(trees, new InterfaceTypeAndConstantReferenceFinder.Listener() {

            private final Set<Element> importedTypes = new HashSet<>();

            @Override
            public void onTypeImported(TypeElement type) {
                importedTypes.add(type);
            }

            @Override
            public void onTypeReferenceFound(TypeElement referencedType, TreePath path, Element enclosingElement) {
                PackageElement enclosingPackage = getPackageElement(enclosingElement);
                if (typeWillBeAvailable(referencedType) || referenceIsLegalForMissingTypes(path, enclosingPackage, referencedType)) {
                    // All good!
                    return;
                }
                String minimalQualifiedName = findMinimalQualifiedName(path, enclosingPackage, referencedType);
                // TODO(jkeljo): Clearer message
                trees.printMessage(messageKind, String.format("Must qualify the name: %s", minimalQualifiedName), path.getLeaf(), path.getCompilationUnit());
            }

            @Override
            public void onConstantReferenceFound(VariableElement constant, TreePath path, Element enclosingElement) {
                TypeElement constantEnclosingType = (TypeElement) constant.getEnclosingElement();
                if (typeWillBeAvailable(constantEnclosingType)) {
                    // All good!
                    return;
                }
                // TODO(jkeljo): Clearer message
                trees.printMessage(messageKind, String.format("Must inline the constant value: %s", constant.getConstantValue()), path.getLeaf(), path.getCompilationUnit());
            }

            private boolean typeWillBeAvailable(TypeElement type) {
                return isCompiledInCurrentRun(type) || isOnBootClasspath(type);
            }

            private boolean isCompiledInCurrentRun(TypeElement typeElement) {
                return trees.getPath(typeElement) != null;
            }

            private boolean isOnBootClasspath(TypeElement typeElement) {
                return bootClasspathOracle.isOnBootClasspath(elements.getBinaryName(typeElement).toString());
            }

            private boolean referenceIsLegalForMissingTypes(TreePath path, PackageElement enclosingPackage, TypeElement referencedTypeElement) {
                return isImported(referencedTypeElement) || isTopLevelTypeInPackage(referencedTypeElement, enclosingPackage) || isFullyQualified(path, referencedTypeElement);
            }

            private boolean isImported(TypeElement referencedTypeElement) {
                return importedTypes.contains(referencedTypeElement);
            }

            private boolean isTopLevelTypeInPackage(TypeElement referencedTypeElement, PackageElement enclosingPackage) {
                return enclosingPackage == referencedTypeElement.getEnclosingElement();
            }

            private boolean isFullyQualified(TreePath path, TypeElement referencedTypeElement) {
                return referencedTypeElement.getQualifiedName().contentEquals(TreeBackedTrees.treeToName(path.getLeaf()));
            }

            private String findMinimalQualifiedName(TreePath path, PackageElement enclosingPackage, TypeElement typeElement) {
                List<QualifiedNameable> enclosingElements = new ArrayList<>();
                QualifiedNameable walker = typeElement;
                while (walker.getKind() != ElementKind.PACKAGE && !referenceIsLegalForMissingTypes(path, enclosingPackage, (TypeElement) walker)) {
                    enclosingElements.add(walker);
                    walker = (QualifiedNameable) walker.getEnclosingElement();
                }
                enclosingElements.add(walker);
                StringBuilder resultBuilder = new StringBuilder();
                for (int i = enclosingElements.size() - 1; i >= 0; i--) {
                    QualifiedNameable element = enclosingElements.get(i);
                    if (element.getKind() == ElementKind.PACKAGE) {
                        resultBuilder.append(element.getQualifiedName());
                    } else {
                        resultBuilder.append(element.getSimpleName());
                    }
                    if (i > 0) {
                        resultBuilder.append(".");
                    }
                }
                return resultBuilder.toString();
            }
        }).findReferences(compilationUnits);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) BuckTracing(com.facebook.buck.event.api.BuckTracing) TreePath(com.sun.source.util.TreePath) QualifiedNameable(javax.lang.model.element.QualifiedNameable) PackageElement(javax.lang.model.element.PackageElement)

Example 3 with TreePath

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

the class TreeBackedTreesTest method testGetTreeGetPathRoundtripTypeElement.

@Test
public void testGetTreeGetPathRoundtripTypeElement() throws IOException {
    compile("class Foo<T, U> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    Tree fooTree = trees.getTree(fooElement);
    TreePath fooPath = trees.getPath(fooElement);
    assertSame(fooPath.getLeaf(), fooTree);
    assertSame(fooElement, trees.getElement(fooPath));
}
Also used : TreePath(com.sun.source.util.TreePath) TypeElement(javax.lang.model.element.TypeElement) Tree(com.sun.source.tree.Tree) Test(org.junit.Test)

Example 4 with TreePath

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

the class TreeUtils method getAssignmentContext.

/**
     * Returns the tree with the assignment context for the treePath
     * leaf node.
     *
     * The assignment context for the treepath is the most enclosing
     * tree of type:
     * <ul>
     *   <li>AssignmentTree </li>
     *   <li>CompoundAssignmentTree </li>
     *   <li>MethodInvocationTree</li>
     *   <li>NewArrayTree</li>
     *   <li>NewClassTree</li>
     *   <li>ReturnTree</li>
     *   <li>VariableTree</li>
     * </ul>
     *
     * @param treePath
     * @return  the assignment context as described.
     */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null)
        return null;
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree))
        return node;
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) 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) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 5 with TreePath

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

the class TreeUtils method enclosingOfKind.

/**
     * Gets the first enclosing tree in path, 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 enclosing tree of the given type as given by the path
     */
public static Tree enclosingOfKind(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 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)

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