Search in sources :

Example 1 with DependentTypesError

use of org.checkerframework.framework.util.dependenttypes.DependentTypesError in project checker-framework by typetools.

the class LockAnnotatedTypeFactory method createDependentTypesHelper.

@Override
protected DependentTypesHelper createDependentTypesHelper() {
    return new DependentTypesHelper(this) {

        @Override
        protected void reportErrors(Tree errorTree, List<DependentTypesError> errors) {
            // If the error message is NOT_EFFECTIVELY_FINAL, then report lock.expression.not
            // .final instead of an expression.unparsable.type.invalid error.
            List<DependentTypesError> superErrors = new ArrayList<>();
            for (DependentTypesError error : errors) {
                if (error.error.equals(NOT_EFFECTIVELY_FINAL)) {
                    checker.report(Result.failure("lock.expression.not.final", error.expression), errorTree);
                } else {
                    superErrors.add(error);
                }
            }
            super.reportErrors(errorTree, superErrors);
        }

        @Override
        protected String standardizeString(String expression, FlowExpressionContext context, TreePath localScope, boolean useLocalScope) {
            if (DependentTypesError.isExpressionError(expression)) {
                return expression;
            }
            // Adds logic to parse <self> expression, which only the Lock Checker uses.
            if (LockVisitor.selfReceiverPattern.matcher(expression).matches()) {
                return expression;
            }
            try {
                FlowExpressions.Receiver result = FlowExpressionParseUtil.parse(expression, context, localScope, useLocalScope);
                if (result == null) {
                    return new DependentTypesError(expression, " ").toString();
                }
                if (!isExpressionEffectivelyFinal(result)) {
                    // NOT_EFFECTIVELY_FINAL error string.
                    return new DependentTypesError(expression, NOT_EFFECTIVELY_FINAL).toString();
                }
                return result.toString();
            } catch (FlowExpressionParseUtil.FlowExpressionParseException e) {
                return new DependentTypesError(expression, e).toString();
            }
        }
    };
}
Also used : FlowExpressionParseUtil(org.checkerframework.framework.util.FlowExpressionParseUtil) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) DependentTypesError(org.checkerframework.framework.util.dependenttypes.DependentTypesError) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) TreePath(com.sun.source.util.TreePath) DependentTypesHelper(org.checkerframework.framework.util.dependenttypes.DependentTypesHelper) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with DependentTypesError

use of org.checkerframework.framework.util.dependenttypes.DependentTypesError in project checker-framework by typetools.

the class LockVisitor method parseExpressionString.

private LockExpression parseExpressionString(String expression, FlowExpressionContext flowExprContext, TreePath path, Receiver itself) {
    LockExpression lockExpression = new LockExpression(expression);
    if (DependentTypesError.isExpressionError(expression)) {
        lockExpression.error = new DependentTypesError(expression);
        return lockExpression;
    }
    Matcher selfReceiverMatcher = selfReceiverPattern.matcher(expression);
    try {
        if (selfReceiverMatcher.matches()) {
            String remainingExpression = selfReceiverMatcher.group(2);
            if (remainingExpression == null || remainingExpression.isEmpty()) {
                lockExpression.lockExpression = itself;
                if (!atypeFactory.isExpressionEffectivelyFinal(lockExpression.lockExpression)) {
                    checker.report(Result.failure("lock.expression.not.final", lockExpression.lockExpression), path.getLeaf());
                }
                return lockExpression;
            } else {
                // TODO: The proper way to do this is to call
                // flowExprContext.copyChangeToParsingMemberOfReceiver to set the receiver to
                // the <self> expression, and then call FlowExpressionParseUtil.parse on the
                // remaining expression string with the new flow expression context. However,
                // this currently results in a FlowExpressions.Receiver that has a different
                // hash code than if the following flow expression is parsed directly, which
                // results in our inability to check that a lock expression is held as it does
                // not match anything in the store due to the hash code mismatch.  For now,
                // convert the "<self>" portion to the node's string representation, and parse
                // the entire string:
                lockExpression.lockExpression = FlowExpressionParseUtil.parse(itself.toString() + "." + remainingExpression, flowExprContext, path, true);
                if (!atypeFactory.isExpressionEffectivelyFinal(lockExpression.lockExpression)) {
                    checker.report(Result.failure("lock.expression.not.final", lockExpression.lockExpression), path.getLeaf());
                }
                return lockExpression;
            }
        } else {
            lockExpression.lockExpression = FlowExpressionParseUtil.parse(expression, flowExprContext, path, true);
            return lockExpression;
        }
    } catch (FlowExpressionParseException ex) {
        lockExpression.error = new DependentTypesError(expression, ex);
        return lockExpression;
    }
}
Also used : Matcher(java.util.regex.Matcher) DependentTypesError(org.checkerframework.framework.util.dependenttypes.DependentTypesError) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)

Aggregations

DependentTypesError (org.checkerframework.framework.util.dependenttypes.DependentTypesError)2 ExpressionTree (com.sun.source.tree.ExpressionTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 Tree (com.sun.source.tree.Tree)1 VariableTree (com.sun.source.tree.VariableTree)1 TreePath (com.sun.source.util.TreePath)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)1 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)1 FlowExpressionParseUtil (org.checkerframework.framework.util.FlowExpressionParseUtil)1 FlowExpressionContext (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext)1 FlowExpressionParseException (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)1 DependentTypesHelper (org.checkerframework.framework.util.dependenttypes.DependentTypesHelper)1