Skip to content

Commit 3e2322b

Browse files
committed
Updated CheckstyleAutoFix to load report
1 parent 706b598 commit 3e2322b

File tree

10 files changed

+189
-46
lines changed

10 files changed

+189
-46
lines changed

config/suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
<suppress checks="JavadocVariable" files=".*"/>
1010
<suppress checks="MissingJavadocMethod" files=".*"/>
1111
<suppress checks="DesignForExtension" files="[\\/]src[\\/]test[\\/]java[\\/]org[\\/]checkstyle[\\/]autofix[\\/]recipe[\\/]AbstractRecipeTest\.java"/>
12+
<suppress checks="DesignForExtension" files="[\\/]src[\\/]main[\\/]java[\\/]org[\\/]checkstyle[\\/]autofix[\\/]CheckstyleAutoFix\.java"/>
1213
</suppressions>

pom.xml

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,32 +111,6 @@
111111
</configuration>
112112
</plugin>
113113

114-
<plugin>
115-
<groupId>org.openrewrite.maven</groupId>
116-
<artifactId>rewrite-maven-plugin</artifactId>
117-
<version>${rewrite.maven.plugin}</version>
118-
<configuration>
119-
<activeRecipes>
120-
<recipe>org.checkstyle.autofix.CheckstyleAutoFix</recipe>
121-
</activeRecipes>
122-
<exclusions>
123-
<exclusion>src/test/resources/**</exclusion>
124-
</exclusions>
125-
<recipeArtifactCoordinates>
126-
<coordinate>org.checkstyle.autofix:checkstyle-openrewrite-recipes:1.0.0</coordinate>
127-
</recipeArtifactCoordinates>
128-
</configuration>
129-
<executions>
130-
<execution>
131-
<id>checkstyle-autofix</id>
132-
<phase>process-sources</phase>
133-
<goals>
134-
<goal>run</goal>
135-
</goals>
136-
</execution>
137-
</executions>
138-
</plugin>
139-
140114
<!-- Checkstyle plugin for code style regulation -->
141115

142116
<plugin>
@@ -176,6 +150,33 @@
176150
</execution>
177151
</executions>
178152
</plugin>
153+
154+
<plugin>
155+
<groupId>org.openrewrite.maven</groupId>
156+
<artifactId>rewrite-maven-plugin</artifactId>
157+
<version>${rewrite.maven.plugin}</version>
158+
<configuration>
159+
<activeRecipes>
160+
<recipe>CheckstyleAutoFixConfigured</recipe>
161+
</activeRecipes>
162+
<exclusions>
163+
<exclusion>src/test/resources/**</exclusion>
164+
</exclusions>
165+
<recipeArtifactCoordinates>
166+
<coordinate>org.checkstyle.autofix:checkstyle-openrewrite-recipes:1.0.0</coordinate>
167+
</recipeArtifactCoordinates>
168+
</configuration>
169+
<executions>
170+
<execution>
171+
<id>checkstyle-autofix</id>
172+
<phase>verify</phase>
173+
<goals>
174+
<goal>run</goal>
175+
</goals>
176+
</execution>
177+
</executions>
178+
</plugin>
179+
179180
</plugins>
180181
</build>
181182

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

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

1818
package org.checkstyle.autofix;
1919

20-
import java.util.Collections;
20+
import java.nio.file.Path;
2121
import java.util.List;
2222

23-
import org.checkstyle.autofix.recipe.UpperEll;
23+
import org.checkstyle.autofix.parser.CheckstyleReportParser;
24+
import org.checkstyle.autofix.parser.CheckstyleViolation;
25+
import org.openrewrite.Option;
2426
import org.openrewrite.Recipe;
2527

2628
/**
2729
* Main recipe that automatically fixes all supported Checkstyle violations.
2830
*/
2931
public class CheckstyleAutoFix extends Recipe {
3032

33+
@Option(displayName = "Violation report path",
34+
description = "Path to the checkstyle violation report XML file.",
35+
example = "target/checkstyle/checkstyle-report.xml")
36+
private String violationReportPath;
37+
3138
@Override
3239
public String getDisplayName() {
3340
return "Checkstyle autoFix";
@@ -38,11 +45,16 @@ public String getDescription() {
3845
return "Automatically fixes Checkstyle violations.";
3946
}
4047

48+
public String getViolationReportPath() {
49+
return violationReportPath;
50+
}
51+
4152
@Override
4253
public List<Recipe> getRecipeList() {
43-
return Collections.singletonList(
4454

45-
new UpperEll()
46-
);
55+
final List<CheckstyleViolation> violations = CheckstyleReportParser
56+
.parse(Path.of(getViolationReportPath()));
57+
58+
return CheckstyleRecipeRegistry.getRecipes(violations);
4759
}
4860
}
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.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.function.Function;
25+
import java.util.stream.Collectors;
26+
27+
import org.checkstyle.autofix.parser.CheckstyleViolation;
28+
import org.checkstyle.autofix.recipe.UpperEll;
29+
import org.openrewrite.Recipe;
30+
31+
public final class CheckstyleRecipeRegistry {
32+
33+
private static final Map<String, Function<List<CheckstyleViolation>, Recipe>> RECIPE_MAP =
34+
new HashMap<>();
35+
36+
static {
37+
RECIPE_MAP.put("UpperEllCheck", UpperEll::new);
38+
}
39+
40+
private CheckstyleRecipeRegistry() {
41+
// utility class
42+
}
43+
44+
/**
45+
* Returns a list of Recipe objects based on the given list of Checkstyle violations.
46+
* The method groups violations by their check name, finds the matching recipe factory
47+
* using the simple name of the check, and applies the factory to generate Recipe instances.
48+
*
49+
* @param violations the list of Checkstyle violations
50+
* @return a list of generated Recipe objects
51+
*/
52+
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations) {
53+
54+
final Map<String, List<CheckstyleViolation>> violationsByCheck = violations.stream()
55+
.collect(Collectors.groupingBy(CheckstyleViolation::getSource));
56+
57+
final List<Recipe> recipes = new ArrayList<>();
58+
59+
for (Map.Entry<String, List<CheckstyleViolation>> entry : violationsByCheck.entrySet()) {
60+
final String checkName = entry.getKey();
61+
final String simpleCheckName = checkName
62+
.substring(checkName.lastIndexOf('.') + 1);
63+
final List<CheckstyleViolation> checkViolations = entry.getValue();
64+
65+
final Function<List<CheckstyleViolation>, Recipe> recipeFactory =
66+
RECIPE_MAP.get(simpleCheckName);
67+
if (recipeFactory != null) {
68+
recipes.add(recipeFactory.apply(checkViolations));
69+
}
70+
}
71+
72+
return recipes;
73+
}
74+
}

