diff --git a/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java b/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java index 3b2d6d9..056eb40 100644 --- a/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java +++ b/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java @@ -17,9 +17,8 @@ package org.checkstyle.autofix; +import java.util.ArrayList; import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; @@ -49,21 +48,32 @@ public TreeVisitor getVisitor() { private static final class ViolationCommentRemover extends JavaIsoVisitor { @Override public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) { - final StringBuilder suffixAccumulator = new StringBuilder(); + String suffixAccumulator = null; + final List filteredComments = new ArrayList<>(); - final List filteredComments = space.getComments().stream() - .map(comment -> { - Comment result = comment; - if (!comment.isMultiline() && comment instanceof TextComment) { - final TextComment textComment = (TextComment) comment; - if (textComment.getText().startsWith("violation")) { - suffixAccumulator.append(textComment.getSuffix()); - result = null; - } + for (Comment comment : space.getComments()) { + if (!comment.isMultiline() && comment instanceof TextComment) { + final TextComment textComment = (TextComment) comment; + if (textComment.getText().matches("\\s*(\\d+\\s*)?violation.*")) { + if (filteredComments.isEmpty()) { + suffixAccumulator = textComment.getSuffix(); } - return result; }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + else { + final int lastIndex = filteredComments.size() - 1; + final Comment previousComment = filteredComments.get(lastIndex); + filteredComments.set(lastIndex, + previousComment.withSuffix(textComment.getSuffix())); + } + } + else { + filteredComments.add(comment); + } + + } + else { + filteredComments.add(comment); + } + } Space result; @@ -72,17 +82,8 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) { } else { result = space.withComments(filteredComments); - if (!suffixAccumulator.isEmpty()) { - if (filteredComments.isEmpty()) { - result = result.withWhitespace(suffixAccumulator.toString()); - } - else { - final Comment lastComment = filteredComments - .get(filteredComments.size() - 1); - filteredComments.set(filteredComments.size() - 1, - lastComment.withSuffix(suffixAccumulator.toString())); - result = space.withComments(filteredComments); - } + if (suffixAccumulator != null) { + result = result.withWhitespace(suffixAccumulator); } } return super.visitSpace(result, loc, ctx); diff --git a/src/test/java/org/checkstyle/autofix/recipe/RemoveViolationCommentsTest.java b/src/test/java/org/checkstyle/autofix/recipe/RemoveViolationCommentsTest.java new file mode 100644 index 0000000..e4f25f7 --- /dev/null +++ b/src/test/java/org/checkstyle/autofix/recipe/RemoveViolationCommentsTest.java @@ -0,0 +1,49 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. +// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +/////////////////////////////////////////////////////////////////////////////////////////////// + +package org.checkstyle.autofix.recipe; + +import static org.openrewrite.java.Assertions.java; + +import java.io.IOException; + +import org.checkstyle.autofix.InputClassRenamer; +import org.checkstyle.autofix.RemoveViolationComments; +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport; + +public class RemoveViolationCommentsTest extends AbstractPathTestSupport implements RewriteTest { + + @Override + protected String getPackageLocation() { + return "org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations"; + } + + @Test + void removeAllViolationCommentPatterns() throws IOException { + final String beforeCode = readFile(getPath("InputAllCommentVariations.java")); + final String afterCode = readFile(getPath("OutputAllCommentVariations.java")); + + rewriteRun( + spec -> { + spec.recipes(new InputClassRenamer(), new RemoveViolationComments()); + }, java(beforeCode, afterCode) + ); + } +} diff --git a/src/test/resources/org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations/InputAllCommentVariations.java b/src/test/resources/org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations/InputAllCommentVariations.java new file mode 100644 index 0000000..a2411cd --- /dev/null +++ b/src/test/resources/org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations/InputAllCommentVariations.java @@ -0,0 +1,32 @@ +package org.checkstyle.autofix.recipe.removeviolationcomments.allcommentvariations; + +public class InputAllCommentVariations { + + private int field1; // violation + private int field2; // 2 violation message + private int field3; // violation + private int field4; // 3 violation text + private int field5; // violation - missing javadoc + private int field6; // 10 violation some descriptive text + + /* This violation should not be removed - multiline */ + private int field7; // this is not a violation + private int field8; // some regular comment + + // Another regular comment + private int field9; // violation + // violation below + private int field10; // random comment + private int field11; // 25 violation + private int field12; + // violation above + + /** + * Javadoc with violation word should stay + */ + public void method() { + int local; // violation + // violation in standalone comment + int other; // normal comment + } +} \ No newline at end of file diff --git a/src/test/resources/org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations/OutputAllCommentVariations.java b/src/test/resources/org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations/OutputAllCommentVariations.java new file mode 100644 index 0000000..e6bc67a --- /dev/null +++ b/src/test/resources/org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations/OutputAllCommentVariations.java @@ -0,0 +1,29 @@ +package org.checkstyle.autofix.recipe.removeviolationcomments.allcommentvariations; + +public class OutputAllCommentVariations { + + private int field1; + private int field2; + private int field3; + private int field4; + private int field5; + private int field6; + + /* This violation should not be removed - multiline */ + private int field7; // this is not a violation + private int field8; // some regular comment + + // Another regular comment + private int field9; + private int field10; // random comment + private int field11; + private int field12; + + /** + * Javadoc with violation word should stay + */ + public void method() { + int local; + int other; // normal comment + } +} \ No newline at end of file