Search in sources :

Example 1 with Comment

use of com.sun.tools.javac.parser.Tokens.Comment 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)

Aggregations

VisitorState (com.google.errorprone.VisitorState)1 CompilerBasedAbstractTest (com.google.errorprone.matchers.CompilerBasedAbstractTest)1 NewClassTree (com.sun.source.tree.NewClassTree)1 Comment (com.sun.tools.javac.parser.Tokens.Comment)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1