Search in sources :

Example 1 with CompilerMessageKey

use of org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey in project checker-framework by typetools.

the class InitializationVisitor method commonAssignmentCheck.

@Override
protected void commonAssignmentCheck(Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey) {
    // field write of the form x.f = y
    if (TreeUtils.isFieldAccess(varTree)) {
        // cast is safe: a field access can only be an IdentifierTree or
        // MemberSelectTree
        ExpressionTree lhs = (ExpressionTree) varTree;
        ExpressionTree y = valueExp;
        Element el = TreeUtils.elementFromUse(lhs);
        AnnotatedTypeMirror xType = atypeFactory.getReceiverType(lhs);
        AnnotatedTypeMirror yType = atypeFactory.getAnnotatedType(y);
        // the special FBC rules do not apply if there is an explicit
        // UnknownInitialization annotation
        Set<AnnotationMirror> fieldAnnotations = atypeFactory.getAnnotatedType(TreeUtils.elementFromUse(lhs)).getAnnotations();
        if (!AnnotationUtils.containsSameIgnoringValues(fieldAnnotations, atypeFactory.UNCLASSIFIED)) {
            if (!ElementUtils.isStatic(el) && !(atypeFactory.isCommitted(yType) || atypeFactory.isFree(xType) || atypeFactory.isFbcBottom(yType))) {
                @CompilerMessageKey String err;
                if (atypeFactory.isCommitted(xType)) {
                    err = COMMITMENT_INVALID_FIELD_WRITE_COMMITTED;
                } else {
                    err = COMMITMENT_INVALID_FIELD_WRITE_UNCLASSIFIED;
                }
                checker.report(Result.failure(err, varTree), varTree);
                // prevent issuing another errow about subtyping
                return;
            }
        }
    }
    super.commonAssignmentCheck(varTree, valueExp, errorKey);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) CompilerMessageKey(org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 2 with CompilerMessageKey

use of org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey in project checker-framework by typetools.

the class BaseTypeVisitor method checkContractsAtMethodDeclaration.

private void checkContractsAtMethodDeclaration(MethodTree node, ExecutableElement methodElement, List<String> formalParamNames, boolean abstractMethod) {
    FlowExpressionContext flowExprContext = null;
    List<Contract> contracts = contractsUtils.getContracts(methodElement);
    for (Contract contract : contracts) {
        String expression = contract.expression;
        AnnotationMirror annotation = contract.annotation;
        if (flowExprContext == null) {
            flowExprContext = FlowExpressionContext.buildContextForMethodDeclaration(node, getCurrentPath(), checker.getContext());
        }
        annotation = standardizeAnnotationFromContract(annotation, flowExprContext, getCurrentPath());
        FlowExpressions.Receiver expr = null;
        try {
            expr = FlowExpressionParseUtil.parse(expression, flowExprContext, getCurrentPath(), false);
        } catch (FlowExpressionParseException e) {
            checker.report(e.getResult(), node);
        }
        if (expr != null && !abstractMethod) {
            switch(contract.kind) {
                case POSTCONDTION:
                    checkPostcondition(node, annotation, expr);
                    break;
                case CONDITIONALPOSTCONDTION:
                    checkConditionalPostcondition(node, annotation, expr, ((ConditionalPostcondition) contract).annoResult);
                    break;
                case PRECONDITION:
                    // Preconditions are checked at method invocations, not declarations
                    break;
            }
        }
        if (formalParamNames != null && formalParamNames.contains(expression)) {
            @SuppressWarnings("CompilerMessages") @CompilerMessageKey String key = "contracts." + contract.kind.errorKey + ".expression.parameter.name";
            checker.report(Result.warning(key, node.getName().toString(), expression, formalParamNames.indexOf(expression) + 1, expression), node);
        }
        checkParametersAreEffectivelyFinal(node, methodElement, expression);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) CompilerMessageKey(org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) Contract(org.checkerframework.framework.util.ContractsUtils.Contract)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 CompilerMessageKey (org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey)2 ExpressionTree (com.sun.source.tree.ExpressionTree)1 Element (javax.lang.model.element.Element)1 VariableElement (javax.lang.model.element.VariableElement)1 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)1 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 Contract (org.checkerframework.framework.util.ContractsUtils.Contract)1 FlowExpressionContext (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext)1 FlowExpressionParseException (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)1