Skip to content

Commit 6281305

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue checkstyle#85: resolved position helper bug
1 parent 4d6bb41 commit 6281305

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

src/main/java/org/checkstyle/autofix/PositionHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
package org.checkstyle.autofix;
1919

20+
import java.util.List;
2021
import java.util.concurrent.CancellationException;
2122
import java.util.function.Function;
2223

2324
import org.openrewrite.Cursor;
2425
import org.openrewrite.PrintOutputCapture;
2526
import org.openrewrite.TreeVisitor;
2627
import org.openrewrite.internal.RecipeRunException;
28+
import org.openrewrite.java.tree.Comment;
2729
import org.openrewrite.java.tree.J;
2830

2931
public final class PositionHelper {
@@ -65,6 +67,12 @@ private static int computePosition(
6567
public PrintOutputCapture<TreeVisitor<?, ?>> append(String text) {
6668
if (targetElement.isScope(getContext().getCursor().getValue())) {
6769
super.append(targetElement.getPrefix().getWhitespace());
70+
71+
final List<Comment> comments = targetElement.getPrefix().getComments();
72+
for (Comment comment : comments) {
73+
super.append(comment.printComment(cursor) + comment.getSuffix());
74+
}
75+
6876
throw new CancellationException();
6977
}
7078
return super.append(text);

src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/InputSingleLocalTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public void basicLocalVariables() {
2626
List<String> items = new ArrayList<>(); // violation, "should be declared final"
2727
StringBuilder builder = new StringBuilder(); // violation, "should be declared final"
2828
Date currentDate = new Date(); // violation, "should be declared final"
29-
File tempFile = new File("temp.txt"); // violation, "should be declared final"
29+
File // prefix comment preceding target element
30+
tempFile = new File("temp.txt"); // violation, "should be declared final"
3031
}
3132

3233
public void methodWithCalculations() {

src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/OutputSingleLocalTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public void basicLocalVariables() {
2626
final List<String> items = new ArrayList<>();
2727
final StringBuilder builder = new StringBuilder();
2828
final Date currentDate = new Date();
29-
final File tempFile = new File("temp.txt");
29+
final File // prefix comment preceding target element
30+
tempFile = new File("temp.txt");
3031
}
3132

3233
public void methodWithCalculations() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class InputHexOctalLiteral {
1818
private long hexUpper = 0X2DEFl; // violation 'Should use uppercase 'L'.'
1919
private long octal = 0777l; // violation 'Should use uppercase 'L'.'
2020
private long binary = 0b1010l; // violation 'Should use uppercase 'L'.'
21-
private long decimal = 12345l; // violation 'Should use uppercase 'L'.'
21+
private long decimal = // comment preceding a target element
22+
12345l; // violation 'Should use uppercase 'L'.'
2223

2324
public void calculateValues() {
2425
// violation below, 'Should use uppercase 'L'.'

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class OutputHexOctalLiteral {
1818
private long hexUpper = 0X2DEFL;
1919
private long octal = 0777L;
2020
private long binary = 0b1010L;
21-
private long decimal = 12345L;
21+
private long decimal = // comment preceding a target element
22+
12345L;
2223

2324
public void calculateValues() {
2425
long hexResult = 0xDEADBEEFL + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl

0 commit comments

Comments
 (0)