Skip to content

Commit

Permalink
Merge pull request #916 from hazendaz/charset
Browse files Browse the repository at this point in the history
[encoding] Use standard charset/standardstarset instead of system to …
  • Loading branch information
hazendaz authored Oct 27, 2024
2 parents 4cedf53 + 374abf8 commit e66ac2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package org.codehaus.mojo.spotbugs

import groovy.ant.AntBuilder

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import javax.inject.Inject

import org.apache.maven.execution.MavenSession
Expand Down Expand Up @@ -138,15 +141,15 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait {

ant.java(classname: "edu.umd.cs.findbugs.LaunchAppropriateUI", fork: "true", failonerror: "true", clonevm: "true", maxmemory: "${maxHeap}m") {

String effectiveEncoding = System.getProperty("file.encoding", "UTF-8")
Charset effectiveEncoding = Charset.defaultCharset() ?: StandardCharsets.UTF_8

if (encoding) {
effectiveEncoding = encoding
effectiveEncoding = Charset.forName(encoding)
}

log.info("File Encoding is " + effectiveEncoding)
log.info("File Encoding is " + effectiveEncoding.name())

sysproperty(key: "file.encoding" , value: effectiveEncoding)
sysproperty(key: "file.encoding" , value: effectiveEncoding.name())

// spotbugs assumes that multiple arguments (because of options) means text mode, so need to request gui explicitly
jvmarg(value: "-Dfindbugs.launchUI=gui2")
Expand Down
19 changes: 10 additions & 9 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.codehaus.plexus.resource.ResourceManager
import org.codehaus.plexus.resource.loader.FileResourceLoader

import java.nio.charset.Charset
import java.nio.charset.StandardCharsets;
import java.nio.file.Files
import java.nio.file.Paths
import java.util.stream.Collectors
Expand Down Expand Up @@ -1029,7 +1030,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
forceFileCreation(sarifTempFile)
}

outputEncoding = outputEncoding ?: 'UTF-8'
outputEncoding = outputEncoding ?: StandardCharsets.UTF_8

log.debug("****** Executing SpotBugsMojo *******")

Expand Down Expand Up @@ -1063,17 +1064,17 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {

List<String> spotbugsArgs = getSpotbugsArgs(htmlTempFile, xmlTempFile, sarifTempFile)

String effectiveEncoding = System.getProperty("file.encoding", "UTF-8")
Charset effectiveEncoding = Charset.defaultCharset() ?: StandardCharsets.UTF_8

if (sourceEncoding) {
effectiveEncoding = sourceEncoding
effectiveEncoding = Charset.forName(sourceEncoding)
}

ant.java(classname: "edu.umd.cs.findbugs.FindBugs2", fork: "${fork}", failonerror: "true", clonevm: "false", timeout: "${timeout}", maxmemory: "${maxHeap}m") {

log.debug("File Encoding is " + effectiveEncoding)
log.debug("File Encoding is " + effectiveEncoding.name())

sysproperty(key: "file.encoding", value: effectiveEncoding)
sysproperty(key: "file.encoding", value: effectiveEncoding.name())

if (jvmArgs && fork) {
log.debug("Adding JVM Args => ${jvmArgs}")
Expand All @@ -1093,7 +1094,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
classpath() {

pluginArtifacts.each() { pluginArtifact ->
log.debug(" Adding to pluginArtifact ->" + pluginArtifact.file.toString())
log.debug(" Adding to pluginArtifact -> " + pluginArtifact.file.toString())

pathelement(location: pluginArtifact.file)
}
Expand Down Expand Up @@ -1163,12 +1164,12 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
outputFile.getParentFile().mkdirs()
outputFile.createNewFile()

BufferedWriter writer = Files.newBufferedWriter(outputFile.toPath(), Charset.forName(effectiveEncoding))
BufferedWriter writer = Files.newBufferedWriter(outputFile.toPath(), effectiveEncoding)

if (effectiveEncoding.equalsIgnoreCase("Cp1252")) {
if (effectiveEncoding.name().equalsIgnoreCase("Cp1252")) {
writer.write "<?xml version=\"1.0\" encoding=\"windows-1252\"?>"
} else {
writer.write "<?xml version=\"1.0\" encoding=\"" + effectiveEncoding.toLowerCase(Locale.ENGLISH) + "\"?>"
writer.write "<?xml version=\"1.0\" encoding=\"" + effectiveEncoding.name().toLowerCase(Locale.ENGLISH) + "\"?>"
}

writer.write SpotBugsInfo.EOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import edu.umd.cs.findbugs.Version
import groovy.xml.slurpersupport.GPathResult
import groovy.xml.StreamingMarkupBuilder

import java.nio.charset.StandardCharsets;

import org.apache.maven.plugin.logging.Log

/**
Expand Down Expand Up @@ -132,7 +134,7 @@ class XDocsReporter {
public void generateReport() {

StreamingMarkupBuilder xmlBuilder = new StreamingMarkupBuilder()
xmlBuilder.encoding = "UTF-8"
xmlBuilder.encoding = StandardCharsets.UTF_8.name()

def xdoc = {
mkp.xmlDeclaration()
Expand Down

0 comments on commit e66ac2c

Please sign in to comment.