Skip to content

Commit e0fbc20

Browse files
committed
Issue #122: Added support for multiple instances of same check
1 parent bb2e6c3 commit e0fbc20

File tree

16 files changed

+269
-220
lines changed

16 files changed

+269
-220
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# All files default to text with LF line endings
2+
* text eol=lf
3+
4+
# Windows files always have CRLF eol
5+
*.cmd text eol=crlf

config/pitest-suppressions.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@
11231123
<mutatedMethod>parseErrorTag</mutatedMethod>
11241124
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
11251125
<description>removed call to java/util/Optional::empty</description>
1126-
<lineContent>Optional&lt;CheckstyleCheck&gt; source = Optional.empty();</lineContent>
1126+
<lineContent>Optional&lt;CheckstyleCheck&gt; check = Optional.empty();</lineContent>
11271127
</mutation>
11281128

11291129
<mutation unstable="false">
@@ -1132,7 +1132,7 @@
11321132
<mutatedMethod>parseErrorTag</mutatedMethod>
11331133
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
11341134
<description>removed conditional - replaced equality check with true</description>
1135-
<lineContent>if (source.isPresent()) {</lineContent>
1135+
<lineContent>if (check.isPresent()) {</lineContent>
11361136
</mutation>
11371137

11381138
<mutation unstable="false">

mvnw.cmd

Lines changed: 189 additions & 189 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
<!-- Checkstyle properties -->
4747
<maven.checkstyle.plugin.version>3.6.0</maven.checkstyle.plugin.version>
48-
<checkstyle.version>12.1.0</checkstyle.version>
48+
<checkstyle.version>12.2.0</checkstyle.version>
4949

5050
<!-- Pitest properties -->
5151
<pitest.version>1.22.0</pitest.version>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public List<Recipe> getRecipeList() {
8888
final ReportParser reportParser = createReportParser(getViolationReportPath());
8989
final List<CheckstyleViolation> violations = reportParser
9090
.parse(Path.of(getViolationReportPath()));
91-
final Map<CheckstyleCheck,
91+
final Map<CheckstyleCheckInstance,
9292
CheckConfiguration> configuration = loadCheckstyleConfiguration();
9393

9494
return CheckstyleRecipeRegistry.getRecipes(violations, configuration);
@@ -108,7 +108,7 @@ else if (path.endsWith(".sarif") || path.endsWith(".sarif.json")) {
108108
return result;
109109
}
110110

111-
private Map<CheckstyleCheck, CheckConfiguration> loadCheckstyleConfiguration() {
111+
private Map<CheckstyleCheckInstance, CheckConfiguration> loadCheckstyleConfiguration() {
112112
return ConfigurationLoader.loadConfiguration(getConfigurationPath(), getPropertiesPath());
113113
}
114114
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public String getId() {
3939

4040
public static Optional<CheckstyleCheck> fromSource(String source) {
4141
return Arrays.stream(values())
42-
.filter(check -> check.getId().contains(source))
42+
.filter(check -> check.getId().contains(source.split("#")[0]))
4343
.findFirst();
4444
}
4545
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
public record CheckstyleCheckInstance(
21+
CheckstyleCheck check,
22+
String id
23+
) {
24+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private CheckstyleRecipeRegistry() {
6565
* @return a list of generated Recipe objects
6666
*/
6767
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations,
68-
Map<CheckstyleCheck, CheckConfiguration> config) {
68+
Map<CheckstyleCheckInstance, CheckConfiguration> config) {
6969
return violations.stream()
7070
.collect(Collectors.groupingBy(CheckstyleViolation::getSource))
7171
.entrySet()

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import java.nio.file.Path;
2121

22-
import org.checkstyle.autofix.CheckstyleCheck;
22+
import org.checkstyle.autofix.CheckstyleCheckInstance;
2323

2424
public final class CheckstyleViolation {
2525

@@ -29,14 +29,14 @@ public final class CheckstyleViolation {
2929

3030
private final String severity;
3131

32-
private final CheckstyleCheck source;
32+
private final CheckstyleCheckInstance source;
3333

3434
private final String message;
3535

3636
private final Path filePath;
3737

3838
public CheckstyleViolation(int line, int column, String severity,
39-
CheckstyleCheck source, String message, Path filePath) {
39+
CheckstyleCheckInstance source, String message, Path filePath) {
4040
this.line = line;
4141
this.column = column;
4242
this.severity = severity;
@@ -46,7 +46,7 @@ public CheckstyleViolation(int line, int column, String severity,
4646
}
4747

4848
public CheckstyleViolation(int line, String severity,
49-
CheckstyleCheck source, String message, Path filePath) {
49+
CheckstyleCheckInstance source, String message, Path filePath) {
5050
this(line, -1, severity, source, message, filePath);
5151
}
5252

@@ -58,7 +58,7 @@ public Integer getColumn() {
5858
return column;
5959
}
6060

61-
public CheckstyleCheck getSource() {
61+
public CheckstyleCheckInstance getSource() {
6262
return source;
6363
}
6464

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Properties;
2626

2727
import org.checkstyle.autofix.CheckstyleCheck;
28+
import org.checkstyle.autofix.CheckstyleCheckInstance;
2829

2930
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
3031
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
@@ -36,14 +37,16 @@ private ConfigurationLoader() {
3637
// utility class
3738
}
3839

39-
public static Map<CheckstyleCheck, CheckConfiguration> mapConfiguration(Configuration config) {
40-
final Map<CheckstyleCheck, CheckConfiguration> result = new HashMap<>();
41-
final Map<String, String> inherited = getProperties(config);
40+
public static Map<CheckstyleCheckInstance,
41+
CheckConfiguration> mapConfiguration(Configuration config) {
4242

43+
final Map<CheckstyleCheckInstance, CheckConfiguration> result = new HashMap<>();
44+
final Map<String, String> inherited = getProperties(config);
4345
final Optional<CheckstyleCheck> module = CheckstyleCheck.fromSource(config.getName());
46+
4447
module.ifPresent(checkstyleCheck -> {
45-
result.put(checkstyleCheck, new CheckConfiguration(checkstyleCheck, new HashMap<>(),
46-
getProperties(config)));
48+
result.put(new CheckstyleCheckInstance(checkstyleCheck, inherited.get("id")),
49+
new CheckConfiguration(checkstyleCheck, new HashMap<>(), inherited));
4750
});
4851

4952
for (Configuration child : config.getChildren()) {
@@ -69,7 +72,7 @@ private static Map<String, String> getProperties(Configuration config) {
6972
return props;
7073
}
7174

72-
public static Map<CheckstyleCheck, CheckConfiguration> loadConfiguration(
75+
public static Map<CheckstyleCheckInstance, CheckConfiguration> loadConfiguration(
7376
String checkstyleConfigurationPath, String propFile) {
7477
Properties props = new Properties();
7578
if (propFile == null) {

0 commit comments

Comments
 (0)