Skip to content

Commit 18e2ff7

Browse files
committed
Issue #50: Register the Header in recipe registry
1 parent faeab1d commit 18e2ff7

File tree

7 files changed

+91
-27
lines changed

7 files changed

+91
-27
lines changed

config/checkstyle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ checkstyle.regexp.header.file=config/regexp-header.txt
55
checkstyle.importcontrol.file=config/import-control.xml
66
checkstyle.importcontroltest.file=config/import-control-test.xml
77
checkstyle.java.version=17
8+
checkstyle.cache.file=.cache/checkstyle-cachefile

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ public String getPropertiesPath() {
7474
public List<Recipe> getRecipeList() {
7575
final List<CheckstyleViolation> violations = CheckstyleReportParser
7676
.parse(Path.of(getViolationReportPath()));
77+
final CheckConfiguration configuration = loadCheckstyleConfiguration();
7778

78-
return CheckstyleRecipeRegistry.getRecipes(violations);
79+
return CheckstyleRecipeRegistry.getRecipes(violations, configuration);
7980
}
8081

8182
private CheckConfiguration loadCheckstyleConfiguration() {

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

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,32 @@
1717

1818
package org.checkstyle.autofix;
1919

20-
import java.util.ArrayList;
2120
import java.util.HashMap;
2221
import java.util.List;
22+
import java.util.Locale;
2323
import java.util.Map;
24+
import java.util.Objects;
25+
import java.util.function.BiFunction;
2426
import java.util.function.Function;
2527
import java.util.stream.Collectors;
2628

29+
import org.checkstyle.autofix.parser.CheckConfiguration;
2730
import org.checkstyle.autofix.parser.CheckstyleViolation;
31+
import org.checkstyle.autofix.recipe.Header;
2832
import org.checkstyle.autofix.recipe.UpperEll;
2933
import org.openrewrite.Recipe;
3034

3135
public final class CheckstyleRecipeRegistry {
3236

3337
private static final Map<String, Function<List<CheckstyleViolation>, Recipe>> RECIPE_MAP =
3438
new HashMap<>();
39+
private static final Map<String, BiFunction<List<CheckstyleViolation>, CheckConfiguration,
40+
Recipe>> RECIPE_MAP_WITH_CONFIG =
41+
new HashMap<>();
3542

3643
static {
37-
RECIPE_MAP.put("UpperEllCheck", UpperEll::new);
44+
RECIPE_MAP.put("UPPERELL", UpperEll::new);
45+
RECIPE_MAP_WITH_CONFIG.put("HEADER", Header::new);
3846
}
3947

4048
private CheckstyleRecipeRegistry() {
@@ -47,28 +55,55 @@ private CheckstyleRecipeRegistry() {
4755
* using the simple name of the check, and applies the factory to generate Recipe instances.
4856
*
4957
* @param violations the list of Checkstyle violations
58+
* @param config the checkstyle configuration
5059
* @return a list of generated Recipe objects
5160
*/
52-
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations) {
53-
54-
final Map<String, List<CheckstyleViolation>> violationsByCheck = violations.stream()
55-
.collect(Collectors.groupingBy(CheckstyleViolation::getSource));
56-
57-
final List<Recipe> recipes = new ArrayList<>();
61+
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations,
62+
CheckConfiguration config) {
63+
return violations.stream()
64+
.collect(Collectors.groupingBy(CheckstyleViolation::getSource))
65+
.entrySet()
66+
.stream()
67+
.map(entry -> createRecipe(entry, config))
68+
.filter(Objects::nonNull)
69+
.collect(Collectors.toList());
70+
}
5871

59-
for (Map.Entry<String, List<CheckstyleViolation>> entry : violationsByCheck.entrySet()) {
60-
final String checkName = entry.getKey();
61-
final String simpleCheckName = checkName
62-
.substring(checkName.lastIndexOf('.') + 1);
63-
final List<CheckstyleViolation> checkViolations = entry.getValue();
72+
private static Recipe createRecipe(Map.Entry<String, List<CheckstyleViolation>> entry,
73+
CheckConfiguration config) {
74+
final String simpleCheckName = normalizeCheckName(entry.getKey());
75+
final List<CheckstyleViolation> violations = entry.getValue();
6476

65-
final Function<List<CheckstyleViolation>, Recipe> recipeFactory =
77+
Recipe recipe = null;
78+
final BiFunction<List<CheckstyleViolation>, CheckConfiguration, Recipe> configFactory =
79+
RECIPE_MAP_WITH_CONFIG.get(simpleCheckName);
80+
if (configFactory != null) {
81+
recipe = configFactory.apply(violations,
82+
extractCheckConfiguration(config, simpleCheckName));
83+
}
84+
else {
85+
final Function<List<CheckstyleViolation>, Recipe> factory =
6686
RECIPE_MAP.get(simpleCheckName);
67-
if (recipeFactory != null) {
68-
recipes.add(recipeFactory.apply(checkViolations));
87+
if (factory != null) {
88+
recipe = factory.apply(violations);
6989
}
7090
}
7191

72-
return recipes;
92+
return recipe;
93+
}
94+
95+
private static CheckConfiguration extractCheckConfiguration(CheckConfiguration config,
96+
String checkName) {
97+
return config.getChildConfig(checkName);
98+
}
99+
100+
private static String normalizeCheckName(String checkName) {
101+
String normalizedCheckName = checkName.substring(checkName.lastIndexOf('.') + 1);
102+
final int checkLength = 5;
103+
if (normalizedCheckName.toLowerCase().endsWith("check")) {
104+
normalizedCheckName = normalizedCheckName
105+
.substring(0, normalizedCheckName.length() - checkLength);
106+
}
107+
return normalizedCheckName.toUpperCase(Locale.ROOT);
73108
}
74109
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ private CheckConfiguration getParent() {
4545
return parent;
4646
}
4747

48+
private List<CheckConfiguration> getChildren() {
49+
return children;
50+
}
51+
4852
public String getProperty(String key) {
4953
String value = null;
5054

@@ -96,11 +100,15 @@ public int[] getIntArray(String propertyName) {
96100

97101
public CheckConfiguration getChildConfig(String childName) {
98102
CheckConfiguration result = null;
99-
for (CheckConfiguration child : children) {
103+
for (CheckConfiguration child : getChildren()) {
100104
if (childName.equals(child.getName())) {
101105
result = child;
102106
break;
103107
}
108+
result = child.getChildConfig(childName);
109+
if (result != null) {
110+
break;
111+
}
104112
}
105113
return result;
106114
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.util.HashMap;
2323
import java.util.List;
24+
import java.util.Locale;
2425
import java.util.Map;
2526
import java.util.Properties;
2627

@@ -34,7 +35,7 @@ private ConfigurationLoader() {
3435
// utility class
3536
}
3637

37-
private static CheckConfiguration mapConfiguration(Configuration config) {
38+
public static CheckConfiguration mapConfiguration(Configuration config) {
3839
final Map<String, String> properties = new HashMap<>();
3940
final String[] propertyNames = config.getPropertyNames();
4041
for (String propertyName : propertyNames) {
@@ -56,7 +57,8 @@ private static CheckConfiguration mapConfiguration(Configuration config) {
5657
simpleChildren[index] = mapConfiguration(checkstyleChildren[index]);
5758
}
5859

59-
return new CheckConfiguration(config.getName(), properties, List.of(simpleChildren));
60+
return new CheckConfiguration(config.getName().toUpperCase(Locale.ROOT),
61+
properties, List.of(simpleChildren));
6062
}
6163

6264
public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath,

src/test/java/org/checkstyle/autofix/CheckstyleRecipeRegistryTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@
2222
import java.util.List;
2323

2424
import org.checkstyle.autofix.parser.CheckstyleViolation;
25+
import org.checkstyle.autofix.parser.ConfigurationLoader;
2526
import org.junit.jupiter.api.Test;
2627
import org.openrewrite.Recipe;
2728

28-
public class CheckstyleRecipeRegistryTest {
29+
import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport;
30+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31+
import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck;
32+
33+
public class CheckstyleRecipeRegistryTest extends AbstractXmlTestSupport {
34+
35+
@Override
36+
protected String getPackageLocation() {
37+
return "org/checkstyle/autofix/";
38+
}
2939

3040
@Test
3141
void testGetRecipesReturnsCorrectRecipe() {
@@ -40,14 +50,21 @@ void testGetRecipesReturnsCorrectRecipe() {
4050
"Use uppercase 'L' for long literals.", "Example2.java"),
4151

4252
new CheckstyleViolation(8, 12, "error",
43-
"com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck",
53+
"header",
4454
"Variable should be declared final.", "Example3.java")
4555
);
4656

47-
final List<Recipe> recipes = CheckstyleRecipeRegistry.getRecipes(violations);
57+
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
58+
final String headerPath = "src/test/resources/org/checkstyle/autofix/"
59+
+ "recipe/header/header.txt";
60+
checkConfig.addProperty("headerFile", headerPath);
61+
62+
final List<Recipe> recipes = CheckstyleRecipeRegistry.getRecipes(violations,
63+
ConfigurationLoader.mapConfiguration(checkConfig));
4864

49-
assertEquals(1, recipes.size(), "Should return one recipe");
50-
assertEquals("UpperEll recipe", recipes.get(0).getDisplayName());
65+
assertEquals(2, recipes.size(), "Should return one recipe");
66+
assertEquals("Header recipe", recipes.get(0).getDisplayName());
67+
assertEquals("UpperEll recipe", recipes.get(1).getDisplayName());
5168
}
5269

5370
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected Recipe getRecipe() throws CheckstyleException {
4646
CheckstyleReportParser.parse(Path.of(reportPath));
4747

4848
final CheckConfiguration checkConfig = config
49-
.getChildConfig("Header");
49+
.getChildConfig("HEADER");
5050

5151
return new Header(violations, checkConfig);
5252
}

0 commit comments

Comments
 (0)