Search in sources :

Example 1 with AnnotationConverter

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);
        }
    }
}
Also used : AScene(scenelib.annotations.el.AScene) AnnotationConverter(org.checkerframework.common.wholeprograminference.AnnotationConverter) UserError(org.checkerframework.javacutil.UserError) DefException(scenelib.annotations.el.DefException) IndexFileWriter(scenelib.annotations.io.IndexFileWriter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) Annotation(scenelib.annotations.Annotation) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AClass(scenelib.annotations.el.AClass) File(java.io.File) Map(java.util.Map) AMethod(scenelib.annotations.el.AMethod)

Aggregations

File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Map (java.util.Map)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationConverter (org.checkerframework.common.wholeprograminference.AnnotationConverter)1 BugInCF (org.checkerframework.javacutil.BugInCF)1 UserError (org.checkerframework.javacutil.UserError)1 Annotation (scenelib.annotations.Annotation)1 AClass (scenelib.annotations.el.AClass)1 AMethod (scenelib.annotations.el.AMethod)1 AScene (scenelib.annotations.el.AScene)1 DefException (scenelib.annotations.el.DefException)1 IndexFileWriter (scenelib.annotations.io.IndexFileWriter)1