Search in sources :

Example 26 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class ASTHelpersTest method testCommentTokens.

@Test
public void testCommentTokens() {
    writeFile("A.java", "public class A {", "  Runnable theRunnable = new Runnable() {", "    /**", "     * foo", "     */", "    public void run() {", "      /* bar1 */", "      /* bar2 */", "      System.err.println(\"Hi\");", "    }", "    // baz number 1", "    // baz number 2", "  };", "}");
    TestScanner scanner = new TestScanner() {

        @Override
        public Void visitNewClass(NewClassTree tree, VisitorState state) {
            setAssertionsComplete();
            List<String> comments = new ArrayList<>();
            for (ErrorProneToken t : state.getTokensForNode(tree)) {
                if (!t.comments().isEmpty()) {
                    for (Comment c : t.comments()) {
                        Verify.verify(c.getSourcePos(0) >= 0);
                        comments.add(c.getText());
                    }
                }
            }
            assertThat(comments).containsExactly("/**\n     * foo\n     */", "/* bar1 */", "/* bar2 */", "// baz number 1", "// baz number 2").inOrder();
            return super.visitNewClass(tree, state);
        }
    };
    tests.add(scanner);
    assertCompiles(scanner);
}
Also used : Comment(com.sun.tools.javac.parser.Tokens.Comment) VisitorState(com.google.errorprone.VisitorState) ArrayList(java.util.ArrayList) NewClassTree(com.sun.source.tree.NewClassTree) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest) Test(org.junit.Test)

Example 27 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class ASTHelpersTest method expressionStatementMatches.

private Scanner expressionStatementMatches(final String expectedExpression, final Matcher<ExpressionTree> matcher) {
    return new TestScanner() {

        @Override
        public Void visitExpressionStatement(ExpressionStatementTree node, VisitorState state) {
            ExpressionTree expression = node.getExpression();
            if (expression.toString().equals(expectedExpression)) {
                assertMatch(node.getExpression(), state, matcher);
                setAssertionsComplete();
            }
            return super.visitExpressionStatement(node, state);
        }
    };
}
Also used : VisitorState(com.google.errorprone.VisitorState) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 28 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class AnnotationMatcherTest method nodeWithAnnotationMatches.

private Scanner nodeWithAnnotationMatches(final boolean shouldMatch, final AnnotationMatcher<Tree> toMatch) {
    ScannerTest test = new ScannerTest() {

        private boolean matched = false;

        @Override
        public Void visitAnnotation(AnnotationTree node, VisitorState visitorState) {
            TreePath currPath = getCurrentPath().getParentPath();
            Tree parent = currPath.getLeaf();
            if (parent.getKind() == Kind.MODIFIERS) {
                currPath = currPath.getParentPath();
                parent = currPath.getLeaf();
            }
            visitorState = visitorState.withPath(currPath);
            if (toMatch.matches(parent, visitorState)) {
                matched = true;
            }
            visitorState = visitorState.withPath(getCurrentPath());
            return super.visitAnnotation(node, visitorState);
        }

        @Override
        public void assertDone() {
            assertEquals(matched, shouldMatch);
        }
    };
    tests.add(test);
    return test;
}
Also used : TreePath(com.sun.source.util.TreePath) VisitorState(com.google.errorprone.VisitorState) AnnotationTree(com.sun.source.tree.AnnotationTree) Tree(com.sun.source.tree.Tree) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 29 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class DescendantOfAbstractTest method memberSelectMatches.

protected Scanner memberSelectMatches(final boolean shouldMatch, final DescendantOf toMatch) {
    ScannerTest test = new ScannerTest() {

        private boolean matched = false;

        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, VisitorState visitorState) {
            visitorState = visitorState.withPath(getCurrentPath());
            ExpressionTree methodSelect = node.getMethodSelect();
            if (toMatch.matches(methodSelect, visitorState)) {
                matched = true;
            }
            return super.visitMethodInvocation(node, visitorState);
        }

        @Override
        public void assertDone() {
            assertEquals(matched, shouldMatch);
        }
    };
    tests.add(test);
    return test;
}
Also used : VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

VisitorState (com.google.errorprone.VisitorState)29 MethodTree (com.sun.source.tree.MethodTree)13 Tree (com.sun.source.tree.Tree)13 ExpressionTree (com.sun.source.tree.ExpressionTree)12 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 ClassTree (com.sun.source.tree.ClassTree)10 Symbol (com.sun.tools.javac.code.Symbol)9 Type (com.sun.tools.javac.code.Type)9 Description (com.google.errorprone.matchers.Description)8 IdentifierTree (com.sun.source.tree.IdentifierTree)8 VariableTree (com.sun.source.tree.VariableTree)8 JCTree (com.sun.tools.javac.tree.JCTree)8 List (java.util.List)8 BugPattern (com.google.errorprone.BugPattern)7 ASTHelpers (com.google.errorprone.util.ASTHelpers)7 TreePath (com.sun.source.util.TreePath)7 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)7 ArrayList (java.util.ArrayList)7 JDK (com.google.errorprone.BugPattern.Category.JDK)6 MemberSelectTree (com.sun.source.tree.MemberSelectTree)6