Search in sources :

Example 91 with VariableTree

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

the class NullnessVisitor method processClassTree.

@Override
public void processClassTree(ClassTree classTree) {
    Tree extendsClause = classTree.getExtendsClause();
    if (extendsClause != null) {
        reportErrorIfSupertypeContainsNullnessAnnotation(extendsClause);
    }
    for (Tree implementsClause : classTree.getImplementsClause()) {
        reportErrorIfSupertypeContainsNullnessAnnotation(implementsClause);
    }
    if (classTree.getKind() == Tree.Kind.ENUM) {
        for (Tree member : classTree.getMembers()) {
            if (member.getKind() == Tree.Kind.VARIABLE && TreeUtils.elementFromDeclaration((VariableTree) member).getKind() == ElementKind.ENUM_CONSTANT) {
                VariableTree varDecl = (VariableTree) member;
                List<? extends AnnotationTree> annoTrees = varDecl.getModifiers().getAnnotations();
                Tree type = varDecl.getType();
                if (atypeFactory.containsNullnessAnnotation(annoTrees, type)) {
                    checker.reportError(member, "nullness.on.enum");
                }
            }
        }
    }
    super.processClassTree(classTree);
}
Also used : VariableTree(com.sun.source.tree.VariableTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeCastTree(com.sun.source.tree.TypeCastTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SwitchTree(com.sun.source.tree.SwitchTree) ThrowTree(com.sun.source.tree.ThrowTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ExpressionTree(com.sun.source.tree.ExpressionTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree)

Example 92 with VariableTree

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

the class NullnessVisitor method checkExceptionParameter.

@Override
protected void checkExceptionParameter(CatchTree node) {
    VariableTree param = node.getParameter();
    List<? extends AnnotationTree> annoTrees = param.getModifiers().getAnnotations();
    Tree paramType = param.getType();
    if (atypeFactory.containsNullnessAnnotation(annoTrees, paramType)) {
        // This is a warning rather than an error because writing `@Nullable` could make sense
        // if the catch block re-assigns the variable to null.  (That would be bad style.)
        checker.reportWarning(param, "nullness.on.exception.parameter");
    }
// Don't call super.
// BasetypeVisitor forces annotations on exception parameters to be top, but because exceptions
// can never be null, the Nullness Checker does not require this check.
}
Also used : VariableTree(com.sun.source.tree.VariableTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeCastTree(com.sun.source.tree.TypeCastTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SwitchTree(com.sun.source.tree.SwitchTree) ThrowTree(com.sun.source.tree.ThrowTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ExpressionTree(com.sun.source.tree.ExpressionTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree)

Example 93 with VariableTree

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

the class LockVisitor method visitMethod.

/**
 * Issues an error if a method (explicitly or implicitly) annotated with @MayReleaseLocks has a
 * formal parameter or receiver (explicitly or implicitly) annotated with @GuardSatisfied. Also
 * issues an error if a synchronized method has a @LockingFree, @SideEffectFree, or @Pure
 * annotation.
 *
 * @param node the MethodTree of the method definition to visit
 */
@Override
public Void visitMethod(MethodTree node, Void p) {
    ExecutableElement methodElement = TreeUtils.elementFromDeclaration(node);
    issueErrorIfMoreThanOneLockPreconditionMethodAnnotationPresent(methodElement, node);
    SideEffectAnnotation sea = atypeFactory.methodSideEffectAnnotation(methodElement, true);
    if (sea == SideEffectAnnotation.MAYRELEASELOCKS) {
        boolean issueGSwithMRLWarning = false;
        VariableTree receiver = node.getReceiverParameter();
        if (receiver != null) {
            if (atypeFactory.getAnnotatedType(receiver).hasAnnotation(checkerGuardSatisfiedClass)) {
                issueGSwithMRLWarning = true;
            }
        }
        if (!issueGSwithMRLWarning) {
            // Skip loop if we already decided to issue the warning.
            for (VariableTree vt : node.getParameters()) {
                if (atypeFactory.getAnnotatedType(vt).hasAnnotation(checkerGuardSatisfiedClass)) {
                    issueGSwithMRLWarning = true;
                    break;
                }
            }
        }
        if (issueGSwithMRLWarning) {
            checker.reportError(node, "guardsatisfied.with.mayreleaselocks");
        }
    }
    // @GuardSatisfied without an index.
    if (methodElement != null && methodElement.getKind() != ElementKind.CONSTRUCTOR) {
        AnnotatedTypeMirror returnTypeATM = atypeFactory.getAnnotatedType(node).getReturnType();
        if (returnTypeATM != null && returnTypeATM.hasAnnotation(GuardSatisfied.class)) {
            int returnGuardSatisfiedIndex = atypeFactory.getGuardSatisfiedIndex(returnTypeATM);
            if (returnGuardSatisfiedIndex == -1) {
                checker.reportError(node, "guardsatisfied.return.must.have.index");
            }
        }
    }
    if (!sea.isWeakerThan(SideEffectAnnotation.LOCKINGFREE) && methodElement.getModifiers().contains(Modifier.SYNCHRONIZED)) {
        checker.reportError(node, "lockingfree.synchronized.method", sea);
    }
    return super.visitMethod(node, p);
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableTree(com.sun.source.tree.VariableTree) SideEffectAnnotation(org.checkerframework.checker.lock.LockAnnotatedTypeFactory.SideEffectAnnotation) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) GuardSatisfied(org.checkerframework.checker.lock.qual.GuardSatisfied)

Example 94 with VariableTree

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

the class MustCallTransfer method getOrCreateTempVar.

/**
 * Either returns the temporary variable associated with node, or creates one if one does not
 * exist.
 *
 * @param node a node, which must be an expression (not a statement)
 * @return a temporary variable node representing {@code node} that can be placed into a store
 */
@Nullable
private LocalVariableNode getOrCreateTempVar(Node node) {
    LocalVariableNode localVariableNode = atypeFactory.tempVars.get(node.getTree());
    if (localVariableNode == null) {
        VariableTree temp = createTemporaryVar(node);
        if (temp != null) {
            IdentifierTree identifierTree = treeBuilder.buildVariableUse(temp);
            localVariableNode = new LocalVariableNode(identifierTree);
            localVariableNode.setInSource(true);
            atypeFactory.tempVars.put(node.getTree(), localVariableNode);
        }
    }
    return localVariableNode;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) IdentifierTree(com.sun.source.tree.IdentifierTree) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 95 with VariableTree

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

the class TreeUtils method bindingPatternTreeGetVariable.

/**
 * Returns the binding variable of {@code bindingPatternTree}.
 *
 * @param bindingPatternTree the BindingPatternTree whose binding variable is returned
 * @return the binding variable of {@code bindingPatternTree}
 */
public static VariableTree bindingPatternTreeGetVariable(Tree bindingPatternTree) {
    try {
        Class<?> bindingPatternClass = Class.forName("com.sun.source.tree.BindingPatternTree");
        Method getVariableMethod = bindingPatternClass.getMethod("getVariable");
        VariableTree variableTree = (VariableTree) getVariableMethod.invoke(bindingPatternTree);
        if (variableTree != null) {
            return variableTree;
        }
        throw new BugInCF("TreeUtils.bindingPatternTreeGetVariable: variable is null for tree: %s", bindingPatternTree);
    } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        throw new BugInCF("TreeUtils.bindingPatternTreeGetVariable: reflection failed for tree: %s", bindingPatternTree, e);
    }
}
Also used : VariableTree(com.sun.source.tree.VariableTree) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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