Skip to content

Commit 3928fcf

Browse files
committed
Issue #50: Register the Header in recipe registry
1 parent 9279400 commit 3928fcf

File tree

6 files changed

+68
-76
lines changed

6 files changed

+68
-76
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: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,33 @@
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.Optional;
26+
import java.util.function.BiFunction;
2427
import java.util.function.Function;
2528
import java.util.stream.Collectors;
2629

30+
import org.checkstyle.autofix.parser.CheckConfiguration;
2731
import org.checkstyle.autofix.parser.CheckstyleViolation;
32+
import org.checkstyle.autofix.recipe.Header;
2833
import org.checkstyle.autofix.recipe.UpperEll;
2934
import org.openrewrite.Recipe;
3035

3136
public final class CheckstyleRecipeRegistry {
3237

3338
private static final Map<String, Function<List<CheckstyleViolation>, Recipe>> RECIPE_MAP =
3439
new HashMap<>();
40+
private static final Map<String, BiFunction<List<CheckstyleViolation>, CheckConfiguration,
41+
Recipe>> RECIPE_MAP_WITH_CONFIG =
42+
new HashMap<>();
3543

3644
static {
37-
RECIPE_MAP.put("UpperEllCheck", UpperEll::new);
45+
RECIPE_MAP.put("UPPERELL", UpperEll::new);
46+
RECIPE_MAP_WITH_CONFIG.put("HEADER", Header::new);
3847
}
3948

4049
private CheckstyleRecipeRegistry() {
@@ -47,28 +56,49 @@ private CheckstyleRecipeRegistry() {
4756
* using the simple name of the check, and applies the factory to generate Recipe instances.
4857
*
4958
* @param violations the list of Checkstyle violations
59+
* @param config the checkstyle configuration
5060
* @return a list of generated Recipe objects
5161
*/
52-
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations) {
62+
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations,
63+
CheckConfiguration config) {
64+
return violations.stream()
65+
.collect(Collectors.groupingBy(CheckstyleViolation::getSource))
66+
.entrySet()
67+
.stream()
68+
.map(entry -> createRecipe(entry, config))
69+
.filter(Objects::nonNull)
70+
.collect(Collectors.toList());
71+
}
72+
73+
private static Recipe createRecipe(Map.Entry<String, List<CheckstyleViolation>> entry,
74+
CheckConfiguration config) {
5375

54-
final Map<String, List<CheckstyleViolation>> violationsByCheck = violations.stream()
55-
.collect(Collectors.groupingBy(CheckstyleViolation::getSource));
76+
final String simpleCheckName = normalizeCheckName(entry.getKey());
77+
final List<CheckstyleViolation> violations = entry.getValue();
5678

57-
final List<Recipe> recipes = new ArrayList<>();
79+
return Optional.ofNullable(RECIPE_MAP_WITH_CONFIG.get(simpleCheckName))
80+
.map(factory -> {
81+
return factory.apply(violations,
82+
extractCheckConfiguration(config, simpleCheckName));
83+
}).orElseGet(() -> {
84+
return Optional.ofNullable(RECIPE_MAP.get(simpleCheckName))
85+
.map(factory -> factory.apply(violations))
86+
.orElse(null);
87+
});
88+
}
5889

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();
90+
private static CheckConfiguration extractCheckConfiguration(CheckConfiguration config,
91+
String checkName) {
92+
return config.getChildConfig(checkName);
93+
}
6494

65-
final Function<List<CheckstyleViolation>, Recipe> recipeFactory =
66-
RECIPE_MAP.get(simpleCheckName);
67-
if (recipeFactory != null) {
68-
recipes.add(recipeFactory.apply(checkViolations));
69-
}
95+
private static String normalizeCheckName(String checkName) {
96+
String normalizedCheckName = checkName.substring(checkName.lastIndexOf('.') + 1);
97+
final int checkLength = 5;
98+
if (normalizedCheckName.toLowerCase().endsWith("check")) {
99+
normalizedCheckName = normalizedCheckName
100+
.substring(0, normalizedCheckName.length() - checkLength);
70101
}
71-
72-
return recipes;
102+
return normalizedCheckName.toUpperCase(Locale.ROOT);
73103
}
74104
}

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

Lines changed: 14 additions & 3 deletions
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

@@ -95,12 +99,19 @@ public int[] getIntArray(String propertyName) {
9599
}
96100

97101
public CheckConfiguration getChildConfig(String childName) {
102+
final List<CheckConfiguration> childrenList = getChildren();
98103
CheckConfiguration result = null;
99-
for (CheckConfiguration child : children) {
100-
if (childName.equals(child.getName())) {
101-
result = child;
104+
105+
int index = 0;
106+
while (index < childrenList.size()) {
107+
final CheckConfiguration current = childrenList.get(index);
108+
109+
if (childName.equals(current.getName())) {
110+
result = current;
102111
break;
103112
}
113+
childrenList.addAll(current.getChildren());
114+
index++;
104115
}
105116
return result;
106117
}

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

Lines changed: 3 additions & 1 deletion
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

@@ -56,7 +57,8 @@ public 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: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)