Search in sources :

Example 6 with NewClassTree

use of com.sun.source.tree.NewClassTree in project error-prone by google.

the class DefaultCharset method handlePrintWriterOutputStream.

private Description handlePrintWriterOutputStream(NewClassTree tree, VisitorState state) {
    Tree outputStream = tree.getArguments().get(0);
    Description.Builder description = buildDescription(tree);
    for (CharsetFix charsetFix : CharsetFix.values()) {
        SuggestedFix.Builder fix = SuggestedFix.builder().prefixWith(outputStream, "new BufferedWriter(new OutputStreamWriter(").postfixWith(outputStream, String.format(", %s))", charsetFix.replacement()));
        charsetFix.addImport(fix, state);
        fix.addImport("java.io.BufferedWriter");
        fix.addImport("java.io.OutputStreamWriter");
        description.addFix(fix.build());
    }
    return description.build();
}
Also used : Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 7 with NewClassTree

use of com.sun.source.tree.NewClassTree 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

NewClassTree (com.sun.source.tree.NewClassTree)7 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)6 Tree (com.sun.source.tree.Tree)6 VariableTree (com.sun.source.tree.VariableTree)6 AssignmentTree (com.sun.source.tree.AssignmentTree)5 ExpressionTree (com.sun.source.tree.ExpressionTree)5 JCTree (com.sun.tools.javac.tree.JCTree)5 Description (com.google.errorprone.matchers.Description)3 BlockTree (com.sun.source.tree.BlockTree)3 ClassTree (com.sun.source.tree.ClassTree)3 IdentifierTree (com.sun.source.tree.IdentifierTree)3 ImportTree (com.sun.source.tree.ImportTree)3 MemberSelectTree (com.sun.source.tree.MemberSelectTree)3 ArrayList (java.util.ArrayList)3 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)2 BinaryTree (com.sun.source.tree.BinaryTree)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)2 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)2 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)2