Skip to content

Commit 996f49a

Browse files
committed
Issue #36: Updated CheckstyleAutoFix to load checkstyle-config.
1 parent cb642ab commit 996f49a

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

config/import-control.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<allow pkg="javax.xml.stream"/>
1212
<allow pkg="org.checkstyle"/>
1313
<allow pkg="java.util"/>
14+
<allow pkg="com.puppycrawl.tools.checkstyle"/>
1415
</import-control>

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@
1717

1818
package org.checkstyle.autofix;
1919

20+
import java.io.FileInputStream;
21+
import java.io.FileNotFoundException;
22+
import java.io.IOException;
2023
import java.nio.file.Path;
2124
import java.util.List;
25+
import java.util.Properties;
2226

2327
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2428
import org.checkstyle.autofix.parser.CheckstyleViolation;
29+
import org.checkstyle.autofix.parser.Configuration;
2530
import org.openrewrite.Option;
2631
import org.openrewrite.Recipe;
2732

33+
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
34+
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
35+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
36+
2837
/**
2938
* Main recipe that automatically fixes all supported Checkstyle violations.
3039
*/
@@ -35,6 +44,17 @@ public class CheckstyleAutoFix extends Recipe {
3544
example = "target/checkstyle/checkstyle-report.xml")
3645
private String violationReportPath;
3746

47+
@Option(displayName = "Checkstyle config path",
48+
description = "Path to the file containing Checkstyle configuration.",
49+
example = "config/checkstyle.xml")
50+
private String checkstyleConfigurationPath;
51+
52+
@Option(displayName = "Checkstyle properties file path",
53+
description = "Path to the file containing the Checkstyle Properties.",
54+
example = "config/checkstyle.properties",
55+
required = false)
56+
private String propertiesPath;
57+
3858
@Override
3959
public String getDisplayName() {
4060
return "Checkstyle autoFix";
@@ -49,12 +69,38 @@ public String getViolationReportPath() {
4969
return violationReportPath;
5070
}
5171

72+
public String getCheckstyleConfigurationPath() {
73+
return checkstyleConfigurationPath;
74+
}
75+
76+
public String getPropertiesPath() {
77+
return propertiesPath;
78+
}
79+
5280
@Override
5381
public List<Recipe> getRecipeList() {
54-
5582
final List<CheckstyleViolation> violations = CheckstyleReportParser
5683
.parse(Path.of(getViolationReportPath()));
5784

5885
return CheckstyleRecipeRegistry.getRecipes(violations);
5986
}
87+
88+
private Configuration loadCheckstyleConfiguration() throws CheckstyleException, IOException {
89+
Properties props = new Properties();
90+
final String propFile = getPropertiesPath();
91+
92+
if (propFile == null) {
93+
props = System.getProperties();
94+
}
95+
else {
96+
try (FileInputStream input = new FileInputStream(propFile)) {
97+
props.load(input);
98+
}
99+
catch (FileNotFoundException exception) {
100+
throw new IllegalArgumentException("Failed to read: " + propFile, exception);
101+
}
102+
}
103+
return (Configuration) ConfigurationLoader.loadConfiguration(
104+
checkstyleConfigurationPath, new PropertiesExpander(props));
105+
}
60106
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
import java.io.Serializable;
21+
import java.util.Map;
22+
23+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
24+
25+
public interface Configuration extends Serializable {
26+
27+
String[] getAttributeNames();
28+
29+
String getAttribute(String name) throws CheckstyleException;
30+
31+
String[] getPropertyNames();
32+
33+
String getProperty(String name) throws CheckstyleException;
34+
35+
Configuration[] getChildren();
36+
37+
String getName();
38+
39+
Map<String, String> getMessages();
40+
41+
}

src/main/resources/META-INF/rewrite/recipes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ tags:
1515
recipeList:
1616
- org.checkstyle.autofix.CheckstyleAutoFix:
1717
violationReportPath: "target/checkstyle/checkstyle-report.xml"
18+
checkstyleConfigurationPath: "https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${checkstyle.version}/config/checkstyle-checks.xml"

0 commit comments

Comments
 (0)