diff --git a/src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java b/src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java index 2daf412..48d4d3e 100644 --- a/src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java +++ b/src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java @@ -41,16 +41,40 @@ public String getName() { return name; } - public Map getProperties() { - return properties; + private CheckConfiguration getParent() { + return parent; } - public List getChildren() { - return children; + public String getProperty(String key) { + String value = null; + + if (properties.containsKey(key)) { + value = properties.get(key); + } + else if (getParent() != null) { + value = getParent().getProperty(key); + } + return value; } - public String getProperty(String key) { - return properties.get(key); + public String getPropertyOrDefault(String key, String defaultValue) { + String result = getProperty(key); + if (result == null) { + result = defaultValue; + } + return result; + } + + public boolean hasProperty(String key) { + boolean result = false; + + if (properties.containsKey(key)) { + result = true; + } + else if (getParent() != null) { + result = getParent().hasProperty(key); + } + return result; } public int[] getIntArray(String propertyName) { @@ -70,7 +94,7 @@ public int[] getIntArray(String propertyName) { return result; } - public CheckConfiguration getChild(String childName) { + public CheckConfiguration getChildConfig(String childName) { CheckConfiguration result = null; for (CheckConfiguration child : children) { if (childName.equals(child.getName())) { @@ -81,10 +105,6 @@ public CheckConfiguration getChild(String childName) { return result; } - public CheckConfiguration getParent() { - return parent; - } - private void setParent(CheckConfiguration parent) { this.parent = parent; } diff --git a/src/main/java/org/checkstyle/autofix/recipe/Header.java b/src/main/java/org/checkstyle/autofix/recipe/Header.java index 725e5ad..37c7ecc 100644 --- a/src/main/java/org/checkstyle/autofix/recipe/Header.java +++ b/src/main/java/org/checkstyle/autofix/recipe/Header.java @@ -28,6 +28,7 @@ import java.util.Set; import java.util.stream.Collectors; +import org.checkstyle.autofix.parser.CheckConfiguration; import org.checkstyle.autofix.parser.CheckstyleViolation; import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; @@ -38,9 +39,6 @@ import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.java.tree.Space; -import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import com.puppycrawl.tools.checkstyle.api.Configuration; - public class Header extends Recipe { private static final String HEADER_PROPERTY = "header"; private static final String HEADER_FILE_PROPERTY = "headerFile"; @@ -48,13 +46,11 @@ public class Header extends Recipe { private static final String CHARSET_PROPERTY = "charset"; private final List violations; - private final Configuration config; - private final Charset charset; + private final CheckConfiguration config; - public Header(List violations, Configuration config, Charset charset) { + public Header(List violations, CheckConfiguration config) { this.violations = violations; this.config = config; - this.charset = charset; } @Override @@ -69,60 +65,44 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - final String licenseHeader = extractLicenseHeader(config, charset); + final String licenseHeader = extractLicenseHeader(config); final List ignoreLines = extractIgnoreLines(config); return new HeaderVisitor(violations, licenseHeader, ignoreLines); } - private static String extractLicenseHeader(Configuration config, Charset charset) { + private static String extractLicenseHeader(CheckConfiguration config) { final String header; - try { - if (hasProperty(config, HEADER_PROPERTY)) { - header = config.getProperty(HEADER_PROPERTY); - } - else { - final Charset charsetToUse; - if (hasProperty(config, CHARSET_PROPERTY)) { - charsetToUse = Charset.forName(config.getProperty(CHARSET_PROPERTY)); - } - else { - charsetToUse = charset; - } - final String headerFilePath = config.getProperty(HEADER_FILE_PROPERTY); + if (config.hasProperty(HEADER_PROPERTY)) { + header = config.getProperty(HEADER_PROPERTY); + } + else { + final Charset charsetToUse = Charset.forName(config + .getPropertyOrDefault(CHARSET_PROPERTY, Charset.defaultCharset().name())); + final String headerFilePath = config.getProperty(HEADER_FILE_PROPERTY); + try { header = Files.readString(Path.of(headerFilePath), charsetToUse); } - } - catch (CheckstyleException | IOException exception) { - throw new IllegalArgumentException("Failed to extract header from config", exception); + catch (IOException exception) { + throw new IllegalArgumentException("Failed to extract header from config", + exception); + } } return header; } - private static List extractIgnoreLines(Configuration config) { + private static List extractIgnoreLines(CheckConfiguration config) { final List ignoreLinesList; - try { - if (!hasProperty(config, IGNORE_LINES_PROPERTY)) { - ignoreLinesList = new ArrayList<>(); - } - else { - final String ignoreLines = config.getProperty(IGNORE_LINES_PROPERTY); - ignoreLinesList = Arrays.stream(ignoreLines.split(",")) - .map(String::trim) - .map(Integer::parseInt) - .collect(Collectors.toList()); - } + if (config.hasProperty(IGNORE_LINES_PROPERTY)) { + ignoreLinesList = Arrays.stream(config.getIntArray(IGNORE_LINES_PROPERTY)) + .boxed() + .toList(); } - catch (CheckstyleException exception) { - throw new IllegalArgumentException( - "Failed to extract ignore lines from config", exception); + else { + ignoreLinesList = new ArrayList<>(); } return ignoreLinesList; } - private static boolean hasProperty(Configuration config, String propertyName) { - return Arrays.asList(config.getPropertyNames()).contains(propertyName); - } - private static class HeaderVisitor extends JavaIsoVisitor { private final List violations; private final String licenseHeader; @@ -146,7 +126,7 @@ public J visit(Tree tree, ExecutionContext ctx) { if (hasViolation(filePath)) { final String currentHeader = extractCurrentHeader(sourceFile); final String fixedHeader = fixHeaderLines(licenseHeader, - currentHeader, ignoreLines); + currentHeader, ignoreLines); sourceFile = sourceFile.withPrefix( Space.format(fixedHeader + System.lineSeparator())); diff --git a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java index 5d0da12..1c1b0b2 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java @@ -21,17 +21,14 @@ import static org.openrewrite.java.Assertions.java; import java.io.IOException; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; import org.checkstyle.autofix.InputClassRenamer; import org.openrewrite.Recipe; import org.openrewrite.test.RewriteTest; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import com.puppycrawl.tools.checkstyle.api.Configuration; public abstract class AbstractRecipeTest implements RewriteTest { @@ -67,33 +64,4 @@ protected void testRecipe(String recipePath, String testCaseName) throws IOExcep }); } - protected Configuration extractCheckConfiguration(Configuration config, String checkName) { - - return Arrays.stream(config.getChildren()) - .filter(child -> checkName.equals(child.getName())) - .findFirst() - .orElseThrow(() -> { - return new IllegalArgumentException(checkName + "configuration not " - + "found"); - }); - } - - protected Charset getCharset(Configuration config) { - try { - final String charsetName; - - if (Arrays.asList(config.getPropertyNames()).contains("charset")) { - charsetName = config.getProperty("charset"); - } - else { - charsetName = Charset.defaultCharset().name(); - } - - return Charset.forName(charsetName); - } - catch (CheckstyleException exception) { - throw new IllegalArgumentException("Failed to extract charset from config.", exception); - } - } - } diff --git a/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java b/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java index 6ea5d90..65eb986 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java @@ -20,17 +20,15 @@ import java.io.IOException; import java.nio.file.Path; import java.util.List; -import java.util.Properties; +import org.checkstyle.autofix.parser.CheckConfiguration; import org.checkstyle.autofix.parser.CheckstyleReportParser; import org.checkstyle.autofix.parser.CheckstyleViolation; +import org.checkstyle.autofix.parser.ConfigurationLoader; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; -import com.puppycrawl.tools.checkstyle.ConfigurationLoader; -import com.puppycrawl.tools.checkstyle.PropertiesExpander; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import com.puppycrawl.tools.checkstyle.api.Configuration; public class HeaderTest extends AbstractRecipeTest { @@ -42,15 +40,15 @@ protected Recipe getRecipe() throws CheckstyleException { final String configPath = "src/test/resources/org/checkstyle/autofix/recipe/header" + "/config.xml"; - final Configuration config = ConfigurationLoader.loadConfiguration( - configPath, new PropertiesExpander(new Properties()) - ); + final CheckConfiguration config = ConfigurationLoader.loadConfiguration(configPath, null); final List violations = CheckstyleReportParser.parse(Path.of(reportPath)); - return new Header(violations, - extractCheckConfiguration(config, "Header"), getCharset(config)); + final CheckConfiguration checkConfig = config + .getChildConfig("Header"); + + return new Header(violations, checkConfig); } @Test