Skip to content

Commit 42ce6fd

Browse files
committed
Issue #41: add violation comment violation
1 parent 9279400 commit 42ce6fd

File tree

9 files changed

+104
-66
lines changed

9 files changed

+104
-66
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
</dependency>
6666

6767
<!-- Test dependencies -->
68+
<dependency>
69+
<groupId>com.google.truth</groupId>
70+
<artifactId>truth</artifactId>
71+
<version>1.4.4</version>
72+
<scope>test</scope>
73+
</dependency>
6874
<dependency>
6975
<groupId>com.puppycrawl.tools</groupId>
7076
<artifactId>checkstyle</artifactId>

src/test/java/org/checkstyle/autofix/RemoveViolationComments.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818
package org.checkstyle.autofix;
1919

20+
import java.util.ArrayList;
2021
import java.util.List;
21-
import java.util.Objects;
22-
import java.util.stream.Collectors;
2322

2423
import org.openrewrite.ExecutionContext;
2524
import org.openrewrite.Recipe;
@@ -50,20 +49,30 @@ private static final class ViolationCommentRemover extends JavaIsoVisitor<Execut
5049
@Override
5150
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
5251
final StringBuilder suffixAccumulator = new StringBuilder();
52+
final List<Comment> filteredComments = new ArrayList<>();
5353

54-
final List<Comment> filteredComments = space.getComments().stream()
55-
.map(comment -> {
56-
Comment result = comment;
57-
if (!comment.isMultiline() && comment instanceof TextComment) {
58-
final TextComment textComment = (TextComment) comment;
59-
if (textComment.getText().startsWith("violation")) {
60-
suffixAccumulator.append(textComment.getSuffix());
61-
result = null;
62-
}
54+
for (Comment comment : space.getComments()) {
55+
Comment result = comment;
56+
if (!comment.isMultiline() && comment instanceof TextComment) {
57+
final TextComment textComment = (TextComment) comment;
58+
if (textComment.getText().matches("\\s*(\\d+\\s*)?violation.*")) {
59+
if (!filteredComments.isEmpty()) {
60+
final int lastIndex = filteredComments.size() - 1;
61+
final Comment previousComment = filteredComments.get(lastIndex);
62+
filteredComments.set(lastIndex,
63+
previousComment.withSuffix(textComment.getSuffix()));
6364
}
64-
return result; })
65-
.filter(Objects::nonNull)
66-
.collect(Collectors.toList());
65+
else {
66+
suffixAccumulator.append(textComment.getSuffix());
67+
}
68+
result = null;
69+
}
70+
}
71+
72+
if (result != null) {
73+
filteredComments.add(result);
74+
}
75+
}
6776

6877
Space result;
6978

@@ -73,16 +82,7 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
7382
else {
7483
result = space.withComments(filteredComments);
7584
if (!suffixAccumulator.isEmpty()) {
76-
if (filteredComments.isEmpty()) {
77-
result = result.withWhitespace(suffixAccumulator.toString());
78-
}
79-
else {
80-
final Comment lastComment = filteredComments
81-
.get(filteredComments.size() - 1);
82-
filteredComments.set(filteredComments.size() - 1,
83-
lastComment.withSuffix(suffixAccumulator.toString()));
84-
result = space.withComments(filteredComments);
85-
}
85+
result = result.withWhitespace(suffixAccumulator.toString());
8686
}
8787
}
8888
return super.visitSpace(result, loc, ctx);

src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ protected String getPackageLocation() {
5757
}
5858

5959
protected void verify(String testCaseName) throws Exception {
60-
verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName);
60+
61+
final String inputPath = getInputFilePath(testCaseName);
62+
final String outputPath = getOutputFilePath(testCaseName);
63+
final Configuration config = getCheckConfigurations(inputPath);
64+
verifyOutputFile(outputPath, config);
65+
66+
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
67+
68+
verifyWithInlineConfigParser(getPath(inputPath), convertToExpectedMessages(violations));
69+
70+
final String beforeCode = readFile(getPath(inputPath));
71+
final String expectedAfterCode = readFile(getPath(outputPath));
72+
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
73+
final Recipe mainRecipe = createRecipe(violations, checkConfig);
74+
testRecipe(beforeCode, expectedAfterCode,
75+
getPath(inputPath), new InputClassRenamer(),
76+
mainRecipe, new RemoveViolationComments());
6177
}
6278

