Search in sources :

Example 21 with TreePath

use of com.sun.source.util.TreePath in project checker-framework by typetools.

the class AnnotatedTypeFactory method type.

/**
 * Determines an empty annotated type of the given tree. In other words, finds the {@link
 * TypeMirror} for the tree and converts that into an {@link AnnotatedTypeMirror}, but does not
 * add any annotations to the result.
 *
 * <p>Most users will want to use getAnnotatedType instead; this method is mostly for internal
 * use.
 *
 * @param node the tree to analyze
 * @return the type of {@code node}, without any annotations
 */
protected final AnnotatedTypeMirror type(Tree node) {
    boolean isDeclaration = TreeUtils.isTypeDeclaration(node);
    // Attempt to obtain the type via JCTree.
    if (TreeUtils.typeOf(node) != null) {
        AnnotatedTypeMirror result = toAnnotatedType(TreeUtils.typeOf(node), isDeclaration);
        return result;
    }
    // Attempt to obtain the type via TreePath (slower).
    TreePath path = this.getPath(node);
    assert path != null : "No path or type in tree: " + node;
    TypeMirror t = trees.getTypeMirror(path);
    assert validType(t) : "Invalid type " + t + " for node " + t;
    return toAnnotatedType(t, isDeclaration);
}
Also used : TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror)

Example 22 with TreePath

use of com.sun.source.util.TreePath in project checker-framework by typetools.

the class AnnotatedTypeFactory method getMostInnerClassOrMethod.

private final Element getMostInnerClassOrMethod(Tree tree) {
    if (visitorState.getMethodTree() != null) {
        return TreeUtils.elementFromDeclaration(visitorState.getMethodTree());
    }
    if (visitorState.getClassTree() != null) {
        return TreeUtils.elementFromDeclaration(visitorState.getClassTree());
    }
    TreePath path = getPath(tree);
    if (path == null) {
        ErrorReporter.errorAbort(String.format("AnnotatedTypeFactory.getMostInnerClassOrMethod: getPath(tree)=>null%n  TreePath.getPath(root, tree)=>%s\n  for tree (%s) = %s%n  root=%s", TreePath.getPath(root, tree), tree.getClass(), tree, root));
        // dead code
        return null;
    }
    for (Tree pathTree : path) {
        if (pathTree instanceof MethodTree) {
            return TreeUtils.elementFromDeclaration((MethodTree) pathTree);
        } else if (pathTree instanceof ClassTree) {
            return TreeUtils.elementFromDeclaration((ClassTree) pathTree);
        }
    }
    ErrorReporter.errorAbort("AnnotatedTypeFactory.getMostInnerClassOrMethod: cannot be here!");
    // dead code
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ReturnTree(com.sun.source.tree.ReturnTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree)

Example 23 with TreePath

use of com.sun.source.util.TreePath in project checker-framework by typetools.

the class Heuristics method matchParents.

/**
 * Determines whether a tree has a particular set of direct parents, ignoring blocks and
 * parentheses.
 *
 * <p>For example, to test whether an expression (specified by {@code path}) is immediately
 * contained by an if statement which is immediately contained in a method, one would invoke:
 *
 * <pre>
 * matchParents(path, Kind.IF, Kind.METHOD)
 * </pre>
 *
 * @param path the path to match
 * @param kinds the tree kinds to match against, in ascending order starting from the desired
 *     kind of the parent
 * @return true if the tree path matches the desired kinds, skipping blocks and parentheses, for
 *     as many kinds as specified
 */
public static boolean matchParents(TreePath path, Tree.Kind... kinds) {
    TreePath parentPath = path.getParentPath();
    boolean result = true;
    ArrayDeque<Tree.Kind> queue = new ArrayDeque<>(Arrays.asList(kinds));
    Tree tree;
    while ((tree = parentPath.getLeaf()) != null) {
        if (queue.isEmpty()) {
            break;
        }
        if (tree.getKind() == Tree.Kind.BLOCK || tree.getKind() == Tree.Kind.PARENTHESIZED) {
            parentPath = parentPath.getParentPath();
            continue;
        }
        result &= queue.removeFirst() == parentPath.getLeaf().getKind();
        parentPath = parentPath.getParentPath();
    }
    return result;
}
Also used : TreePath(com.sun.source.util.TreePath) IfTree(com.sun.source.tree.IfTree) BlockTree(com.sun.source.tree.BlockTree) ExpressionTree(com.sun.source.tree.ExpressionTree) StatementTree(com.sun.source.tree.StatementTree) UnaryTree(com.sun.source.tree.UnaryTree) Tree(com.sun.source.tree.Tree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ArrayDeque(java.util.ArrayDeque)

Example 24 with TreePath

use of com.sun.source.util.TreePath in project checker-framework by typetools.

the class AliasingVisitor method commonAssignmentCheck.

@Override
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey) {
    super.commonAssignmentCheck(varType, valueType, valueTree, errorKey);
    // If we are visiting a pseudo-assignment, visitorLeafKind is either
    // Kind.NEW_CLASS or Kind.METHOD_INVOCATION.
    TreePath path = visitorState.getPath();
    if (path == null) {
        return;
    }
    Kind visitorLeafKind = path.getLeaf().getKind();
    if (visitorLeafKind == Kind.NEW_CLASS || visitorLeafKind == Kind.METHOD_INVOCATION) {
        // Handling pseudo-assignments
        if (canBeLeaked(valueTree)) {
            Kind parentKind = visitorState.getPath().getParentPath().getLeaf().getKind();
            if (!varType.hasAnnotation(NonLeaked.class) && !(varType.hasAnnotation(LeakedToResult.class) && parentKind == Kind.EXPRESSION_STATEMENT)) {
                checker.report(Result.failure("unique.leaked"), valueTree);
            }
        }
    }
}
Also used : TreePath(com.sun.source.util.TreePath) Kind(com.sun.source.tree.Tree.Kind)

Example 25 with TreePath

use of com.sun.source.util.TreePath in project checker-framework by typetools.

the class LockVisitor method getLockExpressions.

private List<LockExpression> getLockExpressions(boolean implicitThis, AnnotationMirror gbAnno, Tree tree) {
    List<String> expressions = AnnotationUtils.getElementValueArray(gbAnno, "value", String.class, true);
    if (expressions.isEmpty()) {
        return Collections.emptyList();
    }
    TreePath currentPath = getCurrentPath();
    List<Receiver> params = FlowExpressions.getParametersOfEnclosingMethod(atypeFactory, currentPath);
    TypeMirror enclosingType = TreeUtils.typeOf(TreeUtils.enclosingClass(currentPath));
    Receiver pseudoReceiver = FlowExpressions.internalReprOfPseudoReceiver(currentPath, enclosingType);
    FlowExpressionContext exprContext = new FlowExpressionContext(pseudoReceiver, params, atypeFactory.getContext());
    Receiver self;
    if (implicitThis) {
        self = pseudoReceiver;
    } else if (TreeUtils.isExpressionTree(tree)) {
        self = FlowExpressions.internalReprOf(atypeFactory, (ExpressionTree) tree);
    } else {
        self = new Unknown(TreeUtils.typeOf(tree));
    }
    List<LockExpression> lockExpressions = new ArrayList<>();
    for (String expression : expressions) {
        lockExpressions.add(parseExpressionString(expression, exprContext, currentPath, self));
    }
    return lockExpressions;
}
Also used : TreePath(com.sun.source.util.TreePath) Unknown(org.checkerframework.dataflow.analysis.FlowExpressions.Unknown) GuardedByUnknown(org.checkerframework.checker.lock.qual.GuardedByUnknown) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

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