Skip to content

Commit ee86ef3

Browse files
committed
Issue #36: Updated CheckstyleAutoFix to load checkstyle-config.
1 parent 4bf149c commit ee86ef3

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-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: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
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.ConfigConfiguration;
30+
import org.checkstyle.autofix.parser.ConfigurationMapper;
2531
import org.openrewrite.Option;
2632
import org.openrewrite.Recipe;
2733

34+
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
35+
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
36+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37+
2838
/**
2939
* Main recipe that automatically fixes all supported Checkstyle violations.
3040
*/
@@ -35,6 +45,17 @@ public class CheckstyleAutoFix extends Recipe {
3545
example = "target/checkstyle/checkstyle-report.xml")
3646
private String violationReportPath;
3747

48+
@Option(displayName = "Checkstyle config path",
49+
description = "Path to the file containing Checkstyle configuration.",
50+
example = "config/checkstyle.xml")
51+
private String checkstyleConfigurationPath;
52+
53+
@Option(displayName = "Checkstyle properties file path",
54+
description = "Path to the file containing the Checkstyle Properties.",
55+
example = "config/checkstyle.properties",
56+
required = false)
57+
private String propertiesPath;
58+
3859
@Override
3960
public String getDisplayName() {
4061
return "Checkstyle autoFix";
@@ -49,12 +70,39 @@ public String getViolationReportPath() {
4970
return violationReportPath;
5071
}
5172

73+
public String getCheckstyleConfigurationPath() {
74+
return checkstyleConfigurationPath;
75+
}
76+
77+
public String getPropertiesPath() {
78+
return propertiesPath;
79+
}
80+
5281
@Override
5382
public List<Recipe> getRecipeList() {
54-
5583
final List<CheckstyleViolation> violations = CheckstyleReportParser
5684
.parse(Path.of(getViolationReportPath()));
5785

5886
return CheckstyleRecipeRegistry.getRecipes(violations);
5987
}
88+
89+
private ConfigConfiguration loadCheckstyleConfiguration()
90+
throws CheckstyleException, IOException {
91+
Properties props = new Properties();
92+
final String propFile = getPropertiesPath();
93+
94+
if (propFile == null) {
95+
props = System.getProperties();
96+
}
97+
else {
98+
try (FileInputStream input = new FileInputStream(propFile)) {
99+
props.load(input);
100+
}
101+
catch (FileNotFoundException exception) {
102+
throw new IllegalArgumentException("Failed to read: " + propFile, exception);
103+
}
104+
}
105+
return ConfigurationMapper.mapConfiguration(ConfigurationLoader.loadConfiguration(
106+
getCheckstyleConfigurationPath(), new PropertiesExpander(props)));
107+
}
60108
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.util.Map;
21+
22+
public final class ConfigConfiguration {
23+
private final Map<String, String> properties;
24+
private final ConfigConfiguration[] children;
25+
26+
public ConfigConfiguration(Map<String, String> properties, ConfigConfiguration[] children) {
27+
this.properties = properties;
28+
this.children = children;
29+
}
30+
31+
public Map<String, String> getProperties() {
32+
return properties;
33+
}
34+
35+
public ConfigConfiguration[] getChildren() {
36+
return children;
37+
}
38+
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.util.HashMap;
21+
import java.util.Map;
22+
23+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
24+
import com.puppycrawl.tools.checkstyle.api.Configuration;
25+
26+
public final class ConfigurationMapper {
27+
28+
private ConfigurationMapper() {
29+
// utility class
30+
}
31+
32+
public static ConfigConfiguration mapConfiguration(Configuration config) {
33+
final Map<String, String> properties = new HashMap<>();
34+
final String[] propertyNames = config.getPropertyNames();
35+
for (String propertyName : propertyNames) {
36+
try {
37+
final String value = config.getProperty(propertyName);
38+
properties.put(propertyName, value);
39+
40+
}
41+
catch (CheckstyleException exception) {
42+
throw new IllegalArgumentException("Error getting property "
43+
+ propertyName + ": " + exception.getMessage());
44+
}
45+
}
46+
47+
final Configuration[] checkstyleChildren = config.getChildren();
48+
final ConfigConfiguration[] simpleChildren =
49+
new ConfigConfiguration[checkstyleChildren.length];
50+
for (int index = 0; index < checkstyleChildren.length; index++) {
51+
simpleChildren[index] = mapConfiguration(checkstyleChildren[index]);
52+
}
53+
54+
return new ConfigConfiguration(properties, simpleChildren);
55+
}
56+
}

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)