Search in sources :

Example 76 with ClassTree

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

the class CFGTranslationPhaseOne method visitVariable.

@Override
public Node visitVariable(VariableTree tree, Void p) {
    // see JLS 14.4
    boolean isField = false;
    if (getCurrentPath().getParentPath() != null) {
        Tree.Kind kind = TreeUtils.getKindRecordAsClass(getCurrentPath().getParentPath().getLeaf());
        // CLASS includes records.
        if (kind == Tree.Kind.CLASS || kind == Tree.Kind.INTERFACE || kind == Tree.Kind.ENUM) {
            isField = true;
        }
    }
    Node node = null;
    ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
    TypeElement classElem = TreeUtils.elementFromDeclaration(enclosingClass);
    Node receiver = new ImplicitThisNode(classElem.asType());
    if (isField) {
        ExpressionTree initializer = tree.getInitializer();
        assert initializer != null;
        node = translateAssignment(tree, new FieldAccessNode(tree, TreeUtils.elementFromDeclaration(tree), receiver), initializer);
    } else {
        // local variable definition
        VariableDeclarationNode decl = new VariableDeclarationNode(tree);
        extendWithNode(decl);
        // initializer
        ExpressionTree initializer = tree.getInitializer();
        if (initializer != null) {
            node = translateAssignment(tree, new LocalVariableNode(tree, receiver), initializer);
        }
    }
    return node;
}
Also used : Kind(com.sun.source.tree.Tree.Kind) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) TypeElement(javax.lang.model.element.TypeElement) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) BreakTree(com.sun.source.tree.BreakTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ContinueTree(com.sun.source.tree.ContinueTree) CaseTree(com.sun.source.tree.CaseTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SwitchTree(com.sun.source.tree.SwitchTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) ErroneousTree(com.sun.source.tree.ErroneousTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 77 with ClassTree

use of com.sun.source.tree.ClassTree 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 78 with ClassTree

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

the class InitializationAnnotatedTypeFactory method setSelfTypeInInitializationCode.

/**
 * Side-effects argument {@code selfType} to make it @Initialized or @UnderInitialization,
 * depending on whether all fields have been set.
 *
 * @param tree a tree
 * @param selfType the type to side-effect
 * @param path a path
 */
protected void setSelfTypeInInitializationCode(Tree tree, AnnotatedDeclaredType selfType, TreePath path) {
    ClassTree enclosingClass = TreePathUtil.enclosingClass(path);
    Type classType = ((JCTree) enclosingClass).type;
    AnnotationMirror annotation = null;
    // there might still be subclasses that need initialization.
    if (areAllFieldsInitializedOnly(enclosingClass)) {
        Store store = getStoreBefore(tree);
        if (store != null && getUninitializedInvariantFields(store, path, false, Collections.emptyList()).isEmpty()) {
            if (classType.isFinal()) {
                annotation = INITIALIZED;
            } else {
                annotation = createUnderInitializationAnnotation(classType);
            }
        }
    }
    if (annotation == null) {
        annotation = getUnderInitializationAnnotationOfSuperType(classType);
    }
    selfType.replaceAnnotation(annotation);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) Type(com.sun.tools.javac.code.Type) DeclaredType(javax.lang.model.type.DeclaredType) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 79 with ClassTree

use of com.sun.source.tree.ClassTree 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 80 with ClassTree

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

the class InitializationAnnotatedTypeFactory method findTopLevelClassMemberForTree.

/**
 * In the first enclosing class, find the path to the top-level member that contains {@code path}.
 *
 * @param path the path whose leaf is the target
 * @return path to a top-level member containing the leaf of {@code path}
 */
// AST node comparison
@SuppressWarnings("interning:not.interned")
private TreePath findTopLevelClassMemberForTree(TreePath path) {
    if (TreeUtils.isClassTree(path.getLeaf())) {
        path = path.getParentPath();
        if (path == null) {
            return null;
        }
    }
    ClassTree enclosingClass = TreePathUtil.enclosingClass(path);
    if (enclosingClass != null) {
        List<? extends Tree> classMembers = enclosingClass.getMembers();
        TreePath searchPath = path;
        while (searchPath.getParentPath() != null && searchPath.getParentPath().getLeaf() != enclosingClass) {
            searchPath = searchPath.getParentPath();
            if (classMembers.contains(searchPath.getLeaf())) {
                return searchPath;
            }
        }
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewClassTree(com.sun.source.tree.NewClassTree) 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