6379
protected void verify(Configuration config, String testCaseName) throws Exception {
@@ -74,8 +90,7 @@ protected void verify(Configuration config, String testCaseName) throws Exceptio
7490
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
7591
final Recipe mainRecipe = createRecipe(violations, checkConfig);
7692
testRecipe(beforeCode, expectedAfterCode,
77-
getPath(inputPath), new InputClassRenamer(),
78-
new RemoveViolationComments(), mainRecipe);
93+
getPath(inputPath), new InputClassRenamer(), mainRecipe);
7994
}
8095

8196
private List<CheckstyleViolation> runCheckstyle(String inputPath,
@@ -120,6 +135,21 @@ private void verifyOutputFile(String outputPath, Configuration config) throws Ex
120135
}
121136
}
122137

138+
private String[] convertToExpectedMessages(List<CheckstyleViolation> violations) {
139+
return violations.stream()
140+
.map(violation -> {
141+
final String message;
142+
if (violation.getColumn() > 0) {
143+
message = violation.getLine() + ":"
144+
+ violation.getColumn() + ": " + violation.getMessage();
145+
}
146+
else {
147+
message = violation.getLine() + ": " + violation.getMessage();
148+
}
149+
return message; })
150+
.toArray(String[]::new);
151+
}
152+
123153
private Configuration getCheckConfigurations(String inputPath) throws Exception {
124154
final String configFilePath = getPath(inputPath);
125155
final TestInputConfiguration testInputConfiguration =

src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,49 @@
1515
package org.checkstyle.autofix.recipe.upperell.complexlongliterals;
1616

1717
public class InputComplexLongLiterals {
18-
private long withUnderscores = 1_000_000l;
19-
private long multipleUnderscores = 1_234_567_890l;
18+
private long withUnderscores = 1_000_000l; // violation 'Should use uppercase 'L'.'
19+
private long multipleUnderscores = 1_234_567_890l; // violation 'Should use uppercase 'L'.'
2020

21-
private long maxLong = 9223372036854775807l; //suppressed violation
22-
private long minLong = -9223372036854775808l; //suppressed violation
21+
private long maxLong = 9223372036854775807l; // suppressed violation
22+
private long minLong = -9223372036854775808l; // suppressed violation
2323

2424
private Long nullLong = null;
25-
private Long simpleLong = 1234l;
26-
private Long negativeLong = -5678l;
27-
private Long underscoreLong = 1_000_000l;
28-
Long maxLongObject = 9223372036854775807l; //suppressed violation
29-
Long minLongObject = -9223372036854775808l; //suppressed violation
25+
private Long simpleLong = 1234l; // violation 'Should use uppercase 'L'.'
26+
private Long negativeLong = -5678l; // violation 'Should use uppercase 'L'.'
27+
private Long underscoreLong = 1_000_000l; // violation 'Should use uppercase 'L'.'
28+
Long maxLongObject = 9223372036854775807l; // suppressed violation
29+
Long minLongObject = -9223372036854775808l; // suppressed violation
3030

3131
public long calculate(long input1, long input2) {
32-
return input1 + input2 + 1000l;
32+
return input1 + input2 + 1000l; // violation 'Should use uppercase 'L'.'
3333
}
3434

3535
public void lambdaUsage() {
36+
// violation below, 'Should use uppercase 'L'.'
3637
java.util.function.LongSupplier supplier = () -> 42l;
38+
// 3 violations below
3739
java.util.Arrays.asList(1l, 2l, 3l).forEach(System.out::println);
3840
}
3941

4042
public java.util.List<Long> getNumbers() {
41-
return java.util.Arrays.asList(100l, 200l, 300l);
43+
return java.util.Arrays.asList(100l, 200l, 300l); // 3 violations
4244
}
4345

4446
public void longObjectOperations() {
4547
Long a = null;
46-
Long b = 1234l;
47-
Long c = Long.valueOf(5678l);
48-
Long d = new Long(9999l);
48+
Long b = 1234l; // violation 'Should use uppercase 'L'.'
49+
Long c = Long.valueOf(5678l); // violation 'Should use uppercase 'L'.'
50+
Long d = new Long(9999l); // violation 'Should use uppercase 'L'.'
4951

50-
Long conditional = (a != null) ? a : 0l;
51-
long primitive = b != null ? b : 0l;
52-
Long boxed = primitive + 1000l;
52+
Long conditional = (a != null) ? a : 0l; // violation 'Should use uppercase 'L'.'
53+
long primitive = b != null ? b : 0l; // violation 'Should use uppercase 'L'.'
54+
Long boxed = primitive + 1000l; // violation 'Should use uppercase 'L'.'
5355
}
5456

5557
public Long methodReturningLong(Long param) {
5658
if (param == null) {
57-
return 12345l;
59+
return 12345l; // violation 'Should use uppercase 'L'.'
5860
}
59-
return param + 6789l;
61+
return param + 6789l; // violation 'Should use uppercase 'L'.'
6062
}
6163
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public class OutputComplexLongLiterals {
1818
private long withUnderscores = 1_000_000L;
1919
private long multipleUnderscores = 1_234_567_890L;
2020

21-
private long maxLong = 9223372036854775807l; //suppressed violation
22-
private long minLong = -9223372036854775808l; //suppressed violation
21+
private long maxLong = 9223372036854775807l; // suppressed violation
22+
private long minLong = -9223372036854775808l; // suppressed violation
2323

2424
private Long nullLong = null;
2525
private Long simpleLong = 1234L;
2626
private Long negativeLong = -5678L;
2727
private Long underscoreLong = 1_000_000L;
28-
Long maxLongObject = 9223372036854775807l; //suppressed violation
29-
Long minLongObject = -9223372036854775808l; //suppressed violation
28+
Long maxLongObject = 9223372036854775807l; // suppressed violation
29+
Long minLongObject = -9223372036854775808l; // suppressed violation
3030

3131
public long calculate(long input1, long input2) {
3232
return input1 + input2 + 1000L;

src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;
1515

1616
public class InputHexOctalLiteral {
17-
private long hexLower = 0x1ABCl;
18-
private long hexUpper = 0X2DEFl;
19-
private long octal = 0777l;
20-
private long binary = 0b1010l;
21-
private long decimal = 12345l;
17+
private long hexLower = 0x1ABCl; // violation 'Should use uppercase 'L'.'
18+
private long hexUpper = 0X2DEFl; // violation 'Should use uppercase 'L'.'
19+
private long octal = 0777l; // violation 'Should use uppercase 'L'.'
20+
private long binary = 0b1010l; // violation 'Should use uppercase 'L'.'
21+
private long decimal = 12345l; // violation 'Should use uppercase 'L'.'
2222

2323
public void calculateValues() {
24+
// violation below, 'Should use uppercase 'L'.'
2425
long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl
26+
// violation below, 'Should use uppercase 'L'.'
2527
long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 01234l
26-
long binaryResult = 0b11110000l;
28+
long binaryResult = 0b11110000l; // violation 'Should use uppercase 'L'.'
2729
}
2830
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/InputStringAndComments.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ public class InputStringAndComments {
1616
private String message = "The value 456l should not change in strings";
1717
private String code = "long value = 789l;"; // This 789l in string should not change
1818

19-
// Only this actual long literal should change
20-
private long actualLong = 999l;
19+
private long actualLong = 999l; // violation 'Should use uppercase 'L'.'
2120

2221
/*
2322
* Multi-line comment with 111l should not change
2423
*/
2524
public void method() {
2625
// Single line comment with 222l should not change
27-
long value = 333l; // This should change
26+
long value = 333l; // violation 'Should use uppercase 'L'.'
2827
}
2928
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/OutputStringAndComments.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ public class OutputStringAndComments {
1616
private String message = "The value 456l should not change in strings";
1717
private String code = "long value = 789l;"; // This 789l in string should not change
1818

19-
// Only this actual long literal should change
2019
private long actualLong = 999L;
2120

2221
/*
2322
* Multi-line comment with 111l should not change
2423
*/
2524
public void method() {
2625
// Single line comment with 222l should not change
27-
long value = 333L; // This should change
26+
long value = 333L;
2827
}
2928
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/symbolicliterals/InputSymbolicLiterals.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
package org.checkstyle.autofix.recipe.upperell.symbolicliterals;
1111

1212
public class InputSymbolicLiterals {
13-
private long minLong = -9223372036854775808l;
14-
private Long negativeLong = -5678l;
15-
long a = -0xAl;
16-
long b = +-12l;
17-
long c = +-(-(-(-4l)));;
13+
private long minLong = -9223372036854775808l; // violation 'Should use uppercase 'L'.'
14+
private Long negativeLong = -5678l; // violation 'Should use uppercase 'L'.'
15+
long a = -0xAl; // violation 'Should use uppercase 'L'.'
16+
long b = +-12l; // violation 'Should use uppercase 'L'.'
17+
long c = +-(-(-(-4l)));; // violation 'Should use uppercase 'L'.'
1818
}

0 commit comments

Comments
 (0)