use of org.checkerframework.framework.util.ContractsUtils.Precondition in project checker-framework by typetools.
the class CFAbstractTransfer method addInformationFromPreconditions.
/**
* Add the information from all the preconditions of the method {@code method} with
* corresponding tree {@code methodTree} to the store {@code info}.
*/
protected void addInformationFromPreconditions(S info, AnnotatedTypeFactory factory, CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
ContractsUtils contracts = ContractsUtils.getInstance(analysis.atypeFactory);
FlowExpressionContext flowExprContext = null;
Set<Precondition> preconditions = contracts.getPreconditions(methodElement);
for (Precondition p : preconditions) {
String expression = p.expression;
AnnotationMirror annotation = p.annotation;
if (flowExprContext == null) {
flowExprContext = FlowExpressionContext.buildContextForMethodDeclaration(methodTree, method.getClassTree(), analysis.checker.getContext());
}
TreePath localScope = analysis.atypeFactory.getPath(methodTree);
annotation = standardizeAnnotationFromContract(annotation, flowExprContext, localScope);
try {
// TODO: currently, these expressions are parsed at the
// declaration (i.e. here) and for every use. this could
// be optimized to store the result the first time.
// (same for other annotations)
FlowExpressions.Receiver expr = FlowExpressionParseUtil.parse(expression, flowExprContext, localScope, false);
info.insertValue(expr, annotation);
} catch (FlowExpressionParseException e) {
// Errors are reported by BaseTypeVisitor.checkContractsAtMethodDeclaration()
}
}
}
use of org.checkerframework.framework.util.ContractsUtils.Precondition in project checker-framework by typetools.
the class BaseTypeVisitor method checkPreconditions.
/**
* Checks that all the given {@code preconditions} hold true immediately prior to the method
* invocation or variable access at {@code tree}.
*
* @param tree the Tree immediately prior to which the preconditions must hold true
* @param preconditions the preconditions to be checked
*/
protected void checkPreconditions(MethodInvocationTree tree, Set<Precondition> preconditions) {
// TODO: Remove this check and investigate the root cause.
if (preconditions.isEmpty()) {
return;
}
FlowExpressionContext flowExprContext = FlowExpressionContext.buildContextForMethodUse(tree, checker.getContext());
if (flowExprContext == null) {
checker.report(Result.failure("flowexpr.parse.context.not.determined", tree), tree);
return;
}
for (Precondition p : preconditions) {
String expression = p.expression;
AnnotationMirror anno = p.annotation;
anno = standardizeAnnotationFromContract(anno, flowExprContext, getCurrentPath());
try {
FlowExpressions.Receiver expr = FlowExpressionParseUtil.parse(expression, flowExprContext, getCurrentPath(), false);
CFAbstractStore<?, ?> store = atypeFactory.getStoreBefore(tree);
CFAbstractValue<?> value = store.getValue(expr);
AnnotationMirror inferredAnno = null;
if (value != null) {
QualifierHierarchy hierarchy = atypeFactory.getQualifierHierarchy();
Set<AnnotationMirror> annos = value.getAnnotations();
inferredAnno = hierarchy.findAnnotationInSameHierarchy(annos, anno);
}
if (!checkContract(expr, anno, inferredAnno, store)) {
checker.report(Result.failure("contracts.precondition.not.satisfied", tree.toString(), expr == null ? expression : expr.toString()), tree);
}
} catch (FlowExpressionParseException e) {
// report errors here
checker.report(e.getResult(), tree);
}
}
}
Aggregations