Search in sources :

Example 1 with ExpectedTreesVisitor

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);
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) HashMap(java.util.HashMap) InputStream(java.io.InputStream) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) Node(org.checkerframework.dataflow.cfg.node.Node) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) ExpectedTreesVisitor(org.checkerframework.framework.ajava.ExpectedTreesVisitor) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ThrowTree(com.sun.source.tree.ThrowTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) JCTree(com.sun.tools.javac.tree.JCTree) JointVisitorWithDefaultAction(org.checkerframework.framework.ajava.JointVisitorWithDefaultAction)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)1 AnnotationTree (com.sun.source.tree.AnnotationTree)1 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 CatchTree (com.sun.source.tree.CatchTree)1 ClassTree (com.sun.source.tree.ClassTree)1 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1 InstanceOfTree (com.sun.source.tree.InstanceOfTree)1 IntersectionTypeTree (com.sun.source.tree.IntersectionTypeTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)1 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 MethodTree (com.sun.source.tree.MethodTree)1