|
| 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.ArrayList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
| 28 | +import org.checkstyle.autofix.recipe.UpperEll; |
| 29 | +import org.openrewrite.Recipe; |
| 30 | + |
| 31 | +public final class CheckstyleRecipeRegistry { |
| 32 | + |
| 33 | + private static final Map<String, Function<List<CheckstyleViolation>, Recipe>> RECIPE_MAP = |
| 34 | + new HashMap<>(); |
| 35 | + |
| 36 | + static { |
| 37 | + RECIPE_MAP.put("UpperEllCheck", UpperEll::new); |
| 38 | + } |
| 39 | + |
| 40 | + private CheckstyleRecipeRegistry() { |
| 41 | + // utility class |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns a list of Recipe objects based on the given list of Checkstyle violations. |
| 46 | + * The method groups violations by their check name, finds the matching recipe factory |
| 47 | + * using the simple name of the check, and applies the factory to generate Recipe instances. |
| 48 | + * |
| 49 | + * @param violations the list of Checkstyle violations |
| 50 | + * @return a list of generated Recipe objects |
| 51 | + */ |
| 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)); |
| 56 | + |
| 57 | + final List<Recipe> recipes = new ArrayList<>(); |
| 58 | + |
| 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> records = entry.getValue(); |
| 64 | + |
| 65 | + final Function<List<CheckstyleViolation>, Recipe> recipeFactory = |
| 66 | + RECIPE_MAP.get(simpleCheckName); |
| 67 | + if (recipeFactory != null) { |
| 68 | + recipes.add(recipeFactory.apply(records)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return recipes; |
| 73 | + } |
| 74 | +} |
0 commit comments