Skip to content

Commit 3d5cb1a

Browse files
committed
Issue #50: Refactored the header recipe to use checkConfiguration
1 parent 86d6fd4 commit 3d5cb1a

File tree

4 files changed

+32
-74
lines changed

4 files changed

+32
-74
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public String getProperty(String key) {
5353
return properties.get(key);
5454
}
5555

56+
public boolean hasProperty(String key) {
57+
return properties.containsKey(key);
58+
}
59+
5660
public int[] getIntArray(String propertyName) {
5761
final String value = properties.get(propertyName);
5862
final int[] result;

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

Lines changed: 22 additions & 33 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,53 @@ 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;
7975
try {
80-
if (hasProperty(config, HEADER_PROPERTY)) {
76+
if (config.hasProperty(HEADER_PROPERTY)) {
8177
header = config.getProperty(HEADER_PROPERTY);
8278
}
8379
else {
8480
final Charset charsetToUse;
85-
if (hasProperty(config, CHARSET_PROPERTY)) {
81+
if (config.hasProperty(CHARSET_PROPERTY)) {
8682
charsetToUse = Charset.forName(config.getProperty(CHARSET_PROPERTY));
8783
}
84+
else if (config.getParent().hasProperty(CHARSET_PROPERTY)) {
85+
charsetToUse = Charset.forName(config
86+
.getParent().getProperty(CHARSET_PROPERTY));
87+
}
8888
else {
89-
charsetToUse = charset;
89+
charsetToUse = Charset.defaultCharset();
9090
}
91+
9192
final String headerFilePath = config.getProperty(HEADER_FILE_PROPERTY);
9293
header = Files.readString(Path.of(headerFilePath), charsetToUse);
9394
}
9495
}
95-
catch (CheckstyleException | IOException exception) {
96+
catch (IOException exception) {
9697
throw new IllegalArgumentException("Failed to extract header from config", exception);
9798
}
9899
return header;
99100
}
100101

101-
private static List<Integer> extractIgnoreLines(Configuration config) {
102+
private static List<Integer> extractIgnoreLines(CheckConfiguration config) {
102103
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-
}
104+
if (!config.hasProperty(IGNORE_LINES_PROPERTY)) {
105+
ignoreLinesList = new ArrayList<>();
114106
}
115-
catch (CheckstyleException exception) {
116-
throw new IllegalArgumentException(
117-
"Failed to extract ignore lines from config", exception);
107+
else {
108+
ignoreLinesList = Arrays.stream(config.getIntArray(IGNORE_LINES_PROPERTY))
109+
.boxed()
110+
.toList();
118111
}
119112
return ignoreLinesList;
120113
}
121114

122-
private static boolean hasProperty(Configuration config, String propertyName) {
123-
return Arrays.asList(config.getPropertyNames()).contains(propertyName);
124-
}
125-
126115
private static class HeaderVisitor extends JavaIsoVisitor<ExecutionContext> {
127116
private final List<CheckstyleViolation> violations;
128117
private final String licenseHeader;
@@ -146,7 +135,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
146135
if (hasViolation(filePath)) {
147136
final String currentHeader = extractCurrentHeader(sourceFile);
148137
final String fixedHeader = fixHeaderLines(licenseHeader,
149-
currentHeader, ignoreLines);
138+
currentHeader, ignoreLines);
150139

151140
sourceFile = sourceFile.withPrefix(
152141
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: 6 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,14 @@ 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);
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.getChild("Header");
49+
50+
return new Header(violations, checkConfig);
5451
}
5552

5653
@Test

0 commit comments

Comments
 (0)