1717
1818package org .checkstyle .autofix ;
1919
20- import java .util .ArrayList ;
2120import java .util .HashMap ;
2221import java .util .List ;
22+ import java .util .Locale ;
2323import java .util .Map ;
24+ import java .util .Objects ;
25+ import java .util .Optional ;
26+ import java .util .function .BiFunction ;
2427import java .util .function .Function ;
2528import java .util .stream .Collectors ;
2629
30+ import org .checkstyle .autofix .parser .CheckConfiguration ;
2731import org .checkstyle .autofix .parser .CheckstyleViolation ;
32+ import org .checkstyle .autofix .recipe .Header ;
2833import org .checkstyle .autofix .recipe .UpperEll ;
2934import org .openrewrite .Recipe ;
3035
3136public 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}
0 commit comments