Search in sources :

Example 1 with DefException

use of scenelib.annotations.el.DefException 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 DefException

use of scenelib.annotations.el.DefException 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 DefException

use of scenelib.annotations.el.DefException in project checker-framework by typetools.

the class SceneToStubWriter method writeImpl.

/**
 * The implementation of {@link #write}. Prints imports, classes, method signatures, and fields in
 * stub file format, all with appropriate annotations.
 *
 * @param scene the scene to write
 * @param filename the name of the file to write (must end in .astub)
 * @param checker the checker, for computing preconditions
 */
private static void writeImpl(ASceneWrapper scene, String filename, BaseTypeChecker checker) {
    // Sort by package name first so that output is deterministic and default package
    // comes first; within package sort by class name.
    // scene-lib bytecode lacks signature annotations
    @SuppressWarnings("signature") List<@BinaryName String> classes = new ArrayList<>(scene.getAScene().getClasses().keySet());
    Collections.sort(classes, new Comparator<@BinaryName String>() {

        @Override
        public int compare(@BinaryName String o1, @BinaryName String o2) {
            return ComparisonChain.start().compare(packagePart(o1), packagePart(o2), Comparator.nullsFirst(Comparator.naturalOrder())).compare(basenamePart(o1), basenamePart(o2)).result();
        }
    });
    boolean anyClassPrintable = false;
    // The writer is not initialized until it is certain that at
    // least one class can be written, to avoid empty stub files.
    PrintWriter printWriter = null;
    // For each class
    for (String clazz : classes) {
        if (isPrintable(clazz, scene.getAScene().getClasses().get(clazz))) {
            if (!anyClassPrintable) {
                try {
                    printWriter = new PrintWriter(new FileWriter(filename));
                } catch (IOException e) {
                    throw new BugInCF("error writing file during WPI: " + filename);
                }
                // Write out all imports
                ImportDefWriter importDefWriter;
                try {
                    importDefWriter = new ImportDefWriter(scene, printWriter);
                } catch (DefException e) {
                    throw new BugInCF(e);
                }
                importDefWriter.visit();
                printWriter.println("import org.checkerframework.framework.qual.AnnotatedFor;");
                printWriter.println();
                anyClassPrintable = true;
            }
            printClass(clazz, scene.getAScene().getClasses().get(clazz), checker, printWriter);
        }
    }
    if (printWriter != null) {
        printWriter.flush();
    }
}
Also used : DefException(scenelib.annotations.el.DefException) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) PrintWriter(java.io.PrintWriter)

Aggregations

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