Skip to content

Commit

Permalink
Merge pull request #743 from hazendaz/master
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
hazendaz authored Dec 27, 2023
2 parents ef6dd73 + b3bcb2b commit 6ae14e7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,13 @@ abstract class BaseViolationCheckMojo extends AbstractMojo {
}

log.info('Total bugs: ' + bugCount)
int bugCountAboveThreshold = 0

int priorityThresholdNum = failThreshold ? SpotBugsInfo.spotbugsPriority.indexOf(failThreshold) : Integer.MAX_VALUE
if (priorityThresholdNum == -1) {
throw new MojoExecutionException("Invalid value for failThreshold: ${failThreshold}")
}

int bugCountAboveThreshold = 0
for (i in 0..bugCount-1) {
def bug = bugs[i]
int priorityNum = bug.'@priority' as Integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ final class ResourceHelper {
}
}

InputStream is = new BufferedInputStream(resourceManager.getResourceAsInputStream(name))
try (InputStream is = new BufferedInputStream(resourceManager.getResourceAsInputStream(name))) {

try {
if (!outputResourceFile.getParentFile().exists()) {
outputResourceFile.getParentFile().mkdirs()
}
Expand All @@ -118,8 +117,6 @@ final class ResourceHelper {

} catch (IOException e) {
throw new FileResourceCreationException("Cannot create file-based resource.", e)
} finally {
is.close()
}

return outputResourceFile
Expand Down
39 changes: 21 additions & 18 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SourceFileIndexer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,31 @@ class SourceFileIndexer {
*/
private void scanDirectory(File directory,List<String> files,String baseDirectory) {

if (directory.exists()) {
for (File child : directory.listFiles()) {
if (child.isDirectory()) {
scanDirectory(child, files, baseDirectory)
if (!directory.exists()) {
return
}

for (File child : directory.listFiles()) {
if (child.isDirectory()) {
scanDirectory(child, files, baseDirectory)
} else {
String newSourceFile = normalizePath(child.canonicalPath)
if (newSourceFile.startsWith(baseDirectory)) {
// The project will not be at the root of our file system.
// It will most likely be stored in a work directory.
// Example: /work/project-code-to-scan/src/main/java/File.java => src/main/java/File.java
// (Here baseDirectory is /work/project-code-to-scan/)
String relativePath = Paths.get(baseDirectory).relativize(Paths.get(newSourceFile))
files.add(normalizePath(relativePath))
} else {
String newSourceFile = normalizePath(child.canonicalPath)
if (newSourceFile.startsWith(baseDirectory)) {
// The project will not be at the root of our file system.
// It will most likely be stored in a work directory.
// Example: /work/project-code-to-scan/src/main/java/File.java => src/main/java/File.java
// (Here baseDirectory is /work/project-code-to-scan/)
String relativePath = Paths.get(baseDirectory).relativize(Paths.get(newSourceFile))
files.add(normalizePath(relativePath))
} else {
// Use the full path instead:
// This will occurs in many cases including when the pom.xml is
// not in the same directory tree as the sources.
files.add(newSourceFile)
}
// Use the full path instead:
// This will occurs in many cases including when the pom.xml is
// not in the same directory tree as the sources.
files.add(newSourceFile)
}
}
}

}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait {

List<String> auxClasspathElements = project.compileClasspathElements

if ( debug ) {
if (debug) {
log.debug(" Plugin Artifacts to be added ->" + pluginArtifacts.toString())
}

Expand All @@ -200,7 +200,7 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait {

String effectiveEncoding = System.getProperty("file.encoding", "UTF-8")

if ( encoding ) {
if (encoding) {
effectiveEncoding = encoding
}

Expand Down Expand Up @@ -228,15 +228,15 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait {
String spotbugsXmlName = spotbugsXmlOutputDirectory.toString() + "/" + spotbugsXmlOutputFilename
File spotbugsXml = new File(spotbugsXmlName)

if ( spotbugsXml.exists() ) {
if (spotbugsXml.exists()) {
log.debug(" Found an SpotBugs XML at ->" + spotbugsXml.toString())
arg(value: spotbugsXml)
}

classpath() {

pluginArtifacts.each() { pluginArtifact ->
if ( debug ) {
if (debug) {
log.debug(" Trying to Add to pluginArtifact ->" + pluginArtifact.file.toString())
}

Expand Down
41 changes: 21 additions & 20 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -765,26 +765,28 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
private void generateXDoc(Locale locale) {
log.debug("****** SpotBugsMojo generateXDoc *******")

if (outputSpotbugsFile != null && outputSpotbugsFile.exists()) {
if (outputSpotbugsFile == null || !outputSpotbugsFile.exists()) {
return
}

log.debug("xmlOutput is ${xmlOutput}")
log.debug("xmlOutput is ${xmlOutput}")

if (xmlOutput) {
log.debug(" Using the xdoc format")
if (xmlOutput) {
log.debug(" Using the xdoc format")

if (!xmlOutputDirectory.exists() && !xmlOutputDirectory.mkdirs()) {
throw new MojoExecutionException("Cannot create xdoc output directory")
}
if (!xmlOutputDirectory.exists() && !xmlOutputDirectory.mkdirs()) {
throw new MojoExecutionException("Cannot create xdoc output directory")
}

XDocsReporter xDocsReporter = new XDocsReporter(getBundle(locale), log, threshold, effort, outputEncoding)
xDocsReporter.setOutputWriter(Files.newBufferedWriter(Paths.get("${xmlOutputDirectory}/spotbugs.xml"), Charset.forName(outputEncoding)))
xDocsReporter.setSpotbugsResults(new XmlSlurper().parse(outputSpotbugsFile))
xDocsReporter.setCompileSourceRoots(this.compileSourceRoots)
xDocsReporter.setTestSourceRoots(this.testSourceRoots)
XDocsReporter xDocsReporter = new XDocsReporter(getBundle(locale), log, threshold, effort, outputEncoding)
xDocsReporter.setOutputWriter(Files.newBufferedWriter(Paths.get("${xmlOutputDirectory}/spotbugs.xml"), Charset.forName(outputEncoding)))
xDocsReporter.setSpotbugsResults(new XmlSlurper().parse(outputSpotbugsFile))
xDocsReporter.setCompileSourceRoots(this.compileSourceRoots)
xDocsReporter.setTestSourceRoots(this.testSourceRoots)

xDocsReporter.generateReport()
}
xDocsReporter.generateReport()
}

}

/**
Expand Down Expand Up @@ -1094,20 +1096,16 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
private void executeSpotbugs(File outputFile) {

log.debug("****** SpotBugsMojo executeSpotbugs *******")
long startTime
long duration

File htmlTempFile = new File("${project.build.directory}/spotbugs.html")
File xmlTempFile = new File("${project.build.directory}/spotbugsTemp.xml")
File sarifTempFile = new File("${project.build.directory}/spotbugsTempSarif.json")
File sarifFinalFile = new File(sarifOutputDirectory, sarifOutputFilename)

if (htmlOutput) {
forceFileCreation(htmlTempFile)
}

File xmlTempFile = new File("${project.build.directory}/spotbugsTemp.xml")
forceFileCreation(xmlTempFile)

File sarifTempFile = new File("${project.build.directory}/spotbugsTempSarif.json")
if (sarifOutput) {
forceFileCreation(sarifTempFile)
}
Expand Down Expand Up @@ -1139,6 +1137,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {

log.info("Fork Value is ${fork}")

long startTime
if (log.isDebugEnabled()) {
startTime = System.nanoTime()
}
Expand Down Expand Up @@ -1193,6 +1192,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {

}

long duration
if (log.isDebugEnabled()) {
duration = (System.nanoTime() - startTime) / 1000000000.00
log.debug("SpotBugs duration is ${duration}")
Expand Down Expand Up @@ -1302,6 +1302,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
}
}

File sarifFinalFile = new File(sarifOutputDirectory, sarifOutputFilename)
forceFileCreation(sarifFinalFile)

sarifFinalFile.withWriter {
Expand Down

0 comments on commit 6ae14e7

Please sign in to comment.