Search in sources :

Example 21 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class InitializationVisitor method checkContract.

@Override
protected boolean checkContract(Receiver expr, AnnotationMirror necessaryAnnotation, AnnotationMirror inferredAnnotation, CFAbstractStore<?, ?> store) {
    // also use the information about initialized fields to check contracts
    final AnnotationMirror invariantAnno = atypeFactory.getFieldInvariantAnnotation();
    if (atypeFactory.getQualifierHierarchy().isSubtype(invariantAnno, necessaryAnnotation)) {
        if (expr instanceof FieldAccess) {
            FieldAccess fa = (FieldAccess) expr;
            if (fa.getReceiver() instanceof ThisReference || fa.getReceiver() instanceof ClassName) {
                @SuppressWarnings("unchecked") Store s = (Store) store;
                if (s.isFieldInitialized(fa.getField())) {
                    AnnotatedTypeMirror fieldType = atypeFactory.getAnnotatedType(fa.getField());
                    // is this an invariant-field?
                    if (AnnotationUtils.containsSame(fieldType.getAnnotations(), invariantAnno)) {
                        return true;
                    }
                }
            } else {
                Set<AnnotationMirror> recvAnnoSet;
                @SuppressWarnings("unchecked") Value value = (Value) store.getValue(fa.getReceiver());
                if (value != null) {
                    recvAnnoSet = value.getAnnotations();
                } else if (fa.getReceiver() instanceof LocalVariable) {
                    Element elem = ((LocalVariable) fa.getReceiver()).getElement();
                    AnnotatedTypeMirror recvType = atypeFactory.getAnnotatedType(elem);
                    recvAnnoSet = recvType.getAnnotations();
                } else {
                    // Is there anything better we could do?
                    return false;
                }
                boolean isRecvCommitted = false;
                for (AnnotationMirror anno : recvAnnoSet) {
                    if (atypeFactory.isCommitted(anno)) {
                        isRecvCommitted = true;
                    }
                }
                AnnotatedTypeMirror fieldType = atypeFactory.getAnnotatedType(fa.getField());
                // has the invariant type.
                if (isRecvCommitted && AnnotationUtils.containsSame(fieldType.getAnnotations(), invariantAnno)) {
                    return true;
                }
            }
        }
    }
    return super.checkContract(expr, necessaryAnnotation, inferredAnnotation, store);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) CFAbstractValue(org.checkerframework.framework.flow.CFAbstractValue) LocalVariable(org.checkerframework.dataflow.analysis.FlowExpressions.LocalVariable) CFAbstractStore(org.checkerframework.framework.flow.CFAbstractStore) FieldAccess(org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess) ThisReference(org.checkerframework.dataflow.analysis.FlowExpressions.ThisReference) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 22 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class LockVisitor method commonAssignmentCheck.

@Override
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey) {
    Kind valueTreeKind = valueTree.getKind();
    switch(valueTreeKind) {
        case NEW_CLASS:
        case NEW_ARRAY:
            // Do NOT do this if the LHS is @GuardedByBottom.
            if (!varType.hasAnnotation(GuardedByBottom.class)) {
                return;
            }
            break;
        case INT_LITERAL:
        case LONG_LITERAL:
        case FLOAT_LITERAL:
        case DOUBLE_LITERAL:
        case BOOLEAN_LITERAL:
        case CHAR_LITERAL:
        case STRING_LITERAL:
            // Do NOT do this if the LHS is @GuardedByBottom.
            if (!varType.hasAnnotation(GuardedByBottom.class)) {
                return;
            }
            break;
        default:
    }
    if (varType.hasAnnotation(GuardSatisfied.class)) {
        if (valueType.hasAnnotation(GuardedBy.class)) {
            checkLock(valueTree, valueType.getAnnotation(GuardedBy.class));
            return;
        } else if (valueType.hasAnnotation(GuardSatisfied.class)) {
            if (!errorKey.equals("argument.type.incompatible")) {
                // If both @GuardSatisfied have no index, the assignment is not allowed because
                // the LHS and RHS expressions may be guarded by different lock expressions.
                // The assignment is allowed when matching a formal parameter to an actual
                // parameter (see the if block above).
                int varTypeGuardSatisfiedIndex = atypeFactory.getGuardSatisfiedIndex(varType);
                int valueTypeGuardSatisfiedIndex = atypeFactory.getGuardSatisfiedIndex(valueType);
                if (varTypeGuardSatisfiedIndex == -1 && valueTypeGuardSatisfiedIndex == -1) {
                    checker.report(Result.failure("guardsatisfied.assignment.disallowed", varType, valueType), valueTree);
                }
            } else {
                return;
            }
        } else if (!atypeFactory.getTypeHierarchy().isSubtype(valueType, varType)) {
            // Special case: replace the @GuardSatisfied primary annotation on the LHS with
            // @GuardedBy({}) and see if it type checks.
            AnnotatedTypeMirror varType2 = // TODO: Would shallowCopy be sufficient?
            varType.deepCopy();
            varType2.replaceAnnotation(atypeFactory.GUARDEDBY);
            if (atypeFactory.getTypeHierarchy().isSubtype(valueType, varType2)) {
                return;
            }
        }
    }
    super.commonAssignmentCheck(varType, valueType, valueTree, errorKey);
}
Also used : GuardedBy(org.checkerframework.checker.lock.qual.GuardedBy) GuardedByBottom(org.checkerframework.checker.lock.qual.GuardedByBottom) TypeKind(javax.lang.model.type.TypeKind) Kind(com.sun.source.tree.Tree.Kind) ElementKind(javax.lang.model.element.ElementKind) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) GuardSatisfied(org.checkerframework.checker.lock.qual.GuardSatisfied)

