Search in sources :

Example 46 with ClassTree

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

the class MustCallTransfer method createTemporaryVar.

/**
 * Creates a variable declaration for the given expression node, if possible.
 *
 * @param node an expression node
 * @return a variable tree for the node, or null if an appropriate containing element cannot be
 *     located
 */
@Nullable
protected VariableTree createTemporaryVar(Node node) {
    ExpressionTree tree = (ExpressionTree) node.getTree();
    TypeMirror treeType = TreeUtils.typeOf(tree);
    Element enclosingElement;
    TreePath path = atypeFactory.getPath(tree);
    if (path == null) {
        enclosingElement = TreeUtils.elementFromTree(tree).getEnclosingElement();
    } else {
        ClassTree classTree = TreePathUtil.enclosingClass(path);
        enclosingElement = TreeUtils.elementFromTree(classTree);
    }
    if (enclosingElement == null) {
        return null;
    }
    // Declare and initialize a new, unique variable
    VariableTree tmpVarTree = treeBuilder.buildVariableDecl(treeType, uniqueName("temp-var"), enclosingElement, tree);
    return tmpVarTree;
}
Also used : TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror) Element(javax.lang.model.element.Element) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 47 with ClassTree

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

the class InitializationAnnotatedTypeFactory method areAllFieldsInitializedOnly.

/**
 * Are all fields initialized-only?
 *
 * @param classTree the class to query
 * @return true if all fields are initialized-only
 */
protected boolean areAllFieldsInitializedOnly(ClassTree classTree) {
    for (Tree member : classTree.getMembers()) {
        if (member.getKind() != Tree.Kind.VARIABLE) {
            continue;
        }
        VariableTree var = (VariableTree) member;
        VariableElement varElt = TreeUtils.elementFromDeclaration(var);
        // var is not initialized-only
        if (getDeclAnnotation(varElt, NotOnlyInitialized.class) != null) {
            // not of constructor which is where this is used
            if (!varElt.getModifiers().contains(Modifier.STATIC)) {
                return false;
            }
        }
    }
    return true;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) LiteralTree(com.sun.source.tree.LiteralTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) VariableElement(javax.lang.model.element.VariableElement) NotOnlyInitialized(org.checkerframework.checker.initialization.qual.NotOnlyInitialized)

Example 48 with ClassTree

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

the class InitializationAnnotatedTypeFactory method getInitializedInvariantFields.

/**
 * Returns the fields that have the invariant annotation and are initialized in a given store.
 *
 * @param store a store
 * @param path the current path; used to compute the current class
 * @return the fields that have the invariant annotation and are initialized in a given store
 */
public List<VariableTree> getInitializedInvariantFields(Store store, TreePath path) {
    // TODO: Instead of passing the TreePath around, can we use
    // getCurrentClassTree?
    ClassTree currentClass = TreePathUtil.enclosingClass(path);
    List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
    List<VariableTree> initializedFields = new ArrayList<>();
    for (VariableTree field : fields) {
        VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
        if (!ElementUtils.isStatic(fieldElem)) {
            // Does this field need to satisfy the invariant?
            if (hasFieldInvariantAnnotation(field)) {
                // Has the field been initialized?
                if (store.isFieldInitialized(fieldElem)) {
                    initializedFields.add(field);
                }
            }
        }
    }
    return initializedFields;
}
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 49 with ClassTree

use of com.sun.source.tree.ClassTree 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.getKind() == Tree.Kind.BLOCK && !((BlockTree) member).isStatic()) {
            BlockTree block = (BlockTree) member;
            Store store = atypeFactory.getRegularExitStore(block);
            // Add field values for fields with an initializer.
            for (FieldInitialValue<Value> fieldInitialValue : store.getAnalysis().getFieldInitialValues()) {
                if (fieldInitialValue.initializer != null) {
                    store.addInitializedField(fieldInitialValue.fieldDecl.getField());
                }
            }
            final List<VariableTree> init = atypeFactory.getInitializedInvariantFields(store, getCurrentPath());
            initializedFields.addAll(init);
        }
    }
    super.processClassTree(node);
    // Warn about uninitialized static fields.
    Tree.Kind nodeKind = node.getKind();
    // must be initialized.  Java forbids uninitialized variables and static initalizer blocks.
    if (nodeKind != Tree.Kind.INTERFACE && nodeKind != Tree.Kind.ANNOTATION_TYPE) {
        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 (FieldInitialValue<Value> fieldInitialValue : store.getAnalysis().getFieldInitialValues()) {
            if (fieldInitialValue.initializer != null) {
                store.addInitializedField(fieldInitialValue.fieldDecl.getField());
            }
        }
        List<AnnotationMirror> receiverAnnotations = Collections.emptyList();
        checkFieldsInitialized(node, isStatic, store, receiverAnnotations);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) CFAbstractValue(org.checkerframework.framework.flow.CFAbstractValue) FieldInitialValue(org.checkerframework.framework.flow.CFAbstractAnalysis.FieldInitialValue) VariableTree(com.sun.source.tree.VariableTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) 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)

Example 50 with ClassTree

use of com.sun.source.tree.ClassTree 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() == 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)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)119 Tree (com.sun.source.tree.Tree)76 MethodTree (com.sun.source.tree.MethodTree)66 VariableTree (com.sun.source.tree.VariableTree)59 NewClassTree (com.sun.source.tree.NewClassTree)48 ExpressionTree (com.sun.source.tree.ExpressionTree)45 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)40 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)31 AnnotationTree (com.sun.source.tree.AnnotationTree)29 BlockTree (com.sun.source.tree.BlockTree)28 IdentifierTree (com.sun.source.tree.IdentifierTree)27 NewArrayTree (com.sun.source.tree.NewArrayTree)26 AssignmentTree (com.sun.source.tree.AssignmentTree)25 MemberSelectTree (com.sun.source.tree.MemberSelectTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)24 TypeElement (javax.lang.model.element.TypeElement)24 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)23 ArrayList (java.util.ArrayList)23 ReturnTree (com.sun.source.tree.ReturnTree)22 TreePath (com.sun.source.util.TreePath)22