use of org.checkerframework.framework.ajava.ExpectedTreesVisitor in project checker-framework by typetools.
the class BaseTypeVisitor method testJointJavacJavaParserVisitor.
/**
* Test {@link org.checkerframework.framework.ajava.JointJavacJavaParserVisitor} if the checker
* has the "ajavaChecks" option.
*
* <p>Parse the current source file with JavaParser and check that the AST can be matched with the
* Tree prodoced by javac. Crash if not.
*
* <p>Subclasses may override this method to disable the test if even the option is provided.
*/
protected void testJointJavacJavaParserVisitor() {
if (root == null || !checker.hasOption("ajavaChecks")) {
return;
}
Map<Tree, com.github.javaparser.ast.Node> treePairs = new HashMap<>();
try (InputStream reader = root.getSourceFile().openInputStream()) {
CompilationUnit javaParserRoot = JavaParserUtil.parseCompilationUnit(reader);
JavaParserUtil.concatenateAddedStringLiterals(javaParserRoot);
new JointVisitorWithDefaultAction() {
@Override
public void defaultJointAction(Tree javacTree, com.github.javaparser.ast.Node javaParserNode) {
treePairs.put(javacTree, javaParserNode);
}
}.visitCompilationUnit(root, javaParserRoot);
ExpectedTreesVisitor expectedTreesVisitor = new ExpectedTreesVisitor();
expectedTreesVisitor.visitCompilationUnit(root, null);
for (Tree expected : expectedTreesVisitor.getTrees()) {
if (!treePairs.containsKey(expected)) {
throw new BugInCF("Javac tree not matched to JavaParser node: %s [%s @ %d], in file: %s", expected, expected.getClass(), positions.getStartPosition(root, expected), root.getSourceFile().getName());
}
}
} catch (IOException e) {
throw new BugInCF("Error reading Java source file", e);
}
}
Aggregations