Skip to content

Commit cf29987

Browse files
committed
chore: static code analsis (PMD) fixes and stepping dependencies
1 parent f87f42f commit cf29987

27 files changed

+115
-151
lines changed

build.gradle

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
}
88
dependencies {
99
classpath 'se.bjurr.gradle:gradle-scripts:2.+'
10-
classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.2'
10+
classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.2.1'
1111
}
1212
}
1313
project.ext.buildConfig = [
@@ -42,8 +42,8 @@ dependencies {
4242
api 'com.google.code.gson:gson:2.10.1'
4343

4444
testImplementation 'junit:junit:4.13.2'
45-
testImplementation 'org.assertj:assertj-core:3.24.1'
46-
testImplementation 'uk.co.jemos.podam:podam:7.2.11.RELEASE'
47-
testImplementation 'com.approvaltests:approvaltests:18.5.0'
48-
testImplementation 'com.networknt:json-schema-validator:1.0.76'
45+
testImplementation 'org.assertj:assertj-core:3.25.2'
46+
testImplementation 'uk.co.jemos.podam:podam:8.0.1.RELEASE'
47+
testImplementation 'com.approvaltests:approvaltests:22.3.3'
48+
testImplementation 'com.networknt:json-schema-validator:1.3.0'
4949
}

src/main/java/se/bjurr/violations/lib/FilteringViolationsLogger.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.logging.Logger;
66

77
public final class FilteringViolationsLogger implements ViolationsLogger {
8-
private static Logger LOG = Logger.getLogger(ViolationsApi.class.getSimpleName());
8+
private static final Logger LOGGER = Logger.getLogger(ViolationsApi.class.getSimpleName());
99
private final ViolationsLogger violationsLogger;
1010

1111
public static ViolationsLogger filterLevel(final ViolationsLogger violationsLogger) {
@@ -18,14 +18,14 @@ private FilteringViolationsLogger(final ViolationsLogger violationsLogger) {
1818

1919
@Override
2020
public void log(final Level level, final String string) {
21-
if (LOG.isLoggable(level)) {
21+
if (LOGGER.isLoggable(level)) {
2222
this.violationsLogger.log(level, string);
2323
}
2424
}
2525

2626
@Override
2727
public void log(final Level level, final String string, final Throwable t) {
28-
if (LOG.isLoggable(level)) {
28+
if (LOGGER.isLoggable(level)) {
2929
this.violationsLogger.log(level, string, t);
3030
}
3131
}

src/main/java/se/bjurr/violations/lib/ViolationsApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import se.bjurr.violations.lib.reports.ViolationsFinder;
2525

2626
@SuppressFBWarnings("CRLF_INJECTION_LOGS")
27-
public class ViolationsApi {
27+
public final class ViolationsApi {
2828
private final Logger LOGGER = Logger.getLogger(ViolationsApi.class.getSimpleName());
2929
private String pattern;
3030
private ViolationsParser violationsParser;

src/main/java/se/bjurr/violations/lib/model/Violation.java

+36-35
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,45 @@
88
import java.io.Serializable;
99
import java.util.HashMap;
1010
import java.util.Map;
11-
import se.bjurr.violations.lib.parsers.CPPCheckParser;
1211
import se.bjurr.violations.lib.reports.Parser;
1312

1413
public class Violation implements Serializable, Comparable<Violation> {
14+
15+
private static final long serialVersionUID = -6052921679385466168L;
16+
/** A {@link #file} used when there is no file specified in the parsed report. */
17+
public static final String NO_FILE = "-";
18+
/** A {@link #startLine} used when there is no line specified in the parsed report. */
19+
public static final Integer NO_LINE = 0;
20+
21+
private Integer column;
22+
private final Integer endLine;
23+
private final Integer endColumn;
24+
/** Also see {@link #NO_FILE} */
25+
private final String file;
26+
27+
private final String message;
28+
/** The algorithm, the format, used. */
29+
private final Parser parser;
30+
/**
31+
* Intended as the tool used to find the violation. Like Detekt, when it is being used to find
32+
* violations and report them in the {@link Parser#CHECKSTYLE} format.
33+
*/
34+
private String reporter;
35+
36+
private final String rule;
37+
private final String category;
38+
/**
39+
* Something that identifies a group that this violation belongs to. First introduced with {@link
40+
* se.bjurr.violations.lib.parsers.CPPCheckParser} to record what error tag each violation belongs
41+
* to.
42+
*/
43+
private final String group;
44+
45+
private final SEVERITY severity;
46+
private final String source;
47+
private final Map<String, String> specifics;
48+
private final Integer startLine;
49+
1550
public static class ViolationBuilder {
1651

1752
private Integer column;
@@ -116,44 +151,10 @@ public ViolationBuilder setGroup(final String group) {
116151
}
117152
}
118153

119-
private static final long serialVersionUID = -6052921679385466168L;
120-
/** A {@link #file} used when there is no file specified in the parsed report. */
121-
public static final String NO_FILE = "-";
122-
/** A {@link #startLine} used when there is no line specified in the parsed report. */
123-
public static final Integer NO_LINE = 0;
124-
125154
public static ViolationBuilder violationBuilder() {
126155
return new ViolationBuilder();
127156
}
128157

129-
private Integer column;
130-
private final Integer endLine;
131-
private final Integer endColumn;
132-
/** Also see {@link #NO_FILE} */
133-
private final String file;
134-
135-
private final String message;
136-
/** The algorithm, the format, used. */
137-
private final Parser parser;
138-
/**
139-
* Intended as the tool used to find the violation. Like Detekt, when it is being used to find
140-
* violations and report them in the {@link Parser#CHECKSTYLE} format.
141-
*/
142-
private String reporter;
143-
144-
private final String rule;
145-
private final String category;
146-
/**
147-
* Something that identifies a group that this violation belongs to. First introduced with {@link
148-
* CPPCheckParser} to record what error tag each violation belongs to.
149-
*/
150-
private final String group;
151-
152-
private final SEVERITY severity;
153-
private final String source;
154-
private final Map<String, String> specifics;
155-
private final Integer startLine;
156-
157158
public Violation() {
158159
this.startLine = null;
159160
this.endLine = null;

src/main/java/se/bjurr/violations/lib/parsers/AnsibleLaterParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
public class AnsibleLaterParser implements ViolationsParser {
1717

1818
public class AnsibleLaterEntry {
19-
public AnsibleLaterEntry() {}
20-
2119
public String asctime;
2220
public String levelname;
2321
public String message;
@@ -28,6 +26,8 @@ public AnsibleLaterEntry() {}
2826
public String later_sid;
2927
public Integer later_lineno;
3028
public String later_message;
29+
30+
public AnsibleLaterEntry() {}
3131
}
3232

3333
@Override

src/main/java/se/bjurr/violations/lib/parsers/CPPCheckParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Set<Violation> parseReportOutput(
119119

120120
private String constructMessage(
121121
final String msg, final Optional<String> verbose, final Optional<String> info) {
122-
String message = "";
122+
String message;
123123
if (verbose.orElse("").startsWith(msg)) {
124124
message = verbose.get();
125125
} else {

src/main/java/se/bjurr/violations/lib/parsers/CSSLintParser.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,18 @@ public Set<Violation> parseReportOutput(
3232
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
3333
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
3434
String filename = null;
35-
Integer line = null;
36-
Optional<Integer> charAttrib = null;
37-
String severity = null;
38-
String message = null;
39-
String evidence = null;
4035
while (xmlr.hasNext()) {
4136
final int eventType = xmlr.next();
4237
if (eventType == XMLStreamConstants.START_ELEMENT) {
4338
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
4439
filename = getAttribute(xmlr, "name");
4540
}
4641
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
47-
line = findIntegerAttribute(xmlr, "line").orElse(1);
48-
charAttrib = findIntegerAttribute(xmlr, "char");
49-
severity = getAttribute(xmlr, "severity");
50-
message = getAttribute(xmlr, "reason");
51-
evidence = findAttribute(xmlr, "evidence").orElse("").trim();
42+
final Integer line = findIntegerAttribute(xmlr, "line").orElse(1);
43+
final Optional<Integer> charAttrib = findIntegerAttribute(xmlr, "char");
44+
final String severity = getAttribute(xmlr, "severity");
45+
final String message = getAttribute(xmlr, "reason");
46+
final String evidence = findAttribute(xmlr, "evidence").orElse("").trim();
5247
final Violation violation =
5348
violationBuilder() //
5449
.setParser(CSSLINT) //

src/main/java/se/bjurr/violations/lib/parsers/CodeNarcParser.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ public Set<Violation> parseReportOutput(
5252

5353
String path = null;
5454
String name = null;
55-
String ruleName = null;
56-
Integer priority = null;
57-
Integer lineNumber = null;
5855
while (xmlr.hasNext()) {
5956
final int eventType = xmlr.next();
6057
if (eventType == START_ELEMENT) {
@@ -65,18 +62,18 @@ public Set<Violation> parseReportOutput(
6562
name = getAttribute(xmlr, "name");
6663
}
6764
if (xmlr.getLocalName().equalsIgnoreCase("Violation")) {
68-
ruleName = getAttribute(xmlr, "ruleName");
69-
priority = getIntegerAttribute(xmlr, "priority");
65+
final String ruleName = getAttribute(xmlr, "ruleName");
66+
final Integer priority = getIntegerAttribute(xmlr, "priority");
7067
final String lineNumberString = getAttribute(xmlr, "lineNumber");
71-
lineNumber = 1;
68+
int lineNumber = 1;
7269
if (!lineNumberString.isEmpty()) {
7370
lineNumber = Integer.parseInt(lineNumberString);
7471
}
7572
String message = rules.get(ruleName);
7673
if (message == null) {
7774
message = ruleName;
7875
}
79-
String fileString = null;
76+
String fileString;
8077
if (sourceDirectory.isEmpty()) {
8178
fileString = path + "/" + name;
8279
} else {
@@ -119,15 +116,14 @@ private Map<String, String> getRules(final String string) throws IOException, XM
119116
try (InputStream input = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8))) {
120117
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
121118
String name = null;
122-
String description = null;
123119
while (xmlr.hasNext()) {
124120
final int eventType = xmlr.next();
125121
if (eventType == START_ELEMENT) {
126122
if (xmlr.getLocalName().equalsIgnoreCase("Rule")) {
127123
name = getAttribute(xmlr, "name");
128124
}
129125
if (xmlr.getLocalName().equalsIgnoreCase("Description")) {
130-
description = xmlr.getElementText().trim();
126+
final String description = xmlr.getElementText().trim();
131127
rules.put(name, description);
132128
}
133129
}

src/main/java/se/bjurr/violations/lib/parsers/DocFXParser.java

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public Set<Violation> parseReportOutput(
2424
for (final String rawLineToParse : lines) {
2525
@SuppressWarnings("unchecked")
2626
final Map<String, Object> parsedMap = new Gson().fromJson(rawLineToParse, Map.class);
27-
;
2827
final String message = (String) parsedMap.get("message");
2928
final String messageSeverity = (String) parsedMap.get("message_severity");
3029
final String fileEncoded = (String) parsedMap.get("file");

src/main/java/se/bjurr/violations/lib/parsers/Flake8Parser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Set<Violation> parseReportOutput(
3232
getLines(string, "([^:]*):(\\d+)?:?(\\d+)?:? \\[?(\\D+)(\\d*)\\]? (.*)");
3333
for (final List<String> parts : partsPerLine) {
3434
final String filename = parts.get(1);
35-
Integer line = null;
35+
Integer line;
3636
try {
3737
line = parseInt(parts.get(2));
3838
} catch (final Exception e) {

src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,18 @@ public Set<Violation> parseReportOutput(
3232
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
3333

3434
String name = null;
35-
String findingType = null;
36-
Integer line = null;
37-
String message = null;
38-
String origin = null;
39-
String severity = null;
4035
while (xmlr.hasNext()) {
4136
final int eventType = xmlr.next();
4237
if (eventType == START_ELEMENT) {
4338
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
4439
name = getAttribute(xmlr, "name");
4540
}
4641
if (xmlr.getLocalName().equalsIgnoreCase("item")) {
47-
findingType = getAttribute(xmlr, "finding-type");
48-
line = getIntegerAttribute(xmlr, "line");
49-
message = getAttribute(xmlr, "message");
50-
origin = getAttribute(xmlr, "origin");
51-
severity = getAttribute(xmlr, "severity");
42+
final String findingType = getAttribute(xmlr, "finding-type");
43+
final Integer line = getIntegerAttribute(xmlr, "line");
44+
final String message = getAttribute(xmlr, "message");
45+
final String origin = getAttribute(xmlr, "origin");
46+
final String severity = getAttribute(xmlr, "severity");
5247
final Violation violation =
5348
violationBuilder() //
5449
.setParser(JCREPORT) //

src/main/java/se/bjurr/violations/lib/parsers/JSLintParser.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,19 @@ public Set<Violation> parseReportOutput(
2929
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
3030
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
3131
String filename = null;
32-
Integer line = null;
33-
Integer charAttrib = null;
34-
String severity = null;
35-
String message = null;
36-
String evidence = null;
3732
while (xmlr.hasNext()) {
3833
final int eventType = xmlr.next();
3934
if (eventType == XMLStreamConstants.START_ELEMENT) {
4035
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
4136
filename = getAttribute(xmlr, "name");
4237
}
4338
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
44-
line = getIntegerAttribute(xmlr, "line");
45-
charAttrib = getIntegerAttribute(xmlr, "char");
46-
severity = getAttribute(xmlr, "severity");
39+
final Integer line = getIntegerAttribute(xmlr, "line");
40+
final Integer charAttrib = getIntegerAttribute(xmlr, "char");
41+
final String severity = getAttribute(xmlr, "severity");
4742
final String reason = getAttribute(xmlr, "reason").trim();
48-
evidence = getAttribute(xmlr, "evidence").trim();
49-
message = reason + ": " + evidence;
43+
final String evidence = getAttribute(xmlr, "evidence").trim();
44+
final String message = reason + ": " + evidence;
5045
final Violation violation =
5146
violationBuilder()
5247
.setParser(JSLINT) //

src/main/java/se/bjurr/violations/lib/parsers/JUnitParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private FileAndLine findFilePathInContent(final String failureContent, final Str
136136
fl.line = Integer.parseInt(matcher.group(3));
137137
found.add(fl);
138138
}
139-
if (found.size() == 0) {
139+
if (found.isEmpty()) {
140140
return null;
141141
}
142142
if (found.size() == 1) {

src/main/java/se/bjurr/violations/lib/parsers/JacocoParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public Set<Violation> parseReportOutput(
4444
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
4545
while (xmlr.hasNext()) {
4646
final int eventType = xmlr.next();
47-
switch (eventType) {
47+
switch (eventType) { // NOPMD
4848
case XMLStreamConstants.START_ELEMENT:
49-
switch (xmlr.getLocalName()) {
49+
switch (xmlr.getLocalName()) { // NOPMD
5050
case "package":
5151
builder.setPackageDetails(getAttribute(xmlr, "name"));
5252
break;

src/main/java/se/bjurr/violations/lib/parsers/LintParser.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,18 @@ public Set<Violation> parseReportOutput(
3131
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
3232
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
3333
String filename = null;
34-
Integer line = null;
35-
Optional<Integer> charAttrib = null;
36-
String severity = null;
37-
String message = null;
38-
String evidence = null;
3934
while (xmlr.hasNext()) {
4035
final int eventType = xmlr.next();
4136
if (eventType == XMLStreamConstants.START_ELEMENT) {
4237
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
4338
filename = getAttribute(xmlr, "name");
4439
}
4540
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
46-
line = getIntegerAttribute(xmlr, "line");
47-
charAttrib = findIntegerAttribute(xmlr, "char");
48-
severity = getAttribute(xmlr, "severity");
49-
message = getAttribute(xmlr, "reason");
50-
evidence = getAttribute(xmlr, "evidence").trim();
41+
final Integer line = getIntegerAttribute(xmlr, "line");
42+
final Optional<Integer> charAttrib = findIntegerAttribute(xmlr, "char");
43+
final String severity = getAttribute(xmlr, "severity");
44+
final String message = getAttribute(xmlr, "reason");
45+
final String evidence = getAttribute(xmlr, "evidence").trim();
5146
final Violation violation =
5247
violationBuilder() //
5348
.setParser(LINT) //

0 commit comments

Comments
 (0)