Skip to content

Commit 8d4fdf6

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue checkstyle#78: Replace String-based Check Names with Enum
1 parent 5d2a1b2 commit 8d4fdf6

File tree

11 files changed

+117
-172
lines changed

11 files changed

+117
-172
lines changed

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

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

2020
import java.nio.file.Path;
2121
import java.util.List;
22+
import java.util.Map;
2223

2324
import org.checkstyle.autofix.parser.CheckConfiguration;
2425
import org.checkstyle.autofix.parser.CheckstyleReportParser;
@@ -83,12 +84,13 @@ public String getPropertiesPath() {
8384
public List<Recipe> getRecipeList() {
8485
final List<CheckstyleViolation> violations = CheckstyleReportParser
8586
.parse(Path.of(getViolationReportPath()));
86-
final CheckConfiguration configuration = loadCheckstyleConfiguration();
87+
final Map<CheckstyleCheck,
88+
CheckConfiguration> configuration = loadCheckstyleConfiguration();
8789

8890
return CheckstyleRecipeRegistry.getRecipes(violations, configuration);
8991
}
9092

91-
private CheckConfiguration loadCheckstyleConfiguration() {
93+
private Map<CheckstyleCheck, CheckConfiguration> loadCheckstyleConfiguration() {
9294
return ConfigurationLoader.loadConfiguration(getConfigurationPath(), getPropertiesPath());
9395
}
9496
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import java.util.Optional;
2222

2323
public enum CheckstyleCheck {
24-
FINALLOCALVARIABLE("com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck"),
24+
FINAL_LOCAL_VARIABLE("com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck"),
2525
HEADER("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"),
26-
REDUNDANTIMPORT("com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck"),
27-
UPPERELL("com.puppycrawl.tools.checkstyle.checks.UpperEllCheck");
26+
UPPER_ELL("com.puppycrawl.tools.checkstyle.checks.UpperEllCheck"),
27+
REDUNDANT_IMPORT("com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck");
2828

2929
private final String id;
3030

@@ -38,9 +38,7 @@ public String getId() {
3838

3939
public static Optional<CheckstyleCheck> fromSource(String source) {
4040
return Arrays.stream(values())
41-
.filter(check -> check.getId().equals(source))
41+
.filter(check -> check.getId().contains(source))
4242
.findFirst();
43-
4443
}
45-
4644
}

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Objects;
24-
import java.util.Optional;
2524
import java.util.function.BiFunction;
2625
import java.util.function.Function;
2726
import java.util.stream.Collectors;
@@ -44,10 +43,10 @@ public final class CheckstyleRecipeRegistry {
4443
new EnumMap<>(CheckstyleCheck.class);
4544

4645
static {
47-
RECIPE_MAP.put(CheckstyleCheck.UPPERELL, UpperEll::new);
48-
RECIPE_MAP.put(CheckstyleCheck.FINALLOCALVARIABLE, FinalLocalVariable::new);
49-
RECIPE_MAP.put(CheckstyleCheck.REDUNDANTIMPORT, RedundantImport::new);
46+
RECIPE_MAP.put(CheckstyleCheck.UPPER_ELL, UpperEll::new);
47+
RECIPE_MAP.put(CheckstyleCheck.FINAL_LOCAL_VARIABLE, FinalLocalVariable::new);
5048
RECIPE_MAP_WITH_CONFIG.put(CheckstyleCheck.HEADER, Header::new);
49+
RECIPE_MAP.put(CheckstyleCheck.REDUNDANT_IMPORT, RedundantImport::new);
5150
}
5251

5352
private CheckstyleRecipeRegistry() {
@@ -64,47 +63,36 @@ private CheckstyleRecipeRegistry() {
6463
* @return a list of generated Recipe objects
6564
*/
6665
public static List<Recipe> getRecipes(List<CheckstyleViolation> violations,
67-
CheckConfiguration config) {
66+
Map<CheckstyleCheck, CheckConfiguration> config) {
6867
return violations.stream()
6968
.collect(Collectors.groupingBy(CheckstyleViolation::getSource))
7069
.entrySet()
7170
.stream()
72-
.map(entry -> createRecipe(entry, config))
71+
.map(entry -> {
72+
return createRecipe(entry.getValue(), config.get(entry.getKey()));
73+
})
7374
.filter(Objects::nonNull)
7475
.collect(Collectors.toList());
7576
}
7677

77-
private static Recipe createRecipe(Map.Entry<String, List<CheckstyleViolation>> entry,
78-
CheckConfiguration config) {
78+
private static Recipe createRecipe(List<CheckstyleViolation> violations,
79+
CheckConfiguration checkConfig) {
80+
Recipe result = null;
81+
if (checkConfig != null) {
7982

80-
Recipe recipe = null;
81-
82-
final Optional<CheckstyleCheck> check = CheckstyleCheck.fromSource(entry.getKey());
83-
84-
if (check.isPresent()) {
85-
86-
final CheckstyleCheck checkstyleCheck = check.get();
87-
final List<CheckstyleViolation> violations = entry.getValue();
83+
final CheckstyleCheck check = checkConfig.getCheck();
8884

8985
final BiFunction<List<CheckstyleViolation>, CheckConfiguration,
90-
Recipe> configRecipeFactory = RECIPE_MAP_WITH_CONFIG.get(checkstyleCheck);
86+
Recipe> configRecipeFactory = RECIPE_MAP_WITH_CONFIG.get(check);
9187

92-
if (configRecipeFactory == null) {
93-
final Function<List<CheckstyleViolation>, Recipe> simpleRecipeFactory =
94-
RECIPE_MAP.get(checkstyleCheck);
95-
recipe = simpleRecipeFactory.apply(violations);
88+
if (configRecipeFactory != null) {
89+
result = configRecipeFactory.apply(violations, checkConfig);
9690
}
9791
else {
98-
final CheckConfiguration subConfig =
99-
extractCheckConfiguration(config, checkstyleCheck.name());
100-
recipe = configRecipeFactory.apply(violations, subConfig);
92+
result = RECIPE_MAP.get(check).apply(violations);
10193
}
10294
}
103-
return recipe;
95+
return result;
10496
}
10597

106-
private static CheckConfiguration extractCheckConfiguration(CheckConfiguration config,
107-
String checkName) {
108-
return config.getConfig(checkName);
109-
}
11098
}

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

Lines changed: 21 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,37 @@
1717

1818
package org.checkstyle.autofix.parser;
1919

20-
import java.util.List;
20+
import java.util.HashMap;
2121
import java.util.Map;
22-
import java.util.Set;
22+
23+
import org.checkstyle.autofix.CheckstyleCheck;
2324

2425
public final class CheckConfiguration {
25-
private final String name;
26+
private final CheckstyleCheck check;
27+
private final Map<String, String> globalProperties;
2628
private final Map<String, String> properties;
27-
private final List<CheckConfiguration> children;
28-
private CheckConfiguration parent;
29-
30-
public CheckConfiguration(String name,
31-
Map<String, String> properties, List<CheckConfiguration> children) {
32-
this.name = name;
33-
this.properties = properties;
34-
this.children = children;
35-
36-
for (CheckConfiguration child : children) {
37-
child.setParent(this);
38-
}
39-
}
40-
41-
public String getName() {
42-
return name;
43-
}
4429

45-
private CheckConfiguration getParent() {
46-
return parent;
30+
public CheckConfiguration(CheckstyleCheck name,
31+
Map<String, String> globalProperties,
32+
Map<String, String> properties) {
33+
this.check = name;
34+
this.globalProperties = new HashMap<>(globalProperties);
35+
this.properties = new HashMap<>(properties);
4736
}
4837

49-
public List<CheckConfiguration> getChildren() {
50-
return children;
38+
public CheckstyleCheck getCheck() {
39+
return check;
5140
}
5241

5342
public String getProperty(String key) {
54-
String value = null;
55-
43+
final String result;
5644
if (properties.containsKey(key)) {
57-
value = properties.get(key);
45+
result = properties.get(key);
5846
}
59-
else if (getParent() != null) {
60-
value = getParent().getProperty(key);
47+
else {
48+
result = globalProperties.get(key);
6149
}
62-
return value;
50+
return result;
6351
}
6452

6553
public String getPropertyOrDefault(String key, String defaultValue) {
@@ -71,57 +59,10 @@ public String getPropertyOrDefault(String key, String defaultValue) {
7159
}
7260

7361
public boolean hasProperty(String key) {
74-
boolean result = false;
75-
76-
if (properties.containsKey(key)) {
77-
result = true;
78-
}
79-
else if (getParent() != null) {
80-
result = getParent().hasProperty(key);
81-
}
82-
return result;
83-
}
84-
85-
public int[] getIntArray(String propertyName) {
86-
final String value = properties.get(propertyName);
87-
final int[] result;
88-
final String[] parts = value.split(",");
89-
result = new int[parts.length];
90-
for (int index = 0; index < parts.length; index++) {
91-
try {
92-
result[index] = Integer.parseInt(parts[index].trim());
93-
}
94-
catch (NumberFormatException exception) {
95-
throw new IllegalArgumentException("Property '" + propertyName
96-
+ "' has an invalid integer value: " + parts[index].trim(), exception);
97-
}
98-
}
99-
return result;
100-
}
101-
102-
public CheckConfiguration getConfig(String childName) {
103-
CheckConfiguration result = null;
104-
if (name.equals(childName)) {
105-
result = this;
106-
}
107-
else {
108-
final List<CheckConfiguration> childrenList = getChildren();
109-
for (final CheckConfiguration current : childrenList) {
110-
if (childName.equals(current.getName())) {
111-
result = current;
112-
break;
113-
}
114-
childrenList.addAll(current.getChildren());
115-
}
116-
}
117-
return result;
118-
}
119-
120-
public Set<String> getPropertyNames() {
121-
return properties.keySet();
62+
return properties.containsKey(key) || globalProperties.containsKey(key);
12263
}
12364

124-
private void setParent(CheckConfiguration parent) {
125-
this.parent = parent;
65+
public void setGlobalProperty(String key, String value) {
66+
globalProperties.put(key, value);
12667
}
12768
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Iterator;
2626
import java.util.List;
2727
import java.util.Objects;
28+
import java.util.Optional;
2829

2930
import javax.xml.stream.XMLEventReader;
3031
import javax.xml.stream.XMLInputFactory;
@@ -33,6 +34,8 @@
3334
import javax.xml.stream.events.StartElement;
3435
import javax.xml.stream.events.XMLEvent;
3536

37+
import org.checkstyle.autofix.CheckstyleCheck;
38+
3639
public final class CheckstyleReportParser {
3740

3841
private static final String FILE_TAG = "file";
@@ -78,7 +81,7 @@ public static List<CheckstyleViolation> parse(Path xmlPath) {
7881
}
7982
else if (ERROR_TAG.equals(startElementName)) {
8083
Objects.requireNonNull(filename, "File name can not be null");
81-
result.add(parseErrorTag(startElement, filename));
84+
parseErrorTag(startElement, filename).ifPresent(result::add);
8285
}
8386
}
8487
}
@@ -111,14 +114,16 @@ private static String parseFileTag(StartElement startElement) {
111114
return fileName;
112115
}
113116

114-
private static CheckstyleViolation parseErrorTag(StartElement startElement, String filename) {
117+
private static Optional<CheckstyleViolation> parseErrorTag(StartElement startElement,
118+
String filename) {
115119
int line = -1;
116120
int column = -1;
117-
String source = null;
118121
String message = null;
119122
String severity = null;
120-
final Iterator<Attribute> attributes = startElement
121-
.getAttributes();
123+
CheckstyleViolation violation = null;
124+
Optional<CheckstyleCheck> source = Optional.empty();
125+
126+
final Iterator<Attribute> attributes = startElement.getAttributes();
122127
while (attributes.hasNext()) {
123128
final Attribute attribute = attributes.next();
124129
final String attrName = attribute.getName().getLocalPart();
@@ -136,14 +141,17 @@ private static CheckstyleViolation parseErrorTag(StartElement startElement, Stri
136141
message = attribute.getValue();
137142
break;
138143
case SOURCE_ATTR:
139-
source = attribute.getValue();
144+
source = CheckstyleCheck.fromSource(attribute.getValue());
140145
break;
141146
default:
142147
break;
143148
}
144149
}
145-
return new CheckstyleViolation(
146-
line, column, severity, source, message, filename);
150+
if (source.isPresent()) {
151+
violation = new CheckstyleViolation(line, column, severity,
152+
source.get(), message, filename);
153+
}
154+
return Optional.ofNullable(violation);
147155

148156
}
149157
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.checkstyle.autofix.parser;
1919

20+
import org.checkstyle.autofix.CheckstyleCheck;
21+
2022
public final class CheckstyleViolation {
2123

2224
private final int line;
@@ -25,14 +27,14 @@ public final class CheckstyleViolation {
2527

2628
private final String severity;
2729

28-
private final String source;
30+
private final CheckstyleCheck source;
2931

3032
private final String message;
3133

3234
private final String fileName;
3335

34-
public CheckstyleViolation(int line, int column,
35-
String severity, String source, String message, String fileName) {
36+
public CheckstyleViolation(int line, int column, String severity,
37+
CheckstyleCheck source, String message, String fileName) {
3638
this.line = line;
3739
this.column = column;
3840
this.severity = severity;
@@ -49,7 +51,7 @@ public Integer getColumn() {
4951
return column;
5052
}
5153

52-
public String getSource() {
54+
public CheckstyleCheck getSource() {
5355
return source;
5456
}
5557

0 commit comments

Comments
 (0)