Skip to content

Commit 7c70af7

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

File tree

5 files changed

+209
-1
lines changed

5 files changed

+209
-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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.nio.file.Path;
2121
import java.util.List;
2222

23+
import org.checkstyle.autofix.parser.CheckConfiguration;
2324
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2425
import org.checkstyle.autofix.parser.CheckstyleViolation;
26+
import org.checkstyle.autofix.parser.ConfigurationLoader;
2527
import org.openrewrite.Option;
2628
import org.openrewrite.Recipe;
2729

@@ -35,6 +37,17 @@ public class CheckstyleAutoFix extends Recipe {
3537
example = "target/checkstyle/checkstyle-report.xml")
3638
private String violationReportPath;
3739

40+
@Option(displayName = "Checkstyle config path",
41+
description = "Path to the file containing Checkstyle configuration.",
42+
example = "config/checkstyle.xml")
43+
private String configurationPath;
44+
45+
@Option(displayName = "Checkstyle properties file path",
46+
description = "Path to the file containing the Checkstyle Properties.",
47+
example = "config/checkstyle.properties",
48+
required = false)
49+
private String propertiesPath;
50+
3851
@Override
3952
public String getDisplayName() {
4053
return "Checkstyle autoFix";
@@ -49,12 +62,23 @@ public String getViolationReportPath() {
4962
return violationReportPath;
5063
}
5164

65+
public String getConfigurationPath() {
66+
return configurationPath;
67+
}
68+
69+
public String getPropertiesPath() {
70+
return propertiesPath;
71+
}
72+
5273
@Override
5374
public List<Recipe> getRecipeList() {
54-
5575
final List<CheckstyleViolation> violations = CheckstyleReportParser
5676
.parse(Path.of(getViolationReportPath()));
5777

5878
return CheckstyleRecipeRegistry.getRecipes(violations);
5979
}
80+
81+
private CheckConfiguration loadCheckstyleConfiguration() {
82+
return ConfigurationLoader.loadConfiguration(getConfigurationPath(), getPropertiesPath());
83+
}
6084
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.List;
21+
import java.util.Map;
22+
23+
public final class CheckConfiguration {
24+
private final String name;
25+
private final Map<String, String> properties;
26+
private final List<CheckConfiguration> children;
27+
private CheckConfiguration parent;
28+
29+
public CheckConfiguration(String name,
30+
Map<String, String> properties, List<CheckConfiguration> children) {
31+
this.name = name;
32+
this.properties = properties;
33+
this.children = children;
34+
35+
for (CheckConfiguration child : children) {
36+
child.setParent(this);
37+
}
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public Map<String, String> getProperties() {
45+
return properties;
46+
}
47+
48+
public List<CheckConfiguration> getChildren() {
49+
return children;
50+
}
51+
52+
public String getProperty(String key) {
53+
return properties.get(key);
54+
}
55+
56+
public int[] getIntArray(String propertyName) {
57+
final String value = properties.get(propertyName);
58+
final int[] result;
59+
final String[] parts = value.split(",");
60+
result = new int[parts.length];
61+
for (int index = 0; index < parts.length; index++) {
62+
try {
63+
result[index] = Integer.parseInt(parts[index].trim());
64+
}
65+
catch (NumberFormatException exception) {
66+
throw new IllegalArgumentException("Property '" + propertyName
67+
+ "' has an invalid integer value: " + parts[index].trim(), exception);
68+
}
69+
}
70+
return result;
71+
}
72+
73+
public CheckConfiguration getChild(String childName) {
74+
CheckConfiguration result = null;
75+
for (CheckConfiguration child : children) {
76+
if (childName.equals(child.getName())) {
77+
result = child;
78+
break;
79+
}
80+
}
81+
return result;
82+
}
83+
84+
public CheckConfiguration getParent() {
85+
return parent;
86+
}
87+
88+
private void setParent(CheckConfiguration parent) {
89+
this.parent = parent;
90+
}
91+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.FileInputStream;
21+
import java.io.IOException;
22+
import java.util.HashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Properties;
26+
27+
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
28+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
29+
import com.puppycrawl.tools.checkstyle.api.Configuration;
30+
31+
public final class ConfigurationLoader {
32+
33+
private ConfigurationLoader() {
34+
// utility class
35+
}
36+
37+
private static CheckConfiguration mapConfiguration(Configuration config) {
38+
final Map<String, String> properties = new HashMap<>();
39+
final String[] propertyNames = config.getPropertyNames();
40+
for (String propertyName : propertyNames) {
41+
try {
42+
final String value = config.getProperty(propertyName);
43+
properties.put(propertyName, value);
44+
45+
}
46+
catch (CheckstyleException exception) {
47+
throw new IllegalStateException("Error getting property " + propertyName,
48+
exception);
49+
}
50+
}
51+
52+
final Configuration[] checkstyleChildren = config.getChildren();
53+
final CheckConfiguration[] simpleChildren =
54+
new CheckConfiguration[checkstyleChildren.length];
55+
for (int index = 0; index < checkstyleChildren.length; index++) {
56+
simpleChildren[index] = mapConfiguration(checkstyleChildren[index]);
57+
}
58+
59+
return new CheckConfiguration(config.getName(), properties, List.of(simpleChildren));
60+
}
61+
62+
public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath,
63+
String propFile) {
64+
Properties props = new Properties();
65+
66+
if (propFile == null) {
67+
props = System.getProperties();
68+
}
69+
else {
70+
try (FileInputStream input = new FileInputStream(propFile)) {
71+
props.load(input);
72+
}
73+
catch (IOException exception) {
74+
throw new IllegalStateException("Failed to read: " + propFile, exception);
75+
}
76+
}
77+
78+
final Configuration checkstyleConfig;
79+
try {
80+
checkstyleConfig = com.puppycrawl.tools.checkstyle.ConfigurationLoader
81+
.loadConfiguration(checkstyleConfigurationPath, new PropertiesExpander(props));
82+
}
83+
catch (CheckstyleException exception) {
84+
throw new IllegalStateException("Failed to load configuration:"
85+
+ checkstyleConfigurationPath, exception);
86+
}
87+
88+
return mapConfiguration(checkstyleConfig);
89+
}
90+
91+
}

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+
configurationPath: "https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${checkstyle.version}/config/checkstyle-checks.xml"

0 commit comments

Comments
 (0)