use of scenelib.annotations.el.AScene in project checker-framework by typetools.
the class WholeProgramInferenceScenesHelper method writeScenesToJaif.
/**
* Write all modified scenes into .jaif files. (Scenes are modified by the method {@link
* #updateAnnotationSetInScene}.)
*/
public void writeScenesToJaif() {
// Create .jaif files directory if it doesn't exist already.
File jaifDir = new File(jaifFilesPath);
if (!jaifDir.exists()) {
jaifDir.mkdirs();
}
// Write scenes into .jaif files.
for (String jaifPath : modifiedScenes) {
try {
AScene scene = scenes.get(jaifPath).clone();
removeIgnoredAnnosFromScene(scene);
new File(jaifPath).delete();
if (!scene.prune()) {
// Only write non-empty scenes into .jaif files.
IndexFileWriter.write(scene, new FileWriter(jaifPath));
}
} catch (IOException e) {
ErrorReporter.errorAbort("Problem while reading file in: " + jaifPath + ". Exception message: " + e.getMessage(), e);
} catch (DefException e) {
ErrorReporter.errorAbort(e.getMessage(), e);
}
}
modifiedScenes.clear();
}
use of scenelib.annotations.el.AScene 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);
}
}
}
use of scenelib.annotations.el.AScene in project checker-framework by typetools.
the class WholeProgramInferenceScenesHelper method getScene.
/**
* Returns the Scene stored in a .jaif file path passed as input. If the file does not exist, an
* empty Scene is created.
*/
protected AScene getScene(String jaifPath) {
AScene scene;
if (!scenes.containsKey(jaifPath)) {
File jaifFile = new File(jaifPath);
scene = new AScene();
if (jaifFile.exists()) {
try {
IndexFileParser.parseFile(jaifPath, scene);
} catch (IOException e) {
ErrorReporter.errorAbort("Problem while reading file in: " + jaifPath + "." + " Exception message: " + e.getMessage(), e);
}
}
scenes.put(jaifPath, scene);
} else {
scene = scenes.get(jaifPath);
}
return scene;
}
use of scenelib.annotations.el.AScene in project checker-framework by typetools.
the class AddAnnotatedFor method main.
/**
* Reads JAIF from the file indicated by the first element, or standard input if the argument
* array is empty; inserts any appropriate {@code @AnnotatedFor} annotations, based on the
* annotations defined in the input JAIF; and writes the augmented JAIF to standard output.
*/
public static void main(String[] args) throws IOException, DefException, ParseException {
AScene scene = new AScene();
String filename;
Reader r;
if (args.length > 0) {
filename = args[0];
r = new FileReader(filename);
} else {
filename = "System.in";
r = new InputStreamReader(System.in);
}
IndexFileParser.parse(new LineNumberReader(r), filename, scene);
scene.prune();
addAnnotatedFor(scene);
IndexFileWriter.write(scene, new PrintWriter(System.out, true));
}
use of scenelib.annotations.el.AScene in project checker-framework by typetools.
the class ToIndexFileConverter method main.
/**
* Parse stub files and write out equivalent JAIFs. Note that the results do not include
* annotation definitions, for which stubfiles do not generally provide complete information.
*
* @param args name of JAIF with annotation definition, followed by names of stub files to be
* converted (if none given, program reads from standard input)
*/
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("usage: java ToIndexFileConverter myfile.jaif [stubfile...]");
System.err.println("(myfile.jaif contains needed annotation definitions)");
System.exit(1);
}
AScene scene = new AScene();
try {
// args[0] is a jaif file with needed annotation definitions
IndexFileParser.parseFile(args[0], scene);
if (args.length == 1) {
convert(scene, System.in, System.out);
return;
}
for (int i = 1; i < args.length; i++) {
String f0 = args[i];
String f1 = (f0.endsWith(".astub") ? f0.substring(0, f0.length() - 6) : f0) + ".jaif";
try (InputStream in = new FileInputStream(f0);
OutputStream out = new FileOutputStream(f1)) {
convert(new AScene(scene), in, out);
}
}
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
Aggregations