use of org.checkerframework.framework.source.DiagMessage in project checker-framework by typetools.
the class BaseTypeValidator method isTopLevelValidType.
/**
* Checks every property listed in {@link #isValidStructurally}, but only for the top level type.
* If successful, returns an empty list. If not successful, returns diagnostics.
*
* @param qualifierHierarchy the QualifierHierarchy
* @param type the type to be checked
* @return the diagnostics indicating failure, or an empty list if successful
*/
// This method returns a singleton or empyty list. Its return type is List rather than
// DiagMessage (with null indicting success) because its caller, isValidStructurally(), expects
// a list.
protected List<DiagMessage> isTopLevelValidType(QualifierHierarchy qualifierHierarchy, AnnotatedTypeMirror type) {
// multiple annotations from the same hierarchy
Set<AnnotationMirror> annotations = type.getAnnotations();
Set<AnnotationMirror> seenTops = AnnotationUtils.createAnnotationSet();
for (AnnotationMirror anno : annotations) {
AnnotationMirror top = qualifierHierarchy.getTopAnnotation(anno);
if (AnnotationUtils.containsSame(seenTops, top)) {
return Collections.singletonList(new DiagMessage(Kind.ERROR, "conflicting.annos", annotations, type));
}
seenTops.add(top);
}
boolean canHaveEmptyAnnotationSet = QualifierHierarchy.canHaveEmptyAnnotationSet(type);
// wrong number of annotations
if (!canHaveEmptyAnnotationSet && seenTops.size() < qualifierHierarchy.getWidth()) {
return Collections.singletonList(new DiagMessage(Kind.ERROR, "too.few.annotations", annotations, type));
}
// success
return Collections.emptyList();
}
use of org.checkerframework.framework.source.DiagMessage in project checker-framework by typetools.
the class BaseTypeVisitor method checkContractsAtMethodDeclaration.
/**
* Check the contracts written on a method declaration. Ensures that the postconditions hold on
* exit, and that the contracts are well-formed.
*
* @param methodTree the method declaration
* @param methodElement the method element
* @param formalParamNames the formal parameter names
* @param abstractMethod whether the method is abstract
*/
private void checkContractsAtMethodDeclaration(MethodTree methodTree, ExecutableElement methodElement, List<String> formalParamNames, boolean abstractMethod) {
Set<Contract> contracts = atypeFactory.getContractsFromMethod().getContracts(methodElement);
if (contracts.isEmpty()) {
return;
}
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodBody(stringExpr, methodTree, checker);
for (Contract contract : contracts) {
String expressionString = contract.expressionString;
AnnotationMirror annotation = contract.viewpointAdaptDependentTypeAnnotation(atypeFactory, stringToJavaExpr, methodTree);
JavaExpression exprJe;
try {
exprJe = StringToJavaExpression.atMethodBody(expressionString, methodTree, checker);
} catch (JavaExpressionParseException e) {
DiagMessage diagMessage = e.getDiagMessage();
if (diagMessage.getMessageKey().equals("flowexpr.parse.error")) {
String s = String.format("'%s' in the %s %s on the declaration of method '%s': ", expressionString, contract.kind.errorKey, contract.contractAnnotation.getAnnotationType().asElement().getSimpleName(), methodTree.getName().toString());
checker.reportError(methodTree, "flowexpr.parse.error", s + diagMessage.getArgs()[0]);
} else {
checker.report(methodTree, e.getDiagMessage());
}
continue;
}
if (!CFAbstractStore.canInsertJavaExpression(exprJe)) {
checker.reportError(methodTree, "flowexpr.parse.error", expressionString);
continue;
}
if (!abstractMethod && contract.kind != Contract.Kind.PRECONDITION) {
switch(contract.kind) {
case POSTCONDITION:
checkPostcondition(methodTree, annotation, exprJe);
break;
case CONDITIONALPOSTCONDITION:
checkConditionalPostcondition(methodTree, annotation, exprJe, ((ConditionalPostcondition) contract).resultValue);
break;
default:
throw new BugInCF("Impossible: " + contract.kind);
}
}
if (formalParamNames != null && formalParamNames.contains(expressionString)) {
String locationOfExpression = contract.kind.errorKey + " " + contract.contractAnnotation.getAnnotationType().asElement().getSimpleName() + " on the declaration";
checker.reportWarning(methodTree, "expression.parameter.name.shadows.field", locationOfExpression, methodTree.getName().toString(), expressionString, expressionString, formalParamNames.indexOf(expressionString) + 1);
}
checkParametersAreEffectivelyFinal(methodTree, exprJe);
}
}
Aggregations