Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence pmd and spotbugs warnings #93

Merged
merged 9 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.74</version>
<version>4.76</version>
<relativePath />
</parent>

Expand All @@ -17,8 +17,6 @@
<description>Parses the console log generated by a build</description>

<properties>
<!-- TODO: remove once FindBugs issues are fixed -->
<spotbugs.failOnError>false</spotbugs.failOnError>
<jenkins.version>2.387.3</jenkins.version>
<!-- Maven Plugins -->
<maven-release-plugin.version>2.5.1</maven-release-plugin.version>
Expand All @@ -38,7 +36,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.21.0</version>
<version>3.21.2</version>
<configuration>
<targetJdk>11</targetJdk>
<rulesets>
Expand Down
29 changes: 0 additions & 29 deletions src/findbugs/excludesFilter.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

public boolean isEmpty() {
return (lines[0] == null);
return lines[0] == null;

Check warning on line 29 in src/main/java/hudson/plugins/logparser/LogParserLogPart.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 29 is not covered by tests
}

}
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/logparser/LogParserParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public LogParserResult parseLog(final Run<?, ?> build) throws IOException, Inter
}

// Open console log for reading and all other files for writing
try (final BufferedWriter writer = new BufferedWriter(new FileWriter(parsedFilePath))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(parsedFilePath))) {

// Record writers to links files in hash
writers.put(LogParserConsts.ERROR, new BufferedWriter(new FileWriter(
Expand Down Expand Up @@ -350,8 +350,8 @@ private void parseLogBody(final Run<?, ?> build, final BufferedWriter writer, fi

// Read log file from start - line by line and apply the statuses as
// found by the threads.
try (final InputStreamReader streamReader = new InputStreamReader(build.getLogInputStream(), charset);
final BufferedReader reader = new BufferedReader(streamReader)) {
try (InputStreamReader streamReader = new InputStreamReader(build.getLogInputStream(), charset);
BufferedReader reader = new BufferedReader(streamReader)) {
String line;
String status;
int line_num = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

int counter = 0;
String line;
while (counter < numLines && ((line = reader.readLine()) != null)) {
while (counter < numLines && (line = reader.readLine()) != null) {

Check warning on line 27 in src/main/java/hudson/plugins/logparser/LogParserReader.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 27 is not covered by tests
lines[counter++] = line;
}
logger.log(Level.INFO, "Done reading log part " + logPartNum);
Expand Down
25 changes: 10 additions & 15 deletions src/main/java/hudson/plugins/logparser/LogParserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@
}

public static boolean skipParsingRule(final String parsingRule) {
boolean skip = false;
if (parsingRule == null || parsingRule.equals("")
|| parsingRule.charAt(0) == '#'
|| parsingRule.startsWith("\\s")
|| parsingRule.startsWith("\r") || // Carriage return
parsingRule.contains("$header")) { // for now - disregard rules
// with header in them
skip = true;
}
return skip;
return parsingRule == null || parsingRule.equals("")

Check warning on line 27 in src/main/java/hudson/plugins/logparser/LogParserUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 27 is only partially covered, one branch is missing
|| parsingRule.charAt(0) == '#'
|| parsingRule.startsWith("\\s")
|| parsingRule.startsWith("\r") // Carriage return

Check warning on line 30 in src/main/java/hudson/plugins/logparser/LogParserUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 30 is only partially covered, one branch is missing
|| parsingRule.contains("$header"); // for now - disregard rules

Check warning on line 31 in src/main/java/hudson/plugins/logparser/LogParserUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 31 is only partially covered, one branch is missing
// with header in them
}

public static String standardizeStatus(final String status) {
Expand Down Expand Up @@ -75,10 +71,9 @@
final String ruleParts[] = parsingRule.split("\\s");
String regexp = ruleParts[1];
String tag = ruleParts[0];
if (!LogParserConsts.LEGAL_STATUS.contains(tag.toUpperCase(Locale.ENGLISH))) {
if (!Arrays.asList("OK", "END", "WARN").contains(tag.toUpperCase(Locale.ENGLISH))) {
extraTags.add(tag);
}
if (!LogParserConsts.LEGAL_STATUS.contains(tag.toUpperCase(Locale.ENGLISH))

Check warning on line 74 in src/main/java/hudson/plugins/logparser/LogParserUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 74 is only partially covered, one branch is missing
&& !Arrays.asList("OK", "END", "WARN").contains(tag.toUpperCase(Locale.ENGLISH))) {
extraTags.add(tag);
}

final int firstDash = parsingRule.indexOf('/');
Expand Down Expand Up @@ -145,7 +140,7 @@
}

public static int countLines(final String filename) throws IOException {
try (final LineNumberReader reader = new LineNumberReader(new FileReader(filename))) {
try (LineNumberReader reader = new LineNumberReader(new FileReader(filename))) {

Check warning on line 143 in src/main/java/hudson/plugins/logparser/LogParserUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 143 is not covered by tests
int count = 0;
while (reader.readLine() != null) {
// Read the whole file to count the lines.
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/hudson/plugins/logparser/LogParserWriter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hudson.plugins.logparser;

import hudson.Functions;
import jenkins.model.Jenkins;

import java.io.BufferedReader;
Expand Down Expand Up @@ -36,7 +35,7 @@ public static void writeWrapperHtml(final String buildWrapperPath)
+ "<a href='build.log'>build log</a>\n" + "</noframes>\n"
+ "</frameset>\n";

try (final BufferedWriter writer = new BufferedWriter(new FileWriter(buildWrapperPath))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(buildWrapperPath))) {
writer.write(wrapperHtml);
}
}
Expand All @@ -57,7 +56,7 @@ public static void writeReferenceHtml(final String buildRefPath,
+ "\t\telement.display == 'none' ? element.display='block' : element.display='none';\n"
+ "\t}\n" + "</script>\n";

try (final BufferedWriter writer = new BufferedWriter(new FileWriter(buildRefPath))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(buildRefPath))) {
// Hudson stylesheets
writer.write(LogParserConsts.getHtmlOpeningTags());
writer.write(refStart); // toggle links javascript
Expand Down Expand Up @@ -110,7 +109,7 @@ private static void writeLinks(final BufferedWriter writer,
final String linkListCount = statusCount.get(status).toString();

final String hudsonRoot = Jenkins.get().getRootUrl();
final String iconLocation = String.format("%s/plugin/log-parser/images/", jenkins.model.Jenkins.RESOURCE_PATH).substring(1);
final String iconLocation = String.format("%s/plugin/log-parser/images/", Jenkins.RESOURCE_PATH).substring(1);

final String styles =
"<style>\n"
Expand All @@ -131,7 +130,7 @@ private static void writeLinks(final BufferedWriter writer,
writer.write(linksStart);

// Read the links file and insert here
try (final BufferedReader reader = new BufferedReader(new FileReader(linkFiles.get(status)))) {
try (BufferedReader reader = new BufferedReader(new FileReader(linkFiles.get(status)))) {
final String summaryLine = "<br/>(SUMMARY_INT_HERE LINK_LIST_DISPLAY_STR in this section)<br/>";

final String headerTemplateRegexp = "HEADER HERE:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,42 @@
static public String getContents(final File aFile) {
final StringBuilder contents = new StringBuilder();

try (final BufferedReader input = new BufferedReader(new FileReader(aFile))) {
try (BufferedReader input = new BufferedReader(new FileReader(aFile))) {
String line = null; // not declared within while loop
while ((line = input.readLine()) != null) {
contents.append(line).append("\n");
}
} catch (IOException ex) {
LOGGER.warning("Failure reading from " + aFile.getPath());
}

return contents.toString();
}

static public void setContents(final File aFile, final String aContents)
throws FileNotFoundException, IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
boolean created = aFile.createNewFile();
if (created) {
LOGGER.fine(aFile.getPath() + " created");
} else {
LOGGER.fine(aFile.getPath() + " already exists");
}
}
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: "
+ aFile);
}
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: "
+ aFile);
}

// use buffering
try (final Writer output = new BufferedWriter(new FileWriter(aFile))) {
try (Writer output = new BufferedWriter(new FileWriter(aFile))) {

Check warning on line 58 in src/main/java/hudson/plugins/logparser/ReadWriteTextFile.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 23-58 are not covered by tests
// FileWriter always assumes default encoding is OK!
output.write(aContents);
}
Expand Down
38 changes: 38 additions & 0 deletions src/spotbugs/excludesFilter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<FindBugsFilter>
<!--
Exclusions in this section have been triaged and determined to be
false positives.
-->
<Match>
<Bug pattern="MS_MUTABLE_COLLECTION_PKGPROTECT" />
<Class name="hudson.plugins.logparser.LogParserConsts" />
<Or>
<Field name="LEGAL_STATUS" />
<Field name="STATUSES_WITH_SECTIONS_IN_LINK_FILES" />
</Or>
</Match>

<!--
Here lies technical debt. Exclusions in this section have not yet
been triaged. When working on this section, pick an exclusion to
triage, then:

- Add a @SuppressFBWarnings(value = "[...]", justification = "[...]")
annotation if it is a false positive. Indicate the reason why
it is a false positive, then remove the exclusion from this
section.

- If it is not a false positive, fix the bug, then remove the
exclusion from this section.
-->
<Match>
<Bug pattern="DM_DEFAULT_ENCODING" />
<Or>
<Class name="hudson.plugins.logparser.LogParserParser" />
<Class name="hudson.plugins.logparser.LogParserResult" />
<Class name="hudson.plugins.logparser.LogParserUtils" />
<Class name="hudson.plugins.logparser.LogParserWriter" />
<Class name="hudson.plugins.logparser.ReadWriteTextFile" />
</Or>
</Match>
</FindBugsFilter>
Loading