Search in sources :

Example 96 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class TreePathUtil method isTreeInStaticScope.

/**
 * Returns true if the leaf of the tree path is in a static scope.
 *
 * @param path TreePath whose leaf may or may not be in static scope
 * @return true if the leaf of the tree path is in a static scope
 */
public static boolean isTreeInStaticScope(TreePath path) {
    MethodTree enclosingMethod = enclosingMethod(path);
    if (enclosingMethod != null) {
        return enclosingMethod.getModifiers().getFlags().contains(Modifier.STATIC);
    }
    // no enclosing method, check for static or initializer block
    BlockTree block = enclosingTopLevelBlock(path);
    if (block != null) {
        return block.isStatic();
    }
    // check if its in a variable initializer
    Tree t = enclosingVariable(path);
    if (t != null) {
        return ((VariableTree) t).getModifiers().getFlags().contains(Modifier.STATIC);
    }
    ClassTree classTree = enclosingClass(path);
    if (classTree != null) {
        return classTree.getModifiers().getFlags().contains(Modifier.STATIC);
    }
    return false;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) ClassTree(com.sun.source.tree.ClassTree) BlockTree(com.sun.source.tree.BlockTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) BlockTree(com.sun.source.tree.BlockTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 97 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class TreePathUtil method getAssignmentContext.

/**
 * Returns the "assignment context" for the leaf of {@code treePath}, which is often the leaf of
 * the parent of {@code treePath}. (Does not handle pseudo-assignment of an argument to a
 * parameter or a receiver expression to a receiver.) This is not the same as {@code
 * org.checkerframework.dataflow.cfg.node.AssignmentContext}, which represents the left-hand side
 * rather than the assignment itself.
 *
 * <p>The assignment context for {@code treePath} is the leaf of its parent, if that leaf is one
 * of the following trees:
 *
 * <ul>
 *   <li>AssignmentTree
 *   <li>CompoundAssignmentTree
 *   <li>MethodInvocationTree
 *   <li>NewArrayTree
 *   <li>NewClassTree
 *   <li>ReturnTree
 *   <li>VariableTree
 * </ul>
 *
 * If the parent is a ConditionalExpressionTree we need to distinguish two cases: If the leaf is
 * either the then or else branch of the ConditionalExpressionTree, then recurse on the parent. If
 * the leaf is the condition of the ConditionalExpressionTree, then return null to not consider
 * this assignment context.
 *
 * <p>If the leaf is a ParenthesizedTree, then recurse on the parent.
 *
 * <p>Otherwise, null is returned.
 *
 * @param treePath a path
 * @return the assignment context as described, {@code null} otherwise
 */
@Nullable
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath parentPath = treePath.getParentPath();
    if (parentPath == null) {
        return null;
    }
    Tree parent = parentPath.getLeaf();
    switch(parent.getKind()) {
        // See below for CompoundAssignmentTree.
        case ASSIGNMENT:
        case METHOD_INVOCATION:
        case NEW_ARRAY:
        case NEW_CLASS:
        case RETURN:
        case VARIABLE:
            return parent;
        case CONDITIONAL_EXPRESSION:
            ConditionalExpressionTree cet = (ConditionalExpressionTree) parent;
            // AST node comparison
            @SuppressWarnings("interning:not.interned") boolean conditionIsLeaf = (cet.getCondition() == treePath.getLeaf());
            if (conditionIsLeaf) {
                // No point in going on.
                return null;
            }
            // Otherwise use the context of the ConditionalExpressionTree.
            return getAssignmentContext(parentPath);
        case PARENTHESIZED:
            return getAssignmentContext(parentPath);
        default:
            // so use instanceof rather than listing all 11.
            if (parent instanceof CompoundAssignmentTree) {
                return parent;
            }
            return null;
    }
}
Also used : TreePath(com.sun.source.util.TreePath) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) BlockTree(com.sun.source.tree.BlockTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 98 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class SameLenTransfer method addInformationFromPreconditions.

/**
 * Overridden to ensure that SameLen annotations on method parameters are symmetric.
 */
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
    super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
    List<? extends VariableTree> paramTrees = methodTree.getParameters();
    int numParams = paramTrees.size();
    List<String> paramNames = new ArrayList<>(numParams);
    List<AnnotatedTypeMirror> params = new ArrayList<>(numParams);
    for (VariableTree tree : paramTrees) {
        paramNames.add(tree.getName().toString());
        params.add(aTypeFactory.getAnnotatedType(tree));
    }
    for (int index = 0; index < numParams; index++) {
        // If the parameter has a samelen annotation, then look for other parameters in that
        // annotation and propagate default the other annotation so that it is symmetric.
        AnnotatedTypeMirror atm = params.get(index);
        AnnotationMirror sameLenAnno = atm.getAnnotation(SameLen.class);
        if (sameLenAnno == null) {
            continue;
        }
        List<String> values = AnnotationUtils.getElementValueArray(sameLenAnno, aTypeFactory.sameLenValueElement, String.class);
        for (String value : values) {
            int otherParamIndex = paramNames.indexOf(value);
            if (otherParamIndex == -1) {
                continue;
            }
            // the SameLen value is in the list of params, so modify the type of
            // that param in the store
            AnnotationMirror newSameLen = aTypeFactory.createSameLen(Collections.singletonList(paramNames.get(index)));
            JavaExpression otherParamRec = JavaExpression.fromVariableTree(paramTrees.get(otherParamIndex));
            info.insertValuePermitNondeterministic(otherParamRec, newSameLen);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ArrayList(java.util.ArrayList) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 99 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method getUninitializedFields.

/**
 * Returns the fields that are not yet initialized in a given store. The result is a pair of
 * lists:
 *
 * <ul>
 *   <li>fields that are not yet initialized and have the invariant annotation
 *   <li>fields that are not yet initialized and do not have the invariant annotation
 * </ul>
 *
 * @param store a store
 * @param path the current path, used to determine the current class
 * @param isStatic whether to report static fields or instance fields
 * @param receiverAnnotations the annotations on the receiver
 * @return the fields that are not yet initialized in a given store (a pair of lists)
 */
public Pair<List<VariableTree>, List<VariableTree>> getUninitializedFields(Store store, TreePath path, boolean isStatic, Collection<? extends AnnotationMirror> receiverAnnotations) {
    ClassTree currentClass = TreePathUtil.enclosingClass(path);
    List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
    List<VariableTree> uninitWithInvariantAnno = new ArrayList<>();
    List<VariableTree> uninitWithoutInvariantAnno = new ArrayList<>();
    for (VariableTree field : fields) {
        if (isUnused(field, receiverAnnotations)) {
            // don't consider unused fields
            continue;
        }
        VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
        if (ElementUtils.isStatic(fieldElem) == isStatic) {
            // Has the field been initialized?
            if (!store.isFieldInitialized(fieldElem)) {
                // Does this field need to satisfy the invariant?
                if (hasFieldInvariantAnnotation(field)) {
                    uninitWithInvariantAnno.add(field);
                } else {
                    uninitWithoutInvariantAnno.add(field);
                }
            }
        }
    }
    return Pair.of(uninitWithInvariantAnno, uninitWithoutInvariantAnno);
}
Also used : NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 100 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class LowerBoundTransfer method addInformationFromPreconditions.

/**
 * Adds a default NonNegative annotation to every character. Implements case 33.
 */
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
    super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
    List<? extends VariableTree> paramTrees = methodTree.getParameters();
    for (VariableTree variableTree : paramTrees) {
        if (TreeUtils.typeOf(variableTree).getKind() == TypeKind.CHAR) {
            JavaExpression je = JavaExpression.fromVariableTree(variableTree);
            info.insertValuePermitNondeterministic(je, aTypeFactory.NN);
        }
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) VariableTree(com.sun.source.tree.VariableTree)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)125 Tree (com.sun.source.tree.Tree)58 MethodTree (com.sun.source.tree.MethodTree)57 ClassTree (com.sun.source.tree.ClassTree)55 ExpressionTree (com.sun.source.tree.ExpressionTree)50 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)38 NewClassTree (com.sun.source.tree.NewClassTree)38 IdentifierTree (com.sun.source.tree.IdentifierTree)32 MemberSelectTree (com.sun.source.tree.MemberSelectTree)30 TreePath (com.sun.source.util.TreePath)27 JCTree (com.sun.tools.javac.tree.JCTree)27 AssignmentTree (com.sun.source.tree.AssignmentTree)24 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)23 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)22 ArrayList (java.util.ArrayList)22 BlockTree (com.sun.source.tree.BlockTree)21 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)21 AnnotationTree (com.sun.source.tree.AnnotationTree)20 BinaryTree (com.sun.source.tree.BinaryTree)19 NewArrayTree (com.sun.source.tree.NewArrayTree)19