Skip to content

Commit 0ce8063

Browse files
committed
Issue #41: Added InptConfigRemover recipe for preprocessing
1 parent a9615dc commit 0ce8063

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
3+
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
///////////////////////////////////////////////////////////////////////////////////////////////
17+
18+
package org.checkstyle.autofix;
19+
20+
import java.util.stream.Collectors;
21+
22+
import org.openrewrite.ExecutionContext;
23+
import org.openrewrite.Recipe;
24+
import org.openrewrite.Tree;
25+
import org.openrewrite.TreeVisitor;
26+
import org.openrewrite.java.JavaIsoVisitor;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.JavaSourceFile;
29+
import org.openrewrite.java.tree.Space;
30+
31+
public class InputConfigRemover extends Recipe {
32+
33+
@Override
34+
public String getDisplayName() {
35+
return "InputConfigRemover recipe";
36+
}
37+
38+
@Override
39+
public String getDescription() {
40+
return "Remove inline config.";
41+
}
42+
43+
@Override
44+
public TreeVisitor<?, ExecutionContext> getVisitor() {
45+
return new CommentVisitor();
46+
}
47+
48+
private static final class CommentVisitor extends JavaIsoVisitor<ExecutionContext> {
49+
50+
@Override
51+
public J visit(Tree tree, ExecutionContext ctx) {
52+
J result = super.visit(tree, ctx);
53+
54+
if (tree instanceof JavaSourceFile) {
55+
JavaSourceFile sourceFile = (JavaSourceFile) tree;
56+
57+
final String currentHeader = extractCurrentHeader(sourceFile);
58+
59+
if (currentHeader.startsWith("/*xml")) {
60+
sourceFile = sourceFile.withPrefix(Space.format(""));
61+
}
62+
63+
result = super.visit(sourceFile, ctx);
64+
}
65+
return result;
66+
}
67+
68+
private String extractCurrentHeader(JavaSourceFile sourceFile) {
69+
return sourceFile.getComments().stream()
70+
.map(comment -> comment.printComment(getCursor()))
71+
.collect(Collectors.joining(System.lineSeparator()));
72+
}
73+
}
74+
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929

3030
import org.checkstyle.autofix.InputClassRenamer;
31+
import org.checkstyle.autofix.InputConfigRemover;
3132
import org.checkstyle.autofix.parser.CheckstyleReportParser;
3233
import org.checkstyle.autofix.parser.CheckstyleViolation;
3334
import org.openrewrite.Recipe;
@@ -54,10 +55,6 @@ protected String getPackageLocation() {
5455
return "org/checkstyle/autofix/recipe/" + getSubpackage();
5556
}
5657

57-
private Recipe createPreprocessingRecipe() {
58-
return new InputClassRenamer();
59-
}
60-
6158
protected void verify(String testCaseName) throws Exception {
6259
final String inputFileName = "Input" + testCaseName + ".java";
6360
final String outputFileName = "Output" + testCaseName + ".java";
@@ -73,7 +70,7 @@ protected void verify(String testCaseName) throws Exception {
7370
final Recipe mainRecipe = createRecipe(violations);
7471

7572
testRecipe(beforeCode, expectedAfterCode,
76-
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
73+
getPath(inputPath), new InputClassRenamer(), new InputConfigRemover(), mainRecipe);
7774
}
7875

7976
private List<CheckstyleViolation> runCheckstyle(String inputPath,

0 commit comments

Comments
 (0)