1717
1818package org .checkstyle .autofix ;
1919
20- import java .util .ArrayList ;
21- import java .util .HashMap ;
20+ import java .util .EnumMap ;
2221import java .util .List ;
2322import java .util .Map ;
23+ import java .util .Objects ;
24+ import java .util .Optional ;
25+ import java .util .function .BiFunction ;
2426import java .util .function .Function ;
2527import java .util .stream .Collectors ;
2628
29+ import org .checkstyle .autofix .parser .CheckConfiguration ;
2730import org .checkstyle .autofix .parser .CheckstyleViolation ;
31+ import org .checkstyle .autofix .recipe .Header ;
2832import org .checkstyle .autofix .recipe .UpperEll ;
2933import org .openrewrite .Recipe ;
3034
3135public 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}
0 commit comments