src/main/java/org/checkstyle/autofix/parser/CheckstyleReportsParser.java renamed to src/main/java/org/checkstyle/autofix/parser/CheckstyleReportParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import javax.xml.stream.events.StartElement;
3434
import javax.xml.stream.events.XMLEvent;
3535

36-
public final class CheckstyleReportsParser {
36+
public final class CheckstyleReportParser {
3737

3838
private static final String FILE_TAG = "file";
3939

@@ -51,7 +51,7 @@ public final class CheckstyleReportsParser {
5151

5252
private static final String SOURCE_ATTR = "source";
5353

54-
private CheckstyleReportsParser() {
54+
private CheckstyleReportParser() {
5555

5656
}
5757

src/main/java/org/checkstyle/autofix/recipe/UpperEll.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.checkstyle.autofix.recipe;
1919

2020
import java.nio.file.Path;
21-
import java.util.ArrayList;
2221
import java.util.List;
2322
import java.util.concurrent.CancellationException;
2423
import java.util.function.Function;
@@ -42,10 +41,6 @@ public class UpperEll extends Recipe {
4241

4342
private final List<CheckstyleViolation> violations;
4443

45-
public UpperEll() {
46-
this(new ArrayList<>());
47-
}
48-
4944
public UpperEll(List<CheckstyleViolation> violations) {
5045
this.violations = violations;
5146
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
---
22
type: specs.openrewrite.org/v1beta/recipe
3-
name: org.checkstyle.autofix.CheckstyleAutoFix
4-
displayName: Checkstyle autoFix
5-
description: Automatically fixes Checkstyle violations.
3+
name: CheckstyleAutoFixConfigured
4+
displayName: Checkstyle Auto Fix Configured
5+
description: |
6+
Automatically applies OpenRewrite recipes to fix Checkstyle violations
7+
based on the provided Checkstyle XML violation report. This recipe serves
8+
as an entry point to apply all auto-fixable rules configured in the report.
69
tags:
710
- checkstyle
11+
- autofix
812
- static-analysis
13+
- java
14+
- code-quality
915
recipeList:
10-
- org.checkstyle.autofix.recipe.UpperEll
16+
- org.checkstyle.autofix.CheckstyleAutoFix:
17+
violationReportPath: "target/checkstyle/checkstyle-report.xml"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
import java.util.List;
23+
24+
import org.checkstyle.autofix.parser.CheckstyleViolation;
25+
import org.junit.jupiter.api.Test;
26+
import org.openrewrite.Recipe;
27+
28+
public class CheckstyleRecipeRegistryTest {
29+
30+
@Test
31+
void testGetRecipesReturnsCorrectRecipe() {
32+
33+
final List<CheckstyleViolation> violations = List.of(
34+
new CheckstyleViolation(5, 10, "error",
35+
"com.puppycrawl.tools.checkstyle.checks.UpperEllCheck",
36+
"Use uppercase 'L' for long literals.", "Example1.java"),
37+
38+
new CheckstyleViolation(15, 20, "error",
39+
"com.puppycrawl.tools.checkstyle.checks.UpperEllCheck",
40+
"Use uppercase 'L' for long literals.", "Example2.java"),
41+
42+
new CheckstyleViolation(8, 12, "error",
43+
"com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck",
44+
"Variable should be declared final.", "Example3.java")
45+
);
46+
47+
final List<Recipe> recipes = CheckstyleRecipeRegistry.getRecipes(violations);
48+
49+
assertEquals(1, recipes.size(), "Should return one recipe");
50+
assertEquals("UpperEll recipe", recipes.get(0).getDisplayName());
51+
}
52+
53+
}

src/test/java/org/checkstyle/autofix/parser/CheckstyleReportsParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static String getPath(String path) {
3636
@Test
3737
public void testParseFromResource() throws Exception {
3838
final Path xmlPath = Path.of(getPath("checkstyle-report.xml"));
39-
final List<CheckstyleViolation> records = CheckstyleReportsParser.parse(xmlPath);
39+
final List<CheckstyleViolation> records = CheckstyleReportParser.parse(xmlPath);
4040

4141
assertNotNull(records);
4242
assertEquals(1, records.size());
@@ -53,7 +53,7 @@ public void testParseFromResource() throws Exception {
5353
@Test
5454
public void testParseMultipleFilesReport() throws Exception {
5555
final Path xmlPath = Path.of(getPath("checkstyle-multiple-files.xml"));
56-
final List<CheckstyleViolation> records = CheckstyleReportsParser.parse(xmlPath);
56+
final List<CheckstyleViolation> records = CheckstyleReportParser.parse(xmlPath);
5757

5858
assertNotNull(records);
5959
assertEquals(3, records.size());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.nio.file.Path;
2222
import java.util.List;
2323

24-
import org.checkstyle.autofix.parser.CheckstyleReportsParser;
24+
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2525
import org.checkstyle.autofix.parser.CheckstyleViolation;
2626
import org.junit.jupiter.api.Test;
2727
import org.openrewrite.Recipe;
@@ -34,7 +34,7 @@ protected Recipe getRecipe() {
3434
+ "/report.xml";
3535

3636
final List<CheckstyleViolation> violations =
37-
CheckstyleReportsParser.parse(Path.of(reportPath));
37+
CheckstyleReportParser.parse(Path.of(reportPath));
3838
return new UpperEll(violations);
3939
}
4040

0 commit comments

Comments
 (0)