Example 23 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class NullnessAnnotatedTypeFactory method adaptGetClassReturnTypeToReceiver.

@Override
public void adaptGetClassReturnTypeToReceiver(final AnnotatedExecutableType getClassType, final AnnotatedTypeMirror receiverType) {
    super.adaptGetClassReturnTypeToReceiver(getClassType, receiverType);
    // Make the wildcard always @NonNull, regardless of the declared type.
    final AnnotatedDeclaredType returnAdt = (AnnotatedDeclaredType) getClassType.getReturnType();
    final List<AnnotatedTypeMirror> typeArgs = returnAdt.getTypeArguments();
    final AnnotatedWildcardType classWildcardArg = (AnnotatedWildcardType) typeArgs.get(0);
    classWildcardArg.getExtendsBoundField().replaceAnnotation(NONNULL);
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 24 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class GuiEffectVisitor method visitReturn.

// Returning a lambda expression also triggers inference based on the method's return type.
@Override
public Void visitReturn(ReturnTree node, Void p) {
    Void result = super.visitReturn(node, p);
    if (node.getExpression() != null && node.getExpression().getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
        // Unfortunatelly, need to duplicate a fair bit of BaseTypeVisitor.visitReturn after lambdas have been
        // inferred.
        Pair<Tree, AnnotatedTypeMirror> preAssCtxt = visitorState.getAssignmentContext();
        try {
            Tree enclosing = TreeUtils.enclosingMethodOrLambda(getCurrentPath());
            AnnotatedTypeMirror ret = null;
            if (enclosing.getKind() == Tree.Kind.METHOD) {
                MethodTree enclosingMethod = (MethodTree) enclosing;
                boolean valid = validateTypeOf(enclosing);
                if (valid) {
                    ret = atypeFactory.getMethodReturnType(enclosingMethod, node);
                }
            } else {
                ret = atypeFactory.getFnInterfaceFromTree((LambdaExpressionTree) enclosing).second.getReturnType();
            }
            if (ret != null) {
                visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
                lambdaAssignmentCheck(ret, (LambdaExpressionTree) node.getExpression(), "return.type.incompatible");
            }
        } finally {
            visitorState.setAssignmentContext(preAssCtxt);
        }
    }
    return result;
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) MethodTree(com.sun.source.tree.MethodTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 25 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class GuiEffectVisitor method lambdaAssignmentCheck.

// For assignments and local variable initialization:
// Check for @UI annotations on the lhs, use them to infer @UI types on lambda expressions in the rhs
private void lambdaAssignmentCheck(AnnotatedTypeMirror varType, LambdaExpressionTree lambdaExp, @CompilerMessageKey String errorKey) {
    AnnotatedTypeMirror lambdaType = atypeFactory.getAnnotatedType(lambdaExp);
    assert lambdaType != null : "null type for expression: " + lambdaExp;
    commonAssignmentCheck(varType, lambdaType, lambdaExp, errorKey);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)299 AnnotationMirror (javax.lang.model.element.AnnotationMirror)84 ExpressionTree (com.sun.source.tree.ExpressionTree)53 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)44 ExecutableElement (javax.lang.model.element.ExecutableElement)41 Tree (com.sun.source.tree.Tree)40 TypeMirror (javax.lang.model.type.TypeMirror)38 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)38 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)38 MethodTree (com.sun.source.tree.MethodTree)34 ArrayList (java.util.ArrayList)34 BugInCF (org.checkerframework.javacutil.BugInCF)32 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)30 TypeElement (javax.lang.model.element.TypeElement)29 VariableElement (javax.lang.model.element.VariableElement)29 VariableTree (com.sun.source.tree.VariableTree)28 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)27 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)26 ClassTree (com.sun.source.tree.ClassTree)25 Map (java.util.Map)24