-
Notifications
You must be signed in to change notification settings - Fork 6
Issue #122: Support multiple instances of the same Checkstyle check #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. | ||
| // Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| package org.checkstyle.autofix; | ||
|
|
||
| public class CheckstyleConfigModule { | ||
| private final CheckstyleCheck check; | ||
| private final String id; | ||
|
|
||
| public CheckstyleConfigModule(CheckstyleCheck check, String id) { | ||
| this.check = check; | ||
| this.id = id; | ||
| } | ||
|
|
||
| public boolean matchesId(String input) { | ||
| return id != null && id.equals(input); | ||
| } | ||
|
|
||
| public boolean matchesCheck(String input) { | ||
| return CheckstyleCheck.fromSourceExact(input) | ||
| .map(checkFromInput -> checkFromInput == check) | ||
| .orElse(false); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,8 @@ public final class CheckstyleRecipeRegistry { | |
| CheckConfiguration, Recipe>> RECIPE_MAP_WITH_CONFIG = | ||
| new EnumMap<>(CheckstyleCheck.class); | ||
|
|
||
| private static final String HASH_SEPARATOR = "#"; | ||
|
|
||
| static { | ||
| RECIPE_MAP.put(CheckstyleCheck.UPPER_ELL, UpperEll::new); | ||
| RECIPE_MAP.put(CheckstyleCheck.HEX_LITERAL_CASE, HexLiteralCase::new); | ||
|
|
@@ -65,18 +67,49 @@ private CheckstyleRecipeRegistry() { | |
| * @return a list of generated Recipe objects | ||
| */ | ||
| public static List<Recipe> getRecipes(List<CheckstyleViolation> violations, | ||
| Map<CheckstyleCheck, CheckConfiguration> config) { | ||
| Map<CheckstyleConfigModule, CheckConfiguration> config) { | ||
| return violations.stream() | ||
| .collect(Collectors.groupingBy(CheckstyleViolation::getSource)) | ||
| .collect(Collectors.groupingBy(CheckstyleViolation::getCheckId)) | ||
| .entrySet() | ||
| .stream() | ||
| .map(entry -> { | ||
| return createRecipe(entry.getValue(), config.get(entry.getKey())); | ||
| final CheckConfiguration configuration = | ||
| findMatchingConfiguration(entry.getKey(), config); | ||
| return createRecipe(entry.getValue(), configuration); | ||
| }) | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static CheckConfiguration findMatchingConfiguration(String source, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkId And for all new methods. |
||
| Map<CheckstyleConfigModule, CheckConfiguration> config) { | ||
| return config.entrySet().stream() | ||
| .filter(configEntry -> matchesSource(configEntry.getKey(), source)) | ||
| .map(Map.Entry::getValue) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you return Optional and filter out absent values before trying to create recipe after calling |
||
| } | ||
|
|
||
| private static boolean matchesSource(CheckstyleConfigModule module, String source) { | ||
| final boolean matches; | ||
| if (source.contains(HASH_SEPARATOR)) { | ||
| matches = matchesWithHashSeparator(module, source); | ||
| } | ||
| else { | ||
| matches = module.matchesId(source) || module.matchesCheck(source); | ||
| } | ||
| return matches; | ||
| } | ||
|
|
||
| private static boolean matchesWithHashSeparator(CheckstyleConfigModule module, String source) { | ||
| final String[] parts = source.split(HASH_SEPARATOR, 2); | ||
| final String checkPart = parts[0]; | ||
| final String idPart = parts[1]; | ||
| final boolean exactMatch = module.matchesCheck(checkPart) && module.matchesId(idPart); | ||
| final boolean individualMatch = module.matchesId(source) || module.matchesCheck(source); | ||
| return exactMatch || individualMatch; | ||
| } | ||
|
|
||
| private static Recipe createRecipe(List<CheckstyleViolation> violations, | ||
| CheckConfiguration checkConfig) { | ||
| Recipe result = null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,6 @@ | |
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| import org.checkstyle.autofix.CheckstyleCheck; | ||
|
|
||
| public final class CheckstyleViolation { | ||
|
|
||
| private final int line; | ||
|
|
@@ -29,24 +27,24 @@ public final class CheckstyleViolation { | |
|
|
||
| private final String severity; | ||
|
|
||
| private final CheckstyleCheck source; | ||
| private final String checkId; | ||
|
|
||
| private final String message; | ||
|
|
||
| private final Path filePath; | ||
|
|
||
| public CheckstyleViolation(int line, int column, String severity, | ||
| CheckstyleCheck source, String message, Path filePath) { | ||
| String source, String message, Path filePath) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkId
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for other places |
||
| this.line = line; | ||
| this.column = column; | ||
| this.severity = severity; | ||
| this.source = source; | ||
| this.checkId = source; | ||
| this.message = message; | ||
| this.filePath = filePath; | ||
| } | ||
|
|
||
| public CheckstyleViolation(int line, String severity, | ||
| CheckstyleCheck source, String message, Path filePath) { | ||
| String source, String message, Path filePath) { | ||
| this(line, -1, severity, source, message, filePath); | ||
| } | ||
|
|
||
|
|
@@ -58,8 +56,8 @@ public Integer getColumn() { | |
| return column; | ||
| } | ||
|
|
||
| public CheckstyleCheck getSource() { | ||
| return source; | ||
| public String getCheckId() { | ||
| return checkId; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name is misleading, this is not a config, but just a wrapper around check, which adds id attribute from config properties. This abstraction makes the whole logic more complex, since you eventually use CheckstyleCheck.
Could you try to simplify the logic by combining both classes into one
CheckstyleCheckandCheckstyleConfigModule? In reality,idfield insideCheckstyleCheckis actually a fully qualified check's name