Search in sources :

Example 1 with AScene

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();
}
Also used : AScene(scenelib.annotations.el.AScene) DefException(scenelib.annotations.el.DefException) IndexFileWriter(scenelib.annotations.io.IndexFileWriter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Example 2 with AScene

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);
        }
    }
}
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)

Example 3 with AScene

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;
}
Also used : AScene(scenelib.annotations.el.AScene) IOException(java.io.IOException) File(java.io.File)

Example 4 with AScene

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));
}
Also used : AScene(scenelib.annotations.el.AScene) InputStreamReader(java.io.InputStreamReader) LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) LineNumberReader(java.io.LineNumberReader) PrintWriter(java.io.PrintWriter)

Example 5 with AScene

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);
    }
}
Also used : AScene(scenelib.annotations.el.AScene) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Aggregations

AScene (scenelib.annotations.el.AScene)6 File (java.io.File)4 IOException (java.io.IOException)4 FileWriter (java.io.FileWriter)2 UserError (org.checkerframework.javacutil.UserError)2 DefException (scenelib.annotations.el.DefException)2 IndexFileWriter (scenelib.annotations.io.IndexFileWriter)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Map (java.util.Map)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationConverter (org.checkerframework.common.wholeprograminference.AnnotationConverter)1 ASceneWrapper (org.checkerframework.common.wholeprograminference.scenelib.ASceneWrapper)1