Skip to content

Commit

Permalink
chore: static code analsis (PMD) fixes and stepping dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jan 29, 2024
1 parent f87f42f commit cf29987
Show file tree
Hide file tree
Showing 27 changed files with 115 additions and 151 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}
dependencies {
classpath 'se.bjurr.gradle:gradle-scripts:2.+'
classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.2'
classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.2.1'
}
}
project.ext.buildConfig = [
Expand Down Expand Up @@ -42,8 +42,8 @@ dependencies {
api 'com.google.code.gson:gson:2.10.1'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.assertj:assertj-core:3.24.1'
testImplementation 'uk.co.jemos.podam:podam:7.2.11.RELEASE'
testImplementation 'com.approvaltests:approvaltests:18.5.0'
testImplementation 'com.networknt:json-schema-validator:1.0.76'
testImplementation 'org.assertj:assertj-core:3.25.2'
testImplementation 'uk.co.jemos.podam:podam:8.0.1.RELEASE'
testImplementation 'com.approvaltests:approvaltests:22.3.3'
testImplementation 'com.networknt:json-schema-validator:1.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.logging.Logger;

public final class FilteringViolationsLogger implements ViolationsLogger {
private static Logger LOG = Logger.getLogger(ViolationsApi.class.getSimpleName());
private static final Logger LOGGER = Logger.getLogger(ViolationsApi.class.getSimpleName());
private final ViolationsLogger violationsLogger;

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

@Override
public void log(final Level level, final String string) {
if (LOG.isLoggable(level)) {
if (LOGGER.isLoggable(level)) {
this.violationsLogger.log(level, string);
}
}

@Override
public void log(final Level level, final String string, final Throwable t) {
if (LOG.isLoggable(level)) {
if (LOGGER.isLoggable(level)) {
this.violationsLogger.log(level, string, t);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/bjurr/violations/lib/ViolationsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import se.bjurr.violations.lib.reports.ViolationsFinder;

@SuppressFBWarnings("CRLF_INJECTION_LOGS")
public class ViolationsApi {
public final class ViolationsApi {
private final Logger LOGGER = Logger.getLogger(ViolationsApi.class.getSimpleName());
private String pattern;
private ViolationsParser violationsParser;
Expand Down
71 changes: 36 additions & 35 deletions src/main/java/se/bjurr/violations/lib/model/Violation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,45 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import se.bjurr.violations.lib.parsers.CPPCheckParser;
import se.bjurr.violations.lib.reports.Parser;

public class Violation implements Serializable, Comparable<Violation> {

private static final long serialVersionUID = -6052921679385466168L;
/** A {@link #file} used when there is no file specified in the parsed report. */
public static final String NO_FILE = "-";
/** A {@link #startLine} used when there is no line specified in the parsed report. */
public static final Integer NO_LINE = 0;

private Integer column;
private final Integer endLine;
private final Integer endColumn;
/** Also see {@link #NO_FILE} */
private final String file;

private final String message;
/** The algorithm, the format, used. */
private final Parser parser;
/**
* Intended as the tool used to find the violation. Like Detekt, when it is being used to find
* violations and report them in the {@link Parser#CHECKSTYLE} format.
*/
private String reporter;

private final String rule;
private final String category;
/**
* Something that identifies a group that this violation belongs to. First introduced with {@link
* se.bjurr.violations.lib.parsers.CPPCheckParser} to record what error tag each violation belongs
* to.
*/
private final String group;

private final SEVERITY severity;
private final String source;
private final Map<String, String> specifics;
private final Integer startLine;

public static class ViolationBuilder {

private Integer column;
Expand Down Expand Up @@ -116,44 +151,10 @@ public ViolationBuilder setGroup(final String group) {
}
}

private static final long serialVersionUID = -6052921679385466168L;
/** A {@link #file} used when there is no file specified in the parsed report. */
public static final String NO_FILE = "-";
/** A {@link #startLine} used when there is no line specified in the parsed report. */
public static final Integer NO_LINE = 0;

public static ViolationBuilder violationBuilder() {
return new ViolationBuilder();
}

private Integer column;
private final Integer endLine;
private final Integer endColumn;
/** Also see {@link #NO_FILE} */
private final String file;

private final String message;
/** The algorithm, the format, used. */
private final Parser parser;
/**
* Intended as the tool used to find the violation. Like Detekt, when it is being used to find
* violations and report them in the {@link Parser#CHECKSTYLE} format.
*/
private String reporter;

private final String rule;
private final String category;
/**
* Something that identifies a group that this violation belongs to. First introduced with {@link
* CPPCheckParser} to record what error tag each violation belongs to.
*/
private final String group;

private final SEVERITY severity;
private final String source;
private final Map<String, String> specifics;
private final Integer startLine;

public Violation() {
this.startLine = null;
this.endLine = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
public class AnsibleLaterParser implements ViolationsParser {

public class AnsibleLaterEntry {
public AnsibleLaterEntry() {}

public String asctime;
public String levelname;
public String message;
Expand All @@ -28,6 +26,8 @@ public AnsibleLaterEntry() {}
public String later_sid;
public Integer later_lineno;
public String later_message;

public AnsibleLaterEntry() {}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Set<Violation> parseReportOutput(

private String constructMessage(
final String msg, final Optional<String> verbose, final Optional<String> info) {
String message = "";
String message;
if (verbose.orElse("").startsWith(msg)) {
message = verbose.get();
} else {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/se/bjurr/violations/lib/parsers/CSSLintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,18 @@ public Set<Violation> parseReportOutput(
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
String filename = null;
Integer line = null;
Optional<Integer> charAttrib = null;
String severity = null;
String message = null;
String evidence = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
filename = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
line = findIntegerAttribute(xmlr, "line").orElse(1);
charAttrib = findIntegerAttribute(xmlr, "char");
severity = getAttribute(xmlr, "severity");
message = getAttribute(xmlr, "reason");
evidence = findAttribute(xmlr, "evidence").orElse("").trim();
final Integer line = findIntegerAttribute(xmlr, "line").orElse(1);
final Optional<Integer> charAttrib = findIntegerAttribute(xmlr, "char");
final String severity = getAttribute(xmlr, "severity");
final String message = getAttribute(xmlr, "reason");
final String evidence = findAttribute(xmlr, "evidence").orElse("").trim();
final Violation violation =
violationBuilder() //
.setParser(CSSLINT) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public Set<Violation> parseReportOutput(

String path = null;
String name = null;
String ruleName = null;
Integer priority = null;
Integer lineNumber = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
Expand All @@ -65,18 +62,18 @@ public Set<Violation> parseReportOutput(
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("Violation")) {
ruleName = getAttribute(xmlr, "ruleName");
priority = getIntegerAttribute(xmlr, "priority");
final String ruleName = getAttribute(xmlr, "ruleName");
final Integer priority = getIntegerAttribute(xmlr, "priority");
final String lineNumberString = getAttribute(xmlr, "lineNumber");
lineNumber = 1;
int lineNumber = 1;
if (!lineNumberString.isEmpty()) {
lineNumber = Integer.parseInt(lineNumberString);
}
String message = rules.get(ruleName);
if (message == null) {
message = ruleName;
}
String fileString = null;
String fileString;
if (sourceDirectory.isEmpty()) {
fileString = path + "/" + name;
} else {
Expand Down Expand Up @@ -119,15 +116,14 @@ private Map<String, String> getRules(final String string) throws IOException, XM
try (InputStream input = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8))) {
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
String name = null;
String description = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("Rule")) {
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("Description")) {
description = xmlr.getElementText().trim();
final String description = xmlr.getElementText().trim();
rules.put(name, description);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public Set<Violation> parseReportOutput(
for (final String rawLineToParse : lines) {
@SuppressWarnings("unchecked")
final Map<String, Object> parsedMap = new Gson().fromJson(rawLineToParse, Map.class);
;
final String message = (String) parsedMap.get("message");
final String messageSeverity = (String) parsedMap.get("message_severity");
final String fileEncoded = (String) parsedMap.get("file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Set<Violation> parseReportOutput(
getLines(string, "([^:]*):(\\d+)?:?(\\d+)?:? \\[?(\\D+)(\\d*)\\]? (.*)");
for (final List<String> parts : partsPerLine) {
final String filename = parts.get(1);
Integer line = null;
Integer line;
try {
line = parseInt(parts.get(2));
} catch (final Exception e) {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,18 @@ public Set<Violation> parseReportOutput(
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);

String name = null;
String findingType = null;
Integer line = null;
String message = null;
String origin = null;
String severity = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("item")) {
findingType = getAttribute(xmlr, "finding-type");
line = getIntegerAttribute(xmlr, "line");
message = getAttribute(xmlr, "message");
origin = getAttribute(xmlr, "origin");
severity = getAttribute(xmlr, "severity");
final String findingType = getAttribute(xmlr, "finding-type");
final Integer line = getIntegerAttribute(xmlr, "line");
final String message = getAttribute(xmlr, "message");
final String origin = getAttribute(xmlr, "origin");
final String severity = getAttribute(xmlr, "severity");
final Violation violation =
violationBuilder() //
.setParser(JCREPORT) //
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/se/bjurr/violations/lib/parsers/JSLintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,19 @@ public Set<Violation> parseReportOutput(
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
String filename = null;
Integer line = null;
Integer charAttrib = null;
String severity = null;
String message = null;
String evidence = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
filename = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
line = getIntegerAttribute(xmlr, "line");
charAttrib = getIntegerAttribute(xmlr, "char");
severity = getAttribute(xmlr, "severity");
final Integer line = getIntegerAttribute(xmlr, "line");
final Integer charAttrib = getIntegerAttribute(xmlr, "char");
final String severity = getAttribute(xmlr, "severity");
final String reason = getAttribute(xmlr, "reason").trim();
evidence = getAttribute(xmlr, "evidence").trim();
message = reason + ": " + evidence;
final String evidence = getAttribute(xmlr, "evidence").trim();
final String message = reason + ": " + evidence;
final Violation violation =
violationBuilder()
.setParser(JSLINT) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private FileAndLine findFilePathInContent(final String failureContent, final Str
fl.line = Integer.parseInt(matcher.group(3));
found.add(fl);
}
if (found.size() == 0) {
if (found.isEmpty()) {
return null;
}
if (found.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public Set<Violation> parseReportOutput(
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
switch (eventType) {
switch (eventType) { // NOPMD
case XMLStreamConstants.START_ELEMENT:
switch (xmlr.getLocalName()) {
switch (xmlr.getLocalName()) { // NOPMD
case "package":
builder.setPackageDetails(getAttribute(xmlr, "name"));
break;
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/se/bjurr/violations/lib/parsers/LintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,18 @@ public Set<Violation> parseReportOutput(
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
String filename = null;
Integer line = null;
Optional<Integer> charAttrib = null;
String severity = null;
String message = null;
String evidence = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
filename = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
line = getIntegerAttribute(xmlr, "line");
charAttrib = findIntegerAttribute(xmlr, "char");
severity = getAttribute(xmlr, "severity");
message = getAttribute(xmlr, "reason");
evidence = getAttribute(xmlr, "evidence").trim();
final Integer line = getIntegerAttribute(xmlr, "line");
final Optional<Integer> charAttrib = findIntegerAttribute(xmlr, "char");
final String severity = getAttribute(xmlr, "severity");
final String message = getAttribute(xmlr, "reason");
final String evidence = getAttribute(xmlr, "evidence").trim();
final Violation violation =
violationBuilder() //
.setParser(LINT) //
Expand Down
Loading

0 comments on commit cf29987

Please sign in to comment.