Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/test/java/org/checkstyle/autofix/RemoveViolationComments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,21 +48,32 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private static final class ViolationCommentRemover extends JavaIsoVisitor<ExecutionContext> {
@Override
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
final StringBuilder suffixAccumulator = new StringBuilder();
String suffixAccumulator = null;
final List<Comment> filteredComments = new ArrayList<>();

final List<Comment> 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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading