Search in sources :

Example 21 with VariableTree

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

the class InitializationChecker method getAllFields.

/**
 * Returns a list of all fields of the given class
 */
public static List<VariableTree> getAllFields(ClassTree clazz) {
    List<VariableTree> fields = new ArrayList<>();
    for (Tree t : clazz.getMembers()) {
        if (t.getKind().equals(Tree.Kind.VARIABLE)) {
            VariableTree vt = (VariableTree) t;
            fields.add(vt);
        }
    }
    return fields;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 22 with VariableTree

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

the class InitializationVisitor method checkFieldsInitialized.

/**
 * Checks that all fields (all static fields if {@code staticFields} is true) are initialized in
 * the given store.
 */
// TODO: the code for checking if fields are initialized should be re-written,
// as the current version contains quite a few ugly parts, is hard to understand,
// and it is likely that it does not take full advantage of the information
// about initialization we compute in
// GenericAnnotatedTypeFactory.initializationStaticStore and
// GenericAnnotatedTypeFactory.initializationStore.
protected void checkFieldsInitialized(Tree blockNode, boolean staticFields, Store store, List<? extends AnnotationMirror> receiverAnnotations) {
    // successfully
    if (store != null) {
        List<VariableTree> violatingFields = atypeFactory.getUninitializedInvariantFields(store, getCurrentPath(), staticFields, receiverAnnotations);
        if (staticFields) {
        // TODO: Why is nothing done for static fields?
        // Do we need the following?
        // violatingFields.removeAll(store.initializedFields);
        } else {
            // remove fields that have already been initialized by an
            // initializer block
            violatingFields.removeAll(initializedFields);
        }
        // Remove fields with a relevant @SuppressWarnings annotation.
        Iterator<VariableTree> itor = violatingFields.iterator();
        while (itor.hasNext()) {
            VariableTree f = itor.next();
            Element e = TreeUtils.elementFromTree(f);
            if (checker.shouldSuppressWarnings(e, COMMITMENT_FIELDS_UNINITIALIZED)) {
                itor.remove();
            }
        }
        if (!violatingFields.isEmpty()) {
            StringBuilder fieldsString = new StringBuilder();
            boolean first = true;
            for (VariableTree f : violatingFields) {
                if (!first) {
                    fieldsString.append(", ");
                }
                first = false;
                fieldsString.append(f.getName());
            }
            checker.report(Result.failure(COMMITMENT_FIELDS_UNINITIALIZED, fieldsString), blockNode);
        }
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) VariableTree(com.sun.source.tree.VariableTree)

Example 23 with VariableTree

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

the class InitializationVisitor method processClassTree.

@Override
public void processClassTree(ClassTree node) {
    // them later when checking constructors.
    for (Tree member : node.getMembers()) {
        if (member instanceof BlockTree && !((BlockTree) member).isStatic()) {
            BlockTree block = (BlockTree) member;
            Store store = atypeFactory.getRegularExitStore(block);
            if (store != null) {
                // Add field values for fields with an initializer.
                for (Pair<VariableElement, Value> t : store.getAnalysis().getFieldValues()) {
                    store.addInitializedField(t.first);
                }
                final List<VariableTree> init = atypeFactory.getInitializedInvariantFields(store, getCurrentPath());
                initializedFields.addAll(init);
            }
        }
    }
    super.processClassTree(node);
    // Is there a static initializer block?
    boolean hasStaticInitializer = false;
    for (Tree t : node.getMembers()) {
        switch(t.getKind()) {
            case BLOCK:
                if (((BlockTree) t).isStatic()) {
                    hasStaticInitializer = true;
                }
                break;
            default:
                break;
        }
    }
    // initializer (otherwise, errors are reported there).
    if (!hasStaticInitializer && node.getKind() == Kind.CLASS) {
        boolean isStatic = true;
        // See GenericAnnotatedTypeFactory.performFlowAnalysis for why we use
        // the regular exit store of the class here.
        Store store = atypeFactory.getRegularExitStore(node);
        // Add field values for fields with an initializer.
        for (Pair<VariableElement, Value> t : store.getAnalysis().getFieldValues()) {
            store.addInitializedField(t.first);
        }
        List<AnnotationMirror> receiverAnnotations = Collections.emptyList();
        checkFieldsInitialized(node, isStatic, store, receiverAnnotations);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) CFAbstractValue(org.checkerframework.framework.flow.CFAbstractValue) VariableTree(com.sun.source.tree.VariableTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) BlockTree(com.sun.source.tree.BlockTree) BlockTree(com.sun.source.tree.BlockTree) CFAbstractStore(org.checkerframework.framework.flow.CFAbstractStore) VariableElement(javax.lang.model.element.VariableElement)

Example 24 with VariableTree

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

the class LockVisitor method issueErrorIfGuardSatisfiedAnnotationInUnsupportedLocation.

/**
 * Issues an error if a GuardSatisfied annotation is found in a location other than a method
 * return type or parameter (including the receiver).
 *
 * @param annotationTree AnnotationTree used for error reporting and to help determine that an
 *     array parameter has no GuardSatisfied annotations except on the array type
 */
// TODO: Remove this method once @TargetLocations are enforced (i.e. once
// issue https://github.com/typetools/checker-framework/issues/515 is closed).
private void issueErrorIfGuardSatisfiedAnnotationInUnsupportedLocation(AnnotationTree annotationTree) {
    TreePath currentPath = getCurrentPath();
    TreePath path = getPathForLocalVariableRetrieval(currentPath);
    if (path != null) {
        Tree tree = path.getLeaf();
        Tree.Kind kind = tree.getKind();
        if (kind == Tree.Kind.METHOD) {
            // The @GuardSatisfied annotation is on the return type.
            return;
        } else if (kind == Tree.Kind.VARIABLE) {
            VariableTree varTree = (VariableTree) tree;
            Tree varTypeTree = varTree.getType();
            if (varTypeTree != null) {
                TreePath parentPath = path.getParentPath();
                if (parentPath != null && parentPath.getLeaf().getKind() == Tree.Kind.METHOD) {
                    Tree.Kind varTypeTreeKind = varTypeTree.getKind();
                    if (varTypeTreeKind == Tree.Kind.ANNOTATED_TYPE) {
                        AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) varTypeTree;
                        if (annotatedTypeTree.getUnderlyingType().getKind() != Tree.Kind.ARRAY_TYPE || annotatedTypeTree.getAnnotations().contains(annotationTree)) {
                            // Method parameter
                            return;
                        }
                    } else if (varTypeTreeKind != Tree.Kind.ARRAY_TYPE) {
                        // Method parameter or receiver
                        return;
                    }
                }
            }
        }
    }
    checker.report(Result.failure("guardsatisfied.location.disallowed"), annotationTree);
}
Also used : AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) Kind(com.sun.source.tree.Tree.Kind) TreePath(com.sun.source.util.TreePath) TypeKind(javax.lang.model.type.TypeKind) Kind(com.sun.source.tree.Tree.Kind) ElementKind(javax.lang.model.element.ElementKind) VariableTree(com.sun.source.tree.VariableTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) TreeUtils.getReceiverTree(org.checkerframework.javacutil.TreeUtils.getReceiverTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 25 with VariableTree

use of com.sun.source.tree.VariableTree in project st-js by st-js.

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
 *            a {@link com.sun.source.util.TreePath} object.
 * @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) 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)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)86 Tree (com.sun.source.tree.Tree)43 ClassTree (com.sun.source.tree.ClassTree)37 MethodTree (com.sun.source.tree.MethodTree)37 ExpressionTree (com.sun.source.tree.ExpressionTree)34 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)25 NewClassTree (com.sun.source.tree.NewClassTree)23 TreePath (com.sun.source.util.TreePath)23 IdentifierTree (com.sun.source.tree.IdentifierTree)22 JCTree (com.sun.tools.javac.tree.JCTree)21 MemberSelectTree (com.sun.source.tree.MemberSelectTree)19 AssignmentTree (com.sun.source.tree.AssignmentTree)17 ArrayList (java.util.ArrayList)17 BlockTree (com.sun.source.tree.BlockTree)16 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)13 Type (com.sun.tools.javac.code.Type)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 ReturnTree (com.sun.source.tree.ReturnTree)12 StatementTree (com.sun.source.tree.StatementTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)12