Skip to content

Commit 0344ae3

Browse files
committed
Updated CheckstyleAutoFix to load report
1 parent 4c0fe1d commit 0344ae3

File tree

16 files changed

+661
-47
lines changed

16 files changed

+661
-47
lines changed

config/import-control-test.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
<allow pkg="org.openrewrite"/>
99
<allow pkg="org.checkstyle"/>
1010
<allow pkg="java.io"/>
11+
<allow pkg="java.util"/>
1112
<allow pkg="java.nio"/>
13+
<allow pkg="java.lang"/>
14+
<allow pkg="javax.xml.stream"/>
1215
</import-control>

config/import-control.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<allow pkg="org.openrewrite.java"/>
99
<allow pkg="org.openrewrite.java.tree"/>
1010
<allow pkg="java"/>
11+
<allow pkg="javax.xml.stream"/>
1112
<allow pkg="org.checkstyle"/>
1213
<allow pkg="java.util"/>
13-
</import-control>
14+
</import-control>

config/suppressions.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
"-//Checkstyle//DTD SuppressionFilter Configuration 1.1//EN"
55
"https://checkstyle.org/dtds/suppressions_1_1.dtd">
66

7-
<suppressions/>
7+
<suppressions>
8+
<suppress checks="MissingJavadocType" files=".*"/>
9+
<suppress checks="JavadocVariable" files=".*"/>
10+
<suppress checks="MissingJavadocMethod" files=".*"/>
11+
</suppressions>

pom.xml

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
</dependencyManagement>
4545

4646
<dependencies>
47+
48+
<!-- Checkstyle dependency for parsing Checkstyle configs -->
49+
<dependency>
50+
<groupId>com.puppycrawl.tools</groupId>
51+
<artifactId>checkstyle</artifactId>
52+
<version>${checkstyle.version}</version>
53+
</dependency>
54+
4755
<!-- OpenRewrite core dependencies - using consistent versions -->
4856
<dependency>
4957
<groupId>org.openrewrite</groupId>
@@ -103,32 +111,6 @@
103111
</configuration>
104112
</plugin>
105113

106-
<plugin>
107-
<groupId>org.openrewrite.maven</groupId>
108-
<artifactId>rewrite-maven-plugin</artifactId>
109-
<version>${rewrite.maven.plugin}</version>
110-
<configuration>
111-
<activeRecipes>
112-
<recipe>org.checkstyle.autofix.CheckstyleAutoFix</recipe>
113-
</activeRecipes>
114-
<exclusions>
115-
<exclusion>src/test/resources/**</exclusion>
116-
</exclusions>
117-
<recipeArtifactCoordinates>
118-
<coordinate>org.checkstyle.autofix:checkstyle-openrewrite-recipes:1.0.0</coordinate>
119-
</recipeArtifactCoordinates>
120-
</configuration>
121-
<executions>
122-
<execution>
123-
<id>checkstyle-autofix</id>
124-
<phase>process-sources</phase>
125-
<goals>
126-
<goal>run</goal>
127-
</goals>
128-
</execution>
129-
</executions>
130-
</plugin>
131-
132114
<!-- Checkstyle plugin for code style regulation -->
133115

134116
<plugin>
@@ -145,6 +127,7 @@
145127
<executions>
146128
<execution>
147129
<id>check</id>
130+
<phase>verify</phase>
148131
<goals>
149132
<goal>check</goal>
150133
</goals>
@@ -168,6 +151,32 @@
168151
</execution>
169152
</executions>
170153
</plugin>
154+
155+
<plugin>
156+
<groupId>org.openrewrite.maven</groupId>
157+
<artifactId>rewrite-maven-plugin</artifactId>
158+
<version>${rewrite.maven.plugin}</version>
159+
<configuration>
160+
<activeRecipes>
161+
<recipe>org.checkstyle.autofix.CheckstyleAutoFix</recipe>
162+
</activeRecipes>
163+
<exclusions>
164+
<exclusion>src/test/resources/**</exclusion>
165+
</exclusions>
166+
<recipeArtifactCoordinates>
167+
<coordinate>org.checkstyle.autofix:checkstyle-openrewrite-recipes:1.0.0</coordinate>
168+
</recipeArtifactCoordinates>
169+
</configuration>
170+
<executions>
171+
<execution>
172+
<id>checkstyle-autofix</id>
173+
<phase>verify</phase>
174+
<goals>
175+
<goal>run</goal>
176+
</goals>
177+
</execution>
178+
</executions>
179+
</plugin>
171180
</plugins>
172181
</build>
173182

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

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

1818
package org.checkstyle.autofix;
1919

20-
import java.util.Collections;
20+
import java.io.FileNotFoundException;
21+
import java.nio.file.Path;
2122
import java.util.List;
2223

23-
import org.checkstyle.autofix.recipe.UpperEllRecipe;
24+
import javax.xml.stream.XMLStreamException;
25+
26+
import org.checkstyle.autofix.parser.CheckstyleRecord;
27+
import org.checkstyle.autofix.parser.CheckstyleReportsParser;
2428
import org.openrewrite.Recipe;
2529

2630
/**
2731
* Main recipe that automatically fixes all supported Checkstyle violations.
2832
*/
2933
public class CheckstyleAutoFix extends Recipe {
3034

35+
private static final Path DEFAULT_VIOLATION_REPORT_PATH = Path.of("target",
36+
"checkstyle", "checkstyle-report.xml");
37+
38+
private final Path violationReportPath;
39+
40+
public CheckstyleAutoFix() {
41+
this.violationReportPath = DEFAULT_VIOLATION_REPORT_PATH;
42+
}
43+
44+
public CheckstyleAutoFix(Path violationReportPath) {
45+
this.violationReportPath = violationReportPath;
46+
}
47+
3148
@Override
3249
public String getDisplayName() {
3350
return "Checkstyle autoFix";
@@ -40,9 +57,17 @@ public String getDescription() {
4057

4158
@Override
4259
public List<Recipe> getRecipeList() {
43-
return Collections.singletonList(
60+
final List<CheckstyleRecord> violations;
61+
try {
62+
violations = CheckstyleReportsParser.parse(violationReportPath);
63+
}
64+
catch (FileNotFoundException | XMLStreamException exception) {
65+
throw new RuntimeException("Failed to parse violation report: " + violationReportPath,
66+
exception);
67+
}
68+
69+
return CheckstyleRecipeRegistry.getRecipes(violations);
4470

45-
new UpperEllRecipe()
46-
);
4771
}
72+
4873
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.CheckstyleRecord;
28+
import org.checkstyle.autofix.recipe.UpperEllRecipe;
29+
import org.openrewrite.Recipe;
30+
31+
public final class CheckstyleRecipeRegistry {
32+
33+
private static final Map<String, Function<List<CheckstyleRecord>, Recipe>> RECIPE_MAP =
34+
new HashMap<>();
35+
36+
static {
37+
RECIPE_MAP.put("com.puppycrawl.tools.checkstyle.checks.coding.UpperEllCheck",
38+
UpperEllRecipe::new);
39+
}
40+
41+
private CheckstyleRecipeRegistry() {
42+
// utility class
43+
}
44+
45+
public static List<Recipe> getRecipes(List<CheckstyleRecord> violations) {
46+
47+
final Map<String, List<CheckstyleRecord>> violationsByCheck = violations.stream()
48+
.collect(Collectors.groupingBy(CheckstyleRecord::getSource));
49+
50+
final List<Recipe> recipes = new ArrayList<>();
51+
52+
for (Map.Entry<String, List<CheckstyleRecord>> entry : violationsByCheck.entrySet()) {
53+
final String checkName = entry.getKey();
54+
final List<CheckstyleRecord> records = entry.getValue();
55+
56+
final Function<List<CheckstyleRecord>, Recipe> recipeFactory =
57+
RECIPE_MAP.get(checkName);
58+
if (recipeFactory != null) {
59+
recipes.add(recipeFactory.apply(records));
60+
}
61+
}
62+
63+
return recipes;
64+
}
65+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.parser;
19+
20+
public final class CheckstyleRecord {
21+
22+
private final int line;
23+
24+
private final int column;
25+
26+
private final String severity;
27+
28+
private final String source;
29+
30+
private final String message;
31+
32+
private String fileName;
33+
34+
public CheckstyleRecord(int line, int column,
35+
String severity, String source, String message, String fileName) {
36+
this.line = line;
37+
this.column = column;
38+
this.severity = severity;
39+
this.source = source;
40+
this.message = message;
41+
this.fileName = fileName;
42+
}
43+
44+
public int getLine() {
45+
return line;
46+
}
47+
48+
public int getColumn() {
49+
return column;
50+
}
51+
52+
public String getSource() {
53+
return source;
54+
}
55+
56+
public String getMessage() {
57+
return message;
58+
}
59+
60+
public String getFileName() {
61+
return fileName;
62+
}
63+
64+
public String getSeverity() {
65+
return severity;
66+
}
67+
68+
}

0 commit comments

Comments
 (0)