Skip to content

Commit e71d4b5

Browse files
Wohopsquentin-jaquier-sonarsource
authored andcommitted
SONARJAVA-3703 Drop 'sysout' calls in favor of loggers and move PrinterVisitor
1 parent d4282e1 commit e71d4b5

5 files changed

Lines changed: 64 additions & 14 deletions

File tree

docs/java-custom-rules-example/src/main/java/org/sonar/samples/java/checks/AvoidAnnotationRule.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ public class AvoidAnnotationRule extends BaseTreeVisitor implements JavaFileScan
5050
@Override
5151
public void scanFile(JavaFileScannerContext context) {
5252
this.context = context;
53-
5453
scan(context.getTree());
55-
56-
System.out.println(PrinterVisitor.print(context.getTree()));
5754
}
5855

5956
@Override
@@ -63,8 +60,6 @@ public void visitMethod(MethodTree tree) {
6360
TypeTree annotationType = annotationTree.annotationType();
6461
if (annotationType.is(Tree.Kind.IDENTIFIER)) {
6562
IdentifierTree identifier = (IdentifierTree) annotationType;
66-
System.out.println(identifier.name());
67-
6863
if (identifier.name().equals(name)) {
6964
context.reportIssue(this, identifier, String.format("Avoid using annotation @%s", name));
7065
}

docs/java-custom-rules-example/src/main/java/org/sonar/samples/java/checks/AvoidBrandInMethodNamesRule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
package org.sonar.samples.java.checks;
2121

2222
import java.util.Locale;
23+
import org.sonar.api.utils.log.Logger;
24+
import org.sonar.api.utils.log.Loggers;
2325
import org.sonar.check.Rule;
2426
import org.sonar.plugins.java.api.JavaFileScanner;
2527
import org.sonar.plugins.java.api.JavaFileScannerContext;
2628
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
2729
import org.sonar.plugins.java.api.tree.MethodTree;
30+
import org.sonar.samples.java.utils.PrinterVisitor;
2831

2932
@Rule(key = "AvoidBrandInMethodNames")
3033
public class AvoidBrandInMethodNamesRule extends BaseTreeVisitor implements JavaFileScanner {
3134

35+
private static final Logger LOGGER = Loggers.get(AvoidBrandInMethodNamesRule.class);
36+
3237
private JavaFileScannerContext context;
3338

3439
protected static final String COMPANY_NAME = "MyCompany";
@@ -41,7 +46,7 @@ public void scanFile(JavaFileScannerContext context) {
4146
scan(context.getTree());
4247

4348
// For debugging purpose, you can print out the entire AST of the analyzed file
44-
System.out.println(PrinterVisitor.print(context.getTree()));
49+
PrinterVisitor.print(context.getTree(), LOGGER::debug);
4550
}
4651

4752
/**

docs/java-custom-rules-example/src/main/java/org/sonar/samples/java/checks/AvoidMethodDeclarationRule.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ public void scanFile(JavaFileScannerContext context) {
5252

5353
// The call to the scan method on the root of the tree triggers the visit of the AST by this visitor
5454
scan(context.getTree());
55-
56-
// For debugging purpose, you can print out the entire AST of the analyzed file
57-
System.out.println(PrinterVisitor.print(context.getTree()));
5855
}
5956

6057
/**

docs/java-custom-rules-example/src/main/java/org/sonar/samples/java/checks/PrinterVisitor.java renamed to docs/java-custom-rules-example/src/main/java/org/sonar/samples/java/utils/PrinterVisitor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
* along with this program; if not, write to the Free Software Foundation,
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20-
package org.sonar.samples.java.checks;
20+
package org.sonar.samples.java.utils;
2121

2222
import java.util.List;
23+
import java.util.function.Consumer;
2324
import javax.annotation.Nullable;
2425
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
2526
import org.sonar.plugins.java.api.tree.Tree;
26-
import org.sonar.samples.java.utils.StringUtils;
2727

2828
public class PrinterVisitor extends BaseTreeVisitor {
29-
3029
private static final int INDENT_SPACES = 2;
3130

3231
private final StringBuilder sb;
@@ -37,10 +36,10 @@ public PrinterVisitor() {
3736
indentLevel = 0;
3837
}
3938

40-
public static String print(Tree tree) {
39+
public static void print(Tree tree, Consumer<String> output) {
4140
PrinterVisitor pv = new PrinterVisitor();
4241
pv.scan(tree);
43-
return pv.sb.toString();
42+
output.accept(pv.sb.toString());
4443
}
4544

4645
private StringBuilder indent() {

docs/java-custom-rules-example/src/test/java/org/sonar/samples/java/checks/AvoidBrandInMethodNamesRuleTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@
2020
package org.sonar.samples.java.checks;
2121

2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.RegisterExtension;
24+
import org.sonar.api.utils.log.LogTesterJUnit5;
25+
import org.sonar.api.utils.log.LoggerLevel;
2326
import org.sonar.java.checks.verifier.JavaCheckVerifier;
2427

28+
import static org.assertj.core.api.Assertions.assertThat;
29+
2530
class AvoidBrandInMethodNamesRuleTest {
2631

32+
// Register the logTester, to see the Syntax Tree when running tests and executing the rule
33+
@RegisterExtension
34+
LogTesterJUnit5 logTester = new LogTesterJUnit5();
35+
2736
@Test
2837
void detected() {
2938
// Verifies that the check will raise the adequate issues with the expected message.
@@ -34,4 +43,49 @@ void detected() {
3443
.withCheck(new AvoidBrandInMethodNamesRule())
3544
.verifyIssues();
3645
}
46+
47+
@Test
48+
void printingTreeInDebug() {
49+
// Make sure logs are visible during the tests, and at DEBUG level in production
50+
logTester.setLevel(LoggerLevel.DEBUG);
51+
52+
// re-execute the test
53+
JavaCheckVerifier.newVerifier()
54+
.onFile("src/test/files/AvoidBrandInMethodNamesRule.java")
55+
.withCheck(new AvoidBrandInMethodNamesRule())
56+
.verifyIssues();
57+
58+
// verify that the tree has been correctly displayed in DEBUG
59+
assertThat(logTester.logs(LoggerLevel.DEBUG))
60+
.hasSize(1)
61+
.containsOnly("CompilationUnitTree : [\n"
62+
+ " ClassTree\n"
63+
+ " ModifiersTree\n"
64+
+ " IdentifierTree\n"
65+
+ " TypeParameters : [\n"
66+
+ " VariableTree\n"
67+
+ " ModifiersTree\n"
68+
+ " PrimitiveTypeTree\n"
69+
+ " IdentifierTree\n"
70+
+ " MethodTree\n"
71+
+ " ModifiersTree\n"
72+
+ " TypeParameters\n"
73+
+ " PrimitiveTypeTree\n"
74+
+ " IdentifierTree\n"
75+
+ " BlockTree\n"
76+
+ " MethodTree\n"
77+
+ " ModifiersTree\n"
78+
+ " TypeParameters\n"
79+
+ " PrimitiveTypeTree\n"
80+
+ " IdentifierTree\n"
81+
+ " BlockTree\n"
82+
+ " MethodTree\n"
83+
+ " ModifiersTree\n"
84+
+ " TypeParameters\n"
85+
+ " PrimitiveTypeTree\n"
86+
+ " IdentifierTree\n"
87+
+ " BlockTree\n"
88+
+ " ]\n"
89+
+ " ]\n");
90+
}
3791
}

0 commit comments

Comments
 (0)