Skip to content

Issue #50: Refactored the header recipe to use checkConfiguration #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,40 @@ public String getName() {
return name;
}

public Map<String, String> getProperties() {
return properties;
private CheckConfiguration getParent() {
return parent;
}

public List<CheckConfiguration> 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) {
Expand All @@ -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())) {
Expand All @@ -81,10 +105,6 @@ public CheckConfiguration getChild(String childName) {
return result;
}

public CheckConfiguration getParent() {
return parent;
}

private void setParent(CheckConfiguration parent) {
this.parent = parent;
}
Expand Down
70 changes: 25 additions & 45 deletions src/main/java/org/checkstyle/autofix/recipe/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,23 +39,18 @@
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";
private static final String IGNORE_LINES_PROPERTY = "ignoreLines";
private static final String CHARSET_PROPERTY = "charset";

private final List<CheckstyleViolation> violations;
private final Configuration config;
private final Charset charset;
private final CheckConfiguration config;

public Header(List<CheckstyleViolation> violations, Configuration config, Charset charset) {
public Header(List<CheckstyleViolation> violations, CheckConfiguration config) {
this.violations = violations;
this.config = config;
this.charset = charset;
}

@Override
Expand All @@ -69,60 +65,44 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
final String licenseHeader = extractLicenseHeader(config, charset);
final String licenseHeader = extractLicenseHeader(config);
final List<Integer> 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<Integer> extractIgnoreLines(Configuration config) {
private static List<Integer> extractIgnoreLines(CheckConfiguration config) {
final List<Integer> 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<ExecutionContext> {
private final List<CheckstyleViolation> violations;
private final String licenseHeader;
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}

}
16 changes: 7 additions & 9 deletions src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<CheckstyleViolation> 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
Expand Down
Loading