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

[encoding] Use standard charset/standardstarset instead of system to … #916

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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