Skip to content

Commit f223f4a

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue #50: Register the Header in recipe registry
1 parent 4abc31b commit f223f4a

File tree

7 files changed

+117
-79
lines changed

7 files changed

+117
-79
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import java.util.Arrays;
21+
import java.util.Optional;
22+
23+
public enum CheckstyleCheck {
24+
UPPERELL("com.puppycrawl.tools.checkstyle.checks.UpperEllCheck"),
25+
HEADER("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
26+
27+
private final String id;
28+
29+
CheckstyleCheck(String id) {
30+
this.id = id;
31+
}
32+
33+
public String getId() {
34+
return id;
35+
}
36+
37+
public static Optional<CheckstyleCheck> fromSource(String source) {
38+
return Arrays.stream(values())
39+
.filter(check -> check.getId().equals(source))
40+
.findFirst();
41+
42+
}
43+
44+
}

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

Lines changed: 51 additions & 19 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;
21-
import java.util.HashMap;
20+
import java.util.EnumMap;
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

33-
private static final Map<String, Function<List<CheckstyleViolation>, Recipe>> RECIPE_MAP =
34-
new HashMap<>();
37+
private static final EnumMap<CheckstyleCheck, Function<List<CheckstyleViolation>,
38+
Recipe>> RECIPE_MAP = new EnumMap<>(CheckstyleCheck.class);
39+
40+
private static final EnumMap<CheckstyleCheck, BiFunction<List<CheckstyleViolation>,
41+
CheckConfiguration, Recipe>> RECIPE_MAP_WITH_CONFIG =
42+
new EnumMap<>(CheckstyleCheck.class);
3543

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

4049
private CheckstyleRecipeRegistry() {
@@ -47,28 +56,51 @@ 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) {
75+
76+
Recipe recipe = null;
5377

54-
final Map<String, List<CheckstyleViolation>> violationsByCheck = violations.stream()
55-
.collect(Collectors.groupingBy(CheckstyleViolation::getSource));
78+
final Optional<CheckstyleCheck> check = CheckstyleCheck.fromSource(entry.getKey());
5679

57-
final List<Recipe> recipes = new ArrayList<>();
80+
if (check.isPresent()) {
5881

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();
82+
final CheckstyleCheck checkstyleCheck = check.get();
83+
final List<CheckstyleViolation> violations = entry.getValue();
6484

65-
final Function<List<CheckstyleViolation>, Recipe> recipeFactory =
66-
RECIPE_MAP.get(simpleCheckName);
67-
if (recipeFactory != null) {
68-
recipes.add(recipeFactory.apply(checkViolations));
85+
final BiFunction<List<CheckstyleViolation>, CheckConfiguration,
86+
Recipe> configRecipeFactory = RECIPE_MAP_WITH_CONFIG.get(checkstyleCheck);
87+
88+
if (configRecipeFactory == null) {
89+
final Function<List<CheckstyleViolation>, Recipe> simpleRecipeFactory =
90+
RECIPE_MAP.get(checkstyleCheck);
91+
recipe = simpleRecipeFactory.apply(violations);
92+
}
93+
else {
94+
final CheckConfiguration subConfig =
95+
extractCheckConfiguration(config, checkstyleCheck.name());
96+
recipe = configRecipeFactory.apply(violations, subConfig);
6997
}
7098
}
99+
return recipe;
100+
}
71101

72-
return recipes;
102+
private static CheckConfiguration extractCheckConfiguration(CheckConfiguration config,
103+
String checkName) {
104+
return config.getConfig(checkName);
73105
}
74106
}

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

Lines changed: 16 additions & 5 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

@@ -94,12 +98,19 @@ public int[] getIntArray(String propertyName) {
9498
return result;
9599
}
96100

97-
public CheckConfiguration getChildConfig(String childName) {
101+
public CheckConfiguration getConfig(String childName) {
98102
CheckConfiguration result = null;
99-
for (CheckConfiguration child : children) {
100-
if (childName.equals(child.getName())) {
101-
result = child;
102-
break;
103+
if (name.equals(childName)) {
104+
result = this;
105+
}
106+
else {
107+
final List<CheckConfiguration> childrenList = getChildren();
108+
for (final CheckConfiguration current : childrenList) {
109+
if (childName.equals(current.getName())) {
110+
result = current;
111+
break;
112+
}
113+
childrenList.addAll(current.getChildren());
103114
}
104115
}
105116
return result;

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)