Skip to content

Commit d9c27e4

Browse files
committed
Issue #122: Added support for multiple instances of same check
1 parent 0391daf commit d9c27e4

File tree

13 files changed

+71
-29
lines changed

13 files changed

+71
-29
lines changed

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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(CheckstyleCheck check, String id) {
21+
22+
}

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) {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Optional;
2727

2828
import org.checkstyle.autofix.CheckstyleCheck;
29+
import org.checkstyle.autofix.CheckstyleCheckInstance;
2930

3031
import de.jcup.sarif_2_1_0.SarifSchema210ImportExportSupport;
3132
import de.jcup.sarif_2_1_0.model.PhysicalLocation;
@@ -57,17 +58,22 @@ public List<CheckstyleViolation> parse(Path reportPath) {
5758
for (final Run run: report.getRuns()) {
5859
if (run.getResults() != null) {
5960
run.getResults().forEach(resultEntry -> {
60-
CheckstyleCheck.fromSource(resultEntry.getRuleId()).ifPresent(check -> {
61-
final CheckstyleViolation violation = createViolation(check, resultEntry);
62-
result.add(violation);
63-
});
64-
});
61+
CheckstyleCheck.fromSource(resultEntry.getRuleId()).ifPresent(
62+
check -> {
63+
final String id = Optional.of(resultEntry.getRuleId())
64+
.filter(src -> src.contains("#"))
65+
.map(src -> src.substring(src.indexOf('#') + 1))
66+
.orElse(null);
67+
result.add(createViolation(new
68+
CheckstyleCheckInstance(check, id), resultEntry));
69+
}); }
70+
);
6571
}
6672
}
6773
return result;
6874
}
6975

70-
private CheckstyleViolation createViolation(CheckstyleCheck check, Result result) {
76+
private CheckstyleViolation createViolation(CheckstyleCheckInstance check, Result result) {
7177
final String severity = result.getLevel().name();
7278
final String message = result.getMessage().getText();
7379
final PhysicalLocation location = result.getLocations().get(0).getPhysicalLocation();

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.xml.stream.events.XMLEvent;
3636

3737
import org.checkstyle.autofix.CheckstyleCheck;
38+
import org.checkstyle.autofix.CheckstyleCheckInstance;
3839

3940
public class XmlReportParser implements ReportParser {
4041

@@ -118,7 +119,8 @@ private Optional<CheckstyleViolation> parseErrorTag(StartElement startElement,
118119
String message = null;
119120
String severity = null;
120121
CheckstyleViolation violation = null;
121-
Optional<CheckstyleCheck> source = Optional.empty();
122+
Optional<CheckstyleCheck> check = Optional.empty();
123+
String id = null;
122124

123125
final Iterator<Attribute> attributes = startElement.getAttributes();
124126
while (attributes.hasNext()) {
@@ -138,15 +140,20 @@ private Optional<CheckstyleViolation> parseErrorTag(StartElement startElement,
138140
message = attribute.getValue();
139141
break;
140142
case SOURCE_ATTR:
141-
source = CheckstyleCheck.fromSource(attribute.getValue());
143+
final String attrValue = attribute.getValue();
144+
check = CheckstyleCheck.fromSource(attrValue);
145+
final int index = attrValue.indexOf('#');
146+
if (index >= 0) {
147+
id = attrValue.substring(index + 1);
148+
}
142149
break;
143150
default:
144151
break;
145152
}
146153
}
147-
if (source.isPresent()) {
154+
if (check.isPresent()) {
148155
violation = new CheckstyleViolation(line, column, severity,
149-
source.get(), message, Path.of(filename));
156+
new CheckstyleCheckInstance(check.get(), id), message, Path.of(filename));
150157
}
151158
return Optional.ofNullable(violation);
152159

src/test/java/org/checkstyle/autofix/parser/CheckstyleReportsParserTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Collectors;
2727

2828
import org.checkstyle.autofix.CheckstyleCheck;
29+
import org.checkstyle.autofix.CheckstyleCheckInstance;
2930
import org.junit.jupiter.api.Test;
3031

3132
public class CheckstyleReportsParserTest {
@@ -49,7 +50,8 @@ public void testParseFromResource() throws Exception {
4950
assertEquals(13, record.getColumn());
5051
assertEquals("error", record.getSeverity());
5152
assertEquals("Example message", record.getMessage());
52-
assertEquals(CheckstyleCheck.UPPER_ELL, record.getSource());
53+
assertEquals(new CheckstyleCheckInstance(CheckstyleCheck.UPPER_ELL, null),
54+
record.getSource());
5355
assertEquals(Path.of("Example.java"), record.getFilePath());
5456
}
5557

0 commit comments

Comments
 (0)