Skip to content

Commit

Permalink
Apply recipes of org.openrewrite.staticanalysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Nov 19, 2024
1 parent 8111479 commit f3f24a9
Show file tree
Hide file tree
Showing 51 changed files with 77 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.InvalidPathException;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import javax.xml.stream.XMLStreamReader;

import org.apache.commons.io.input.BOMInputStream;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.hm.hafner.analysis;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.InvalidPathException;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -76,7 +76,7 @@ private String parseGradleSettings(final String settingsFile) {
String name = null;

try (var input = getFactory().open(settingsFile);
var scan = new Scanner(input, "UTF-8")) {
var scan = new Scanner(input, StandardCharsets.UTF_8)) {
while (scan.hasNextLine()) {
var line = scan.findInLine(RE_GRADLE_SET_PROJECT_NAME);

Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/hm/hafner/analysis/IssueBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;

import edu.hm.hafner.util.LineRange;
import edu.hm.hafner.util.LineRangeList;
import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.TreeString;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.InvalidPathException;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.InvalidPathException;
import java.util.List;
import java.util.Map;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/hm/hafner/analysis/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import edu.hm.hafner.util.Generated;
import edu.hm.hafner.util.LineRangeList;
import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.TreeString;
import edu.hm.hafner.util.TreeStringBuilder;
import edu.hm.hafner.util.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/hm/hafner/analysis/Severity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis.parser;

import java.io.Serial;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ protected void parseJsonObject(final Report report, final JSONObject jsonReport,

private void parseResources(final Report report, final JSONArray resources, final IssueBuilder issueBuilder) {
for (int i = 0; i < resources.length(); i++) {
final var item = resources.get(i);
if (item instanceof JSONObject resourceWrapper) {
if (!resourceWrapper.isNull("vulnerabilities") && !resourceWrapper.isNull("resource")) {
var resource = resourceWrapper.getJSONObject("resource");
parseVulnerabilities(report, issueBuilder, resourceWrapper, resource);
}
var item = resources.get(i);
if (item instanceof JSONObject resourceWrapper
&& !resourceWrapper.isNull("vulnerabilities")
&& !resourceWrapper.isNull("resource")) {
var resource = resourceWrapper.getJSONObject("resource");
parseVulnerabilities(report, issueBuilder, resourceWrapper, resource);
}
}
}
Expand All @@ -69,7 +69,8 @@ private Issue convertToIssue(final JSONObject resource, final JSONObject vulnera
}

private Severity mapSeverity(final String string) {
if (StringUtils.containsAnyIgnoreCase(string, AQUA_VULNERABILITY_LEVEL_TAG_LOW, AQUA_VULNERABILITY_LEVEL_TAG_NEGLIGIBLE)) {
if (StringUtils.containsAnyIgnoreCase(string, AQUA_VULNERABILITY_LEVEL_TAG_LOW,
AQUA_VULNERABILITY_LEVEL_TAG_NEGLIGIBLE)) {
return Severity.WARNING_LOW;
}
else if (StringUtils.equalsIgnoreCase(string, AQUA_VULNERABILITY_LEVEL_TAG_MEDIUM)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serial;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serial;
import java.io.UncheckedIOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/edu/hm/hafner/analysis/parser/FlowParser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis.parser;

import java.io.IOException;
import java.io.Reader;
import java.io.Serial;
import java.util.Optional;

Expand Down Expand Up @@ -49,7 +48,7 @@ public class FlowParser extends JsonIssueParser {
public boolean accepts(final ReaderFactory readerFactory) {
try (var reader = readerFactory.create()) {
var value = new JSONTokener(reader).nextValue();
return value instanceof JSONObject jsono && jsono.has(FLOW_VERSION);
return value instanceof final JSONObject jsono && jsono.has(FLOW_VERSION);
}
catch (IOException | JSONException ignored) {
return false;
Expand All @@ -66,7 +65,7 @@ protected void parseJsonObject(final Report report, final JSONObject jsonReport,
private void extractIssues(final JSONArray elements, final Report report,
final IssueBuilder issueBuilder) {
for (Object object : elements) {
if (object instanceof JSONObject issue) {
if (object instanceof final JSONObject issue) {
findFirstMessage(issue).ifPresent(
jsonObject -> report.add(createIssueFromJsonObject(issue, jsonObject, issueBuilder)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serial;

import org.json.JSONArray;
import org.json.JSONObject;

import edu.hm.hafner.analysis.Issue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.IssueParser;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/edu/hm/hafner/analysis/parser/JsonParser.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package edu.hm.hafner.analysis.parser;

import java.io.IOException;
import java.io.Reader;
import java.io.Serial;
import java.util.Optional;
import java.util.stream.StreamSupport;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serial;

import org.json.JSONArray;
import org.json.JSONObject;

import edu.hm.hafner.analysis.Issue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;
import java.util.Optional;

import org.json.JSONArray;
import org.json.JSONObject;

import edu.hm.hafner.analysis.Issue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package edu.hm.hafner.analysis.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serial;
import java.util.regex.Pattern;

Expand Down
32 changes: 12 additions & 20 deletions src/main/java/edu/hm/hafner/analysis/parser/SonarQubeParser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis.parser;

import java.io.IOException;
import java.io.Reader;
import java.io.Serial;

import org.json.JSONArray;
Expand Down Expand Up @@ -94,10 +93,8 @@ protected void parseJsonObject(final Report report, final JSONObject jsonReport,

private void extractIssues(final JSONArray elements, final Report report, final IssueBuilder issueBuilder) {
for (Object object : elements) {
if (object instanceof JSONObject issue) {
if (filterIssue(issue)) {
report.add(createIssueFromJsonObject(issue, issueBuilder));
}
if (object instanceof final JSONObject issue && filterIssue(issue)) {
report.add(createIssueFromJsonObject(issue, issueBuilder));
}
}
}
Expand Down Expand Up @@ -231,8 +228,7 @@ private String parseMessage(final JSONObject issue) {
* @return the priority.
*/
private Severity parsePriority(final JSONObject issue) {
var severity = issue.optString(ISSUE_SEVERITY, null);
return severityToPriority(severity);
return severityToPriority(issue.optString(ISSUE_SEVERITY, SEVERITY_MINOR));
}

//UTILITIES
Expand Down Expand Up @@ -271,10 +267,9 @@ String parseModulePath(final JSONObject moduleKeyObject, final String componentK
private JSONObject findComponentByKey(final String key) {
if (components != null && key != null) {
for (Object component : components) {
if (component instanceof JSONObject jsonComponent) {
if (key.equals(jsonComponent.optString(COMPONENT_KEY))) {
return jsonComponent;
}
if (component instanceof final JSONObject jsonComponent
&& key.equals(jsonComponent.optString(COMPONENT_KEY))) {
return jsonComponent;
}
}
}
Expand All @@ -298,16 +293,13 @@ private JSONObject findComponentByKey(final String key) {
* @return a priority object corresponding to the passed severity.
*/
private Severity severityToPriority(final String severity) {
var priority = Severity.WARNING_NORMAL;
// Severity MAJOR is omitted as it corresponds with default Severity: NORMAL
if (severity != null) {
if (SEVERITY_BLOCKER.equals(severity) || SEVERITY_CRITICAL.equals(severity)) {
priority = Severity.WARNING_HIGH;
}
else if (SEVERITY_MINOR.equals(severity) || SEVERITY_INFO.equals(severity)) {
priority = Severity.WARNING_LOW;
}
if (SEVERITY_BLOCKER.equals(severity) || SEVERITY_CRITICAL.equals(severity)) {
return Severity.WARNING_HIGH;
}
else if (SEVERITY_MINOR.equals(severity) || SEVERITY_INFO.equals(severity)) {
return Severity.WARNING_LOW;
}
return priority;
return Severity.WARNING_NORMAL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ protected void parseJsonObject(final Report report, final JSONObject jsonReport,

private void parseResources(final Report report, final JSONArray packages, final IssueBuilder issueBuilder) {
for (int i = 0; i < packages.length(); i++) {
final var item = packages.get(i);
if (item instanceof JSONObject resourceWrapper) {
if (!resourceWrapper.isNull("issue")) {
parseVulnerabilities(report, issueBuilder, resourceWrapper);
}
var item = packages.get(i);
if (item instanceof JSONObject resourceWrapper && !resourceWrapper.isNull("issue")) {
parseVulnerabilities(report, issueBuilder, resourceWrapper);
}
}
}

private void parseVulnerabilities(final Report report, final IssueBuilder issueBuilder,
final JSONObject resourceWrapper) {
final var vulnerabilities = resourceWrapper.getJSONArray("issue");
var vulnerabilities = resourceWrapper.getJSONArray("issue");
for (Object vulnerability : vulnerabilities) {
if (vulnerability instanceof JSONObject obj) {
var status = obj.getString("status");
Expand All @@ -70,12 +68,15 @@ private Issue convertToIssue(final JSONObject resource, final JSONObject vulnera
}

private Severity mapSeverity(final JSONObject vulnerability) {
var score = INVALID_SCORE;
boolean hasScoreV3 = vulnerability.has("scorev3");

double score;
if (hasScoreV3) {
score = vulnerability.getDouble("scorev3");
}
else {
score = INVALID_SCORE;
}

if (score <= 0) {
score = vulnerability.getDouble("scorev2");
Expand All @@ -94,7 +95,8 @@ else if (score >= 7.0 && score <= 10.0) {
return Severity.ERROR;
}

private String formatDescription(final String packageName, final JSONObject resource, final JSONObject vulnerability) {
private String formatDescription(final String packageName, final JSONObject resource,
final JSONObject vulnerability) {
final var version = resource.optString("version", VALUE_NOT_SET);
final var layer = resource.optString("layer", "UNKOWN");
final var vector = vulnerability.optString("vector", "UNKOWN");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis.parser.checkstyle;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis.parser.dry;

import java.io.IOException;
import java.io.Reader;
import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.commons.digester3.Digester;

import edu.hm.hafner.analysis.DuplicationGroup;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.parser.dry.AbstractDryParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.commons.digester3.Digester;

import edu.hm.hafner.analysis.DuplicationGroup;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.parser.dry.AbstractDryParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.commons.digester3.Digester;

import edu.hm.hafner.analysis.DuplicationGroup;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.parser.dry.AbstractDryParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.digester3.Digester;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -247,7 +246,7 @@ private void setAffectedLines(final BugInstance warning, final IssueBuilder buil
var lineRanges = new LineRangeList();
while (annotationIterator.hasNext()) {
var bugAnnotation = annotationIterator.next();
if (bugAnnotation instanceof SourceLineAnnotation annotation) {
if (bugAnnotation instanceof final SourceLineAnnotation annotation) {
var lineRange = new LineRange(annotation.getStartLine(), annotation.getEndLine());
if (!lineRanges.contains(lineRange) && !primary.equals(lineRange)) {
lineRanges.add(lineRange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hm.hafner.analysis.parser.jcreport;

import java.io.IOException;
import java.io.Reader;
import java.io.Serial;

import org.xml.sax.SAXException;
Expand Down
Loading

0 comments on commit f3f24a9

Please sign in to comment.