Search in sources :

Example 1 with InsertAjavaAnnotations

use of org.checkerframework.framework.ajava.InsertAjavaAnnotations in project checker-framework by typetools.

the class BaseTypeVisitor method testAnnotationInsertion.

/**
 * Tests {@link org.checkerframework.framework.ajava.InsertAjavaAnnotations} if the checker has
 * the "ajavaChecks" option.
 *
 * <ol>
 *   <li>Parses the current file with JavaParser.
 *   <li>Removes all annotations.
 *   <li>Reinserts the annotations.
 *   <li>Throws an exception if the ASTs are not the same.
 * </ol>
 *
 * <p>Subclasses may override this method to disable the test even if the option is provided.
 */
protected void testAnnotationInsertion() {
    if (root == null || !checker.hasOption("ajavaChecks")) {
        return;
    }
    CompilationUnit originalAst;
    try (InputStream originalInputStream = root.getSourceFile().openInputStream()) {
        originalAst = JavaParserUtil.parseCompilationUnit(originalInputStream);
    } catch (IOException e) {
        throw new BugInCF("Error while reading Java file: " + root.getSourceFile().toUri(), e);
    }
    CompilationUnit astWithoutAnnotations = originalAst.clone();
    JavaParserUtil.clearAnnotations(astWithoutAnnotations);
    String withoutAnnotations = new DefaultPrettyPrinter().print(astWithoutAnnotations);
    String withAnnotations;
    try (InputStream annotationInputStream = root.getSourceFile().openInputStream()) {
        // This check only runs on files from the Checker Framework test suite, which should all use
        // UNIX line separators. Using System.lineSeparator instead of "\n" could cause the test to
        // fail on Mac or Windows.
        withAnnotations = new InsertAjavaAnnotations(elements).insertAnnotations(annotationInputStream, withoutAnnotations, "\n");
    } catch (IOException e) {
        throw new BugInCF("Error while reading Java file: " + root.getSourceFile().toUri(), e);
    }
    CompilationUnit modifiedAst = null;
    try {
        modifiedAst = JavaParserUtil.parseCompilationUnit(withAnnotations);
    } catch (ParseProblemException e) {
        throw new BugInCF("Failed to parse annotation insertion:\n" + withAnnotations, e);
    }
    AnnotationEqualityVisitor visitor = new AnnotationEqualityVisitor();
    originalAst.accept(visitor, modifiedAst);
    if (!visitor.getAnnotationsMatch()) {
        throw new BugInCF(String.join(System.lineSeparator(), "Sanity check of erasing then reinserting annotations produced a different AST.", "File: " + root.getSourceFile(), "Original node: " + visitor.getMismatchedNode1(), "Node with annotations re-inserted: " + visitor.getMismatchedNode2(), "Original annotations: " + visitor.getMismatchedNode1().getAnnotations(), "Re-inserted annotations: " + visitor.getMismatchedNode2().getAnnotations(), "Original AST:", originalAst.toString(), "Ast with annotations re-inserted: " + modifiedAst));
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) DefaultPrettyPrinter(com.github.javaparser.printer.DefaultPrettyPrinter) InsertAjavaAnnotations(org.checkerframework.framework.ajava.InsertAjavaAnnotations) InputStream(java.io.InputStream) AnnotationEqualityVisitor(org.checkerframework.framework.ajava.AnnotationEqualityVisitor) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) ParseProblemException(com.github.javaparser.ParseProblemException)

Aggregations

ParseProblemException (com.github.javaparser.ParseProblemException)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 DefaultPrettyPrinter (com.github.javaparser.printer.DefaultPrettyPrinter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AnnotationEqualityVisitor (org.checkerframework.framework.ajava.AnnotationEqualityVisitor)1 InsertAjavaAnnotations (org.checkerframework.framework.ajava.InsertAjavaAnnotations)1 BugInCF (org.checkerframework.javacutil.BugInCF)1