Skip to content

Commit e2c6a35

Browse files
committed
changes
1 parent 06f3e1c commit e2c6a35

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@
1717

1818
package org.checkstyle.autofix;
1919

20-
import java.io.FileInputStream;
21-
import java.io.FileNotFoundException;
22-
import java.io.IOException;
2320
import java.nio.file.Path;
2421
import java.util.List;
25-
import java.util.Properties;
2622

2723
import org.checkstyle.autofix.parser.CheckConfiguration;
2824
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2925
import org.checkstyle.autofix.parser.CheckstyleViolation;
30-
import org.checkstyle.autofix.parser.ConfigurationMapper;
26+
import org.checkstyle.autofix.parser.ConfigurationLoader;
3127
import org.openrewrite.Option;
3228
import org.openrewrite.Recipe;
3329

34-
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
35-
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
36-
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37-
3830
/**
3931
* Main recipe that automatically fixes all supported Checkstyle violations.
4032
*/
@@ -86,23 +78,7 @@ public List<Recipe> getRecipeList() {
8678
return CheckstyleRecipeRegistry.getRecipes(violations);
8779
}
8880

89-
private CheckConfiguration 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-
getConfigurationPath(), new PropertiesExpander(props)));
81+
private CheckConfiguration loadCheckstyleConfiguration() {
82+
return ConfigurationLoader.loadConfiguration(getConfigurationPath(), getPropertiesPath());
10783
}
10884
}

src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
package org.checkstyle.autofix.parser;
1919

20+
import java.util.List;
2021
import java.util.Map;
2122

2223
public final class CheckConfiguration {
2324
private final Map<String, String> properties;
24-
private final CheckConfiguration[] children;
25+
private final List<CheckConfiguration> children;
2526

26-
public CheckConfiguration(Map<String, String> properties, CheckConfiguration[] children) {
27+
public CheckConfiguration(Map<String, String> properties, List<CheckConfiguration> children) {
2728
this.properties = properties;
2829
this.children = children;
2930
}
@@ -32,7 +33,7 @@ public Map<String, String> getProperties() {
3233
return properties;
3334
}
3435

35-
public CheckConfiguration[] getChildren() {
36+
public List<CheckConfiguration> getChildren() {
3637
return children;
3738
}
3839

src/main/java/org/checkstyle/autofix/parser/ConfigurationMapper.java renamed to src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717

1818
package org.checkstyle.autofix.parser;
1919

20+
import java.io.FileInputStream;
21+
import java.io.IOException;
2022
import java.util.HashMap;
23+
import java.util.List;
2124
import java.util.Map;
25+
import java.util.Properties;
2226

27+
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
2328
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
2429
import com.puppycrawl.tools.checkstyle.api.Configuration;
2530

26-
public final class ConfigurationMapper {
31+
public final class ConfigurationLoader {
2732

28-
private ConfigurationMapper() {
33+
private ConfigurationLoader() {
2934
// utility class
3035
}
3136

@@ -39,8 +44,8 @@ public static CheckConfiguration mapConfiguration(Configuration config) {
3944

4045
}
4146
catch (CheckstyleException exception) {
42-
throw new IllegalArgumentException("Error getting property "
43-
+ propertyName + ": " + exception.getMessage());
47+
throw new IllegalStateException("Error getting property "
48+
+ propertyName + ": " + exception);
4449
}
4550
}
4651

@@ -51,6 +56,36 @@ public static CheckConfiguration mapConfiguration(Configuration config) {
5156
simpleChildren[index] = mapConfiguration(checkstyleChildren[index]);
5257
}
5358

54-
return new CheckConfiguration(properties, simpleChildren);
59+
return new CheckConfiguration(properties, List.of(simpleChildren));
5560
}
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+
5691
}

0 commit comments

Comments
 (0)