use of org.checkerframework.common.wholeprograminference.AnnotationConverter in project checker-framework by typetools.
the class ASceneWrapper method writeToFile.
/**
* Write the scene wrapped by this object to a file at the given path.
*
* @param jaifPath the path of the file to be written, but ending in ".jaif". If {@code
* outputformat} is not {@code JAIF}, the path will be modified to match.
* @param annosToIgnore which annotations should be ignored in which contexts
* @param outputFormat the output format to use
* @param checker the checker from which this method is called, for naming stub files
*/
public void writeToFile(String jaifPath, AnnotationsInContexts annosToIgnore, OutputFormat outputFormat, BaseTypeChecker checker) {
assert jaifPath.endsWith(".jaif");
AScene scene = theScene.clone();
removeAnnosFromScene(scene, annosToIgnore);
scene.prune();
String filepath;
switch(outputFormat) {
case JAIF:
filepath = jaifPath;
break;
case STUB:
String astubWithChecker = "-" + checker.getClass().getCanonicalName() + ".astub";
filepath = jaifPath.replace(".jaif", astubWithChecker);
break;
default:
throw new BugInCF("Unhandled outputFormat " + outputFormat);
}
new File(filepath).delete();
// Only write non-empty scenes into files.
if (!scene.isEmpty()) {
try {
switch(outputFormat) {
case STUB:
// For stub files, pass in the checker to compute contracts on the fly; precomputing
// yields incorrect annotations, most likely due to nested classes.
SceneToStubWriter.write(this, filepath, checker);
break;
case JAIF:
// nothing about (and cannot depend on) the Checker Framework.
for (Map.Entry<String, AClass> classEntry : scene.classes.entrySet()) {
AClass aClass = classEntry.getValue();
for (Map.Entry<String, AMethod> methodEntry : aClass.getMethods().entrySet()) {
AMethod aMethod = methodEntry.getValue();
List<AnnotationMirror> contractAnnotationMirrors = checker.getTypeFactory().getContractAnnotations(aMethod);
List<Annotation> contractAnnotations = CollectionsPlume.mapList(AnnotationConverter::annotationMirrorToAnnotation, contractAnnotationMirrors);
aMethod.contracts = contractAnnotations;
}
}
IndexFileWriter.write(scene, new FileWriter(filepath));
break;
default:
throw new BugInCF("Unhandled outputFormat " + outputFormat);
}
} catch (IOException e) {
throw new UserError("Problem while writing %s: %s", filepath, e.getMessage());
} catch (DefException e) {
throw new BugInCF(e);
}
}
}
Aggregations