use of org.checkerframework.framework.ajava.AnnotationEqualityVisitor 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));
}
}
Aggregations