Skip to content

Commit 86d6fd4

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue #36: Updated CheckstyleAutoFix to load checkstyle-config.
1 parent 51ba40b commit 86d6fd4

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

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

Lines changed: 15 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,11 @@ 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+
3845
@Override
3946
public String getDisplayName() {
4047
return "Checkstyle autoFix";
@@ -49,12 +56,19 @@ public String getViolationReportPath() {
4956
return violationReportPath;
5057
}
5158

59+
public String getConfigurationPath() {
60+
return configurationPath;
61+
}
62+
5263
@Override
5364
public List<Recipe> getRecipeList() {
54-
5565
final List<CheckstyleViolation> violations = CheckstyleReportParser
5666
.parse(Path.of(getViolationReportPath()));
5767

5868
return CheckstyleRecipeRegistry.getRecipes(violations);
5969
}
70+
71+
private CheckConfiguration loadCheckstyleConfiguration() {
72+
return ConfigurationLoader.loadConfiguration(getConfigurationPath());
73+
}
6074
}
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.List;
22+
import java.util.Map;
23+
import java.util.Properties;
24+
25+
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
26+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
27+
import com.puppycrawl.tools.checkstyle.api.Configuration;
28+
29+
public final class ConfigurationLoader {
30+
31+
private ConfigurationLoader() {
32+
// utility class
33+
}
34+
35+
private static CheckConfiguration mapConfiguration(Configuration config) {
36+
final Map<String, String> properties = new HashMap<>();
37+
final String[] propertyNames = config.getPropertyNames();
38+
for (String propertyName : propertyNames) {
39+
try {
40+
final String value = config.getProperty(propertyName);
41+
properties.put(propertyName, value);
42+
43+
}
44+
catch (CheckstyleException exception) {
45+
throw new IllegalStateException("Error getting property " + propertyName,
46+
exception);
47+
}
48+
}
49+
50+
final Configuration[] checkstyleChildren = config.getChildren();
51+
final CheckConfiguration[] simpleChildren =
52+
new CheckConfiguration[checkstyleChildren.length];
53+
for (int index = 0; index < checkstyleChildren.length; index++) {
54+
simpleChildren[index] = mapConfiguration(checkstyleChildren[index]);
55+
}
56+
57+
return new CheckConfiguration(config.getName(), properties, List.of(simpleChildren));
58+
}
59+
60+
public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath) {
61+
62+
final Configuration checkstyleConfig;
63+
try {
64+
checkstyleConfig = com.puppycrawl.tools.checkstyle.ConfigurationLoader
65+
.loadConfiguration(checkstyleConfigurationPath,
66+
new PropertiesExpander(new Properties()));
67+
}
68+
catch (CheckstyleException exception) {
69+
throw new IllegalStateException("Failed to load configuration:"
70+
+ checkstyleConfigurationPath, exception);
71+
}
72+
73+
return mapConfiguration(checkstyleConfig);
74+
}
75+
76+
}

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)