Skip to content

Commit 10788b2

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

File tree

7 files changed

+101
-77
lines changed

7 files changed

+101
-77
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() {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
3+
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
///////////////////////////////////////////////////////////////////////////////////////////////
17+
18+
package org.checkstyle.autofix;
19+
20+
public enum CheckstyleCheck {
21+
UPPERELL("com.puppycrawl.tools.checkstyle.checks.UpperEllCheck"),
22+
HEADER("header");
23+
24+
private final String id;
25+
26+
CheckstyleCheck(String id) {
27+
this.id = id;
28+
}
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public static String fromSource(String source) {
35+
String result = null;
36+
for (CheckstyleCheck check : values()) {
37+
if (check.getId().equals(source)) {
38+
result = check.name();
39+
}
40+
}
41+
return result;
42+
}
43+
}

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

Lines changed: 38 additions & 19 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;
2322
import java.util.Map;
23+
import java.util.Objects;
24+
import java.util.Optional;
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,39 @@ 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));
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+
}
5671

57-
final List<Recipe> recipes = new ArrayList<>();
72+
private static Recipe createRecipe(Map.Entry<String, List<CheckstyleViolation>> entry,
73+
CheckConfiguration config) {
5874

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();
75+
final String checkstyleCheck = CheckstyleCheck.fromSource(entry.getKey());
76+
final List<CheckstyleViolation> violations = entry.getValue();
6477

65-
final Function<List<CheckstyleViolation>, Recipe> recipeFactory =
66-
RECIPE_MAP.get(simpleCheckName);
67-
if (recipeFactory != null) {
68-
recipes.add(recipeFactory.apply(checkViolations));
69-
}
70-
}
78+
return Optional.ofNullable(RECIPE_MAP_WITH_CONFIG.get(checkstyleCheck))
79+
.map(factory -> {
80+
return factory.apply(violations,
81+
extractCheckConfiguration(config, checkstyleCheck));
82+
}).orElseGet(() -> {
83+
return Optional.ofNullable(RECIPE_MAP.get(checkstyleCheck))
84+
.map(factory -> factory.apply(violations))
85+
.orElse(null);
86+
});
87+
}
7188

72-
return recipes;
89+
private static CheckConfiguration extractCheckConfiguration(CheckConfiguration config,
90+
String checkName) {
91+
return config.getChildConfig(checkName);
7392
}
7493
}

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)