Skip to content

Commit 37b3032

Browse files
committed
Issue #50: Refactored the header recipe to use checkConfiguration
1 parent a9615dc commit 37b3032

File tree

4 files changed

+63
-97
lines changed

4 files changed

+63
-97
lines changed

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,40 @@ public String getName() {
4141
return name;
4242
}
4343

44-
public Map<String, String> getProperties() {
45-
return properties;
44+
private CheckConfiguration getParent() {
45+
return parent;
4646
}
4747

48-
public List<CheckConfiguration> getChildren() {
49-
return children;
48+
public String getProperty(String key) {
49+
String value = null;
50+
51+
if (properties.containsKey(key)) {
52+
value = properties.get(key);
53+
}
54+
else if (getParent() != null) {
55+
value = getParent().getProperty(key);
56+
}
57+
return value;
5058
}
5159

52-
public String getProperty(String key) {
53-
return properties.get(key);
60+
public String getPropertyOrDefault(String key, String defaultValue) {
61+
String result = getProperty(key);
62+
if (result == null) {
63+
result = defaultValue;
64+
}
65+
return result;
66+
}
67+
68+
public boolean hasProperty(String key) {
69+
boolean result = false;
70+
71+
if (properties.containsKey(key)) {
72+
result = true;
73+
}
74+
else if (getParent() != null) {
75+
result = getParent().hasProperty(key);
76+
}
77+
return result;
5478
}
5579

5680
public int[] getIntArray(String propertyName) {
@@ -70,7 +94,7 @@ public int[] getIntArray(String propertyName) {
7094
return result;
7195
}
7296

73-
public CheckConfiguration getChild(String childName) {
97+
public CheckConfiguration getChildConfig(String childName) {
7498
CheckConfiguration result = null;
7599
for (CheckConfiguration child : children) {
76100
if (childName.equals(child.getName())) {
@@ -81,10 +105,6 @@ public CheckConfiguration getChild(String childName) {
81105
return result;
82106
}
83107

84-
public CheckConfiguration getParent() {
85-
return parent;
86-
}
87-
88108
private void setParent(CheckConfiguration parent) {
89109
this.parent = parent;
90110
}

src/main/java/org/checkstyle/autofix/recipe/Header.java

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Set;
2929
import java.util.stream.Collectors;
3030

31+
import org.checkstyle.autofix.parser.CheckConfiguration;
3132
import org.checkstyle.autofix.parser.CheckstyleViolation;
3233
import org.openrewrite.ExecutionContext;
3334
import org.openrewrite.Recipe;
@@ -38,23 +39,18 @@
3839
import org.openrewrite.java.tree.JavaSourceFile;
3940
import org.openrewrite.java.tree.Space;
4041

41-
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
42-
import com.puppycrawl.tools.checkstyle.api.Configuration;
43-
4442
public class Header extends Recipe {
4543
private static final String HEADER_PROPERTY = "header";
4644
private static final String HEADER_FILE_PROPERTY = "headerFile";
4745
private static final String IGNORE_LINES_PROPERTY = "ignoreLines";
4846
private static final String CHARSET_PROPERTY = "charset";
4947

5048
private final List<CheckstyleViolation> violations;
51-
private final Configuration config;
52-
private final Charset charset;
49+
private final CheckConfiguration config;
5350

54-
public Header(List<CheckstyleViolation> violations, Configuration config, Charset charset) {
51+
public Header(List<CheckstyleViolation> violations, CheckConfiguration config) {
5552
this.violations = violations;
5653
this.config = config;
57-
this.charset = charset;
5854
}
5955

6056
@Override
@@ -69,60 +65,44 @@ public String getDescription() {
6965

7066
@Override
7167
public TreeVisitor<?, ExecutionContext> getVisitor() {
72-
final String licenseHeader = extractLicenseHeader(config, charset);
68+
final String licenseHeader = extractLicenseHeader(config);
7369
final List<Integer> ignoreLines = extractIgnoreLines(config);
7470
return new HeaderVisitor(violations, licenseHeader, ignoreLines);
7571
}
7672

77-
private static String extractLicenseHeader(Configuration config, Charset charset) {
73+
private static String extractLicenseHeader(CheckConfiguration config) {
7874
final String header;
79-
try {
80-
if (hasProperty(config, HEADER_PROPERTY)) {
81-
header = config.getProperty(HEADER_PROPERTY);
82-
}
83-
else {
84-
final Charset charsetToUse;
85-
if (hasProperty(config, CHARSET_PROPERTY)) {
86-
charsetToUse = Charset.forName(config.getProperty(CHARSET_PROPERTY));
87-
}
88-
else {
89-
charsetToUse = charset;
90-
}
91-
final String headerFilePath = config.getProperty(HEADER_FILE_PROPERTY);
75+
if (config.hasProperty(HEADER_PROPERTY)) {
76+
header = config.getProperty(HEADER_PROPERTY);
77+
}
78+
else {
79+
final Charset charsetToUse = Charset.forName(config
80+
.getPropertyOrDefault(CHARSET_PROPERTY, Charset.defaultCharset().name()));
81+
final String headerFilePath = config.getProperty(HEADER_FILE_PROPERTY);
82+
try {
9283
header = Files.readString(Path.of(headerFilePath), charsetToUse);
9384
}
94-
}
95-
catch (CheckstyleException | IOException exception) {
96-
throw new IllegalArgumentException("Failed to extract header from config", exception);
85+
catch (IOException exception) {
86+
throw new IllegalArgumentException("Failed to extract header from config",
87+
exception);
88+
}
9789
}
9890
return header;
9991
}
10092

101-
private static List<Integer> extractIgnoreLines(Configuration config) {
93+
private static List<Integer> extractIgnoreLines(CheckConfiguration config) {
10294
final List<Integer> ignoreLinesList;
103-
try {
104-
if (!hasProperty(config, IGNORE_LINES_PROPERTY)) {
105-
ignoreLinesList = new ArrayList<>();
106-
}
107-
else {
108-
final String ignoreLines = config.getProperty(IGNORE_LINES_PROPERTY);
109-
ignoreLinesList = Arrays.stream(ignoreLines.split(","))
110-
.map(String::trim)
111-
.map(Integer::parseInt)
112-
.collect(Collectors.toList());
113-
}
95+
if (config.hasProperty(IGNORE_LINES_PROPERTY)) {
96+
ignoreLinesList = Arrays.stream(config.getIntArray(IGNORE_LINES_PROPERTY))
97+
.boxed()
98+
.toList();
11499
}
115-
catch (CheckstyleException exception) {
116-
throw new IllegalArgumentException(
117-
"Failed to extract ignore lines from config", exception);
100+
else {
101+
ignoreLinesList = new ArrayList<>();
118102
}
119103
return ignoreLinesList;
120104
}
121105

122-
private static boolean hasProperty(Configuration config, String propertyName) {
123-
return Arrays.asList(config.getPropertyNames()).contains(propertyName);
124-
}
125-
126106
private static class HeaderVisitor extends JavaIsoVisitor<ExecutionContext> {
127107
private final List<CheckstyleViolation> violations;
128108
private final String licenseHeader;
@@ -146,7 +126,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
146126
if (hasViolation(filePath)) {
147127
final String currentHeader = extractCurrentHeader(sourceFile);
148128
final String fixedHeader = fixHeaderLines(licenseHeader,
149-
currentHeader, ignoreLines);
129+
currentHeader, ignoreLines);
150130

151131
sourceFile = sourceFile.withPrefix(
152132
Space.format(fixedHeader + System.lineSeparator()));

src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@
2121
import static org.openrewrite.java.Assertions.java;
2222

2323
import java.io.IOException;
24-
import java.nio.charset.Charset;
2524
import java.nio.file.Files;
2625
import java.nio.file.Paths;
27-
import java.util.Arrays;
2826

2927
import org.checkstyle.autofix.InputClassRenamer;
3028
import org.openrewrite.Recipe;
3129
import org.openrewrite.test.RewriteTest;
3230

3331
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
34-
import com.puppycrawl.tools.checkstyle.api.Configuration;
3532

3633
public abstract class AbstractRecipeTest implements RewriteTest {
3734

@@ -67,33 +64,4 @@ protected void testRecipe(String recipePath, String testCaseName) throws IOExcep
6764
});
6865
}
6966

70-
protected Configuration extractCheckConfiguration(Configuration config, String checkName) {
71-
72-
return Arrays.stream(config.getChildren())
73-
.filter(child -> checkName.equals(child.getName()))
74-
.findFirst()
75-
.orElseThrow(() -> {
76-
return new IllegalArgumentException(checkName + "configuration not "
77-
+ "found");
78-
});
79-
}
80-
81-
protected Charset getCharset(Configuration config) {
82-
try {
83-
final String charsetName;
84-
85-
if (Arrays.asList(config.getPropertyNames()).contains("charset")) {
86-
charsetName = config.getProperty("charset");
87-
}
88-
else {
89-
charsetName = Charset.defaultCharset().name();
90-
}
91-
92-
return Charset.forName(charsetName);
93-
}
94-
catch (CheckstyleException exception) {
95-
throw new IllegalArgumentException("Failed to extract charset from config.", exception);
96-
}
97-
}
98-
9967
}

src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020
import java.io.IOException;
2121
import java.nio.file.Path;
2222
import java.util.List;
23-
import java.util.Properties;
2423

24+
import org.checkstyle.autofix.parser.CheckConfiguration;
2525
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2626
import org.checkstyle.autofix.parser.CheckstyleViolation;
27+
import org.checkstyle.autofix.parser.ConfigurationLoader;
2728
import org.junit.jupiter.api.Test;
2829
import org.openrewrite.Recipe;
2930

30-
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
31-
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
3231
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
33-
import com.puppycrawl.tools.checkstyle.api.Configuration;
3432

3533
public class HeaderTest extends AbstractRecipeTest {
3634

@@ -42,15 +40,15 @@ protected Recipe getRecipe() throws CheckstyleException {
4240
final String configPath = "src/test/resources/org/checkstyle/autofix/recipe/header"
4341
+ "/config.xml";
4442

45-
final Configuration config = ConfigurationLoader.loadConfiguration(
46-
configPath, new PropertiesExpander(new Properties())
47-
);
43+
final CheckConfiguration config = ConfigurationLoader.loadConfiguration(configPath, null);
4844

4945
final List<CheckstyleViolation> violations =
5046
CheckstyleReportParser.parse(Path.of(reportPath));
5147

52-
return new Header(violations,
53-
extractCheckConfiguration(config, "Header"), getCharset(config));
48+
final CheckConfiguration checkConfig = config
49+
.getChildConfig("Header");
50+
51+
return new Header(violations, checkConfig);
5452
}
5553

5654
@Test

0 commit comments

Comments
 (0)