diff options
8 files changed, 32 insertions, 61 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index 8185c7828a1..2f75323511b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -31,7 +31,6 @@ import org.sonar.api.resources.Language; import org.sonar.api.resources.ResourceType; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.web.Footer; -import org.sonar.api.web.NavigationSection; import org.sonar.api.web.Page; import org.sonar.api.web.RubyRailsWebservice; import org.sonar.api.web.Widget; diff --git a/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java b/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java index 1873448fb96..652dd4cf814 100644 --- a/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java +++ b/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java @@ -19,15 +19,17 @@ */ package org.sonar.application; +import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.sonar.process.ConfigurationUtils; import org.sonar.process.ProcessConstants; import org.sonar.process.Props; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.URISyntaxException; import java.util.Properties; @@ -84,11 +86,8 @@ class PropsBuilder { Properties p = new Properties(); File propsFile = new File(homeDir, "conf/sonar.properties"); if (propsFile.exists()) { - FileReader reader = new FileReader(propsFile); - try { + try (Reader reader = new InputStreamReader(new FileInputStream(propsFile), Charsets.UTF_8)) { p.load(reader); - } finally { - IOUtils.closeQuietly(reader); } } return p; diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java index 2886cfbc28c..f2ccf54b18a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.mediumtest; -import org.apache.commons.io.IOUtils; +import org.apache.commons.io.Charsets; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.SonarPlugin; @@ -69,9 +69,10 @@ import org.sonar.core.plugins.RemotePlugin; import org.sonar.core.source.SnapshotDataTypes; import javax.annotation.CheckForNull; - import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -176,16 +177,10 @@ public class BatchMediumTester { public TaskBuilder newScanTask(File sonarProps) { Properties prop = new Properties(); - FileReader reader = null; - try { - reader = new FileReader(sonarProps); + try (Reader reader = new InputStreamReader(new FileInputStream(sonarProps), Charsets.UTF_8)) { prop.load(reader); } catch (Exception e) { throw new IllegalStateException("Unable to read configuration file", e); - } finally { - if (reader != null) { - IOUtils.closeQuietly(reader); - } } TaskBuilder builder = new TaskBuilder(this); builder.property("sonar.task", "scan"); @@ -197,7 +192,7 @@ public class BatchMediumTester { } public static class TaskBuilder { - private final Map<String, String> taskProperties = new HashMap<String, String>(); + private final Map<String, String> taskProperties = new HashMap<>(); private BatchMediumTester tester; public TaskBuilder(BatchMediumTester tester) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginsConfigurator.java b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginsConfigurator.java index 11f0b82df8f..57cb762c02f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginsConfigurator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginsConfigurator.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.scan.maven; -import org.apache.commons.io.IOUtils; +import org.apache.commons.io.Charsets; import org.apache.maven.project.MavenProject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,8 +30,10 @@ import org.sonar.api.batch.maven.MavenPluginHandler; import org.sonar.api.resources.Project; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; public class MavenPluginsConfigurator implements BatchComponent { @@ -61,15 +63,11 @@ public class MavenPluginsConfigurator implements BatchComponent { MavenProject pom = project.getPom(); if (pom != null) { File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml"); - FileWriter fileWriter = null; - try { - fileWriter = new FileWriter(targetPom, false); + try (Writer fileWriter = new OutputStreamWriter(new FileOutputStream(targetPom, false), Charsets.UTF_8)) { pom.writeModel(fileWriter); } catch (IOException e) { throw new IllegalStateException("Can not save pom to " + targetPom, e); - } finally { - IOUtils.closeQuietly(fileWriter); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalysisPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalysisPublisher.java index f5596acbb52..621668853d3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalysisPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalysisPublisher.java @@ -19,8 +19,8 @@ */ package org.sonar.batch.scan2; +import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.bootstrap.ProjectDefinition; @@ -35,8 +35,10 @@ import org.sonar.api.utils.ZipUtils; import org.sonar.api.utils.text.JsonWriter; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.Properties; public final class AnalysisPublisher { @@ -94,9 +96,7 @@ public final class AnalysisPublisher { private void exportIssues(File exportDir) { File issuesFile = new File(exportDir, "issues.json"); - FileWriter issueWriter = null; - try { - issueWriter = new FileWriter(issuesFile); + try (Writer issueWriter = new OutputStreamWriter(new FileOutputStream(issuesFile), Charsets.UTF_8)) { JsonWriter jsonWriter = JsonWriter.of(issueWriter); jsonWriter .beginObject().name("issues") @@ -123,16 +123,12 @@ public final class AnalysisPublisher { .close(); } catch (IOException e) { throw unableToExport(e); - } finally { - IOUtils.closeQuietly(issueWriter); } } private void exportMeasures(File exportDir) { File measuresFile = new File(exportDir, "measures.json"); - FileWriter measureWriter = null; - try { - measureWriter = new FileWriter(measuresFile); + try (Writer measureWriter = new OutputStreamWriter(new FileOutputStream(measuresFile), Charsets.UTF_8)) { JsonWriter jsonWriter = JsonWriter.of(measureWriter); jsonWriter .beginObject().name("measures") @@ -152,8 +148,6 @@ public final class AnalysisPublisher { .close(); } catch (IOException e) { throw unableToExport(e); - } finally { - IOUtils.closeQuietly(measureWriter); } } @@ -173,14 +167,10 @@ public final class AnalysisPublisher { File propsFile = new File(exportDir, "analysis.properties"); Properties props = new Properties(); props.putAll(settings.getProperties()); - FileWriter writer = null; - try { - writer = new FileWriter(propsFile); + try (Writer writer = new OutputStreamWriter(new FileOutputStream(propsFile), Charsets.UTF_8)) { props.store(writer, "SonarQube batch"); } catch (IOException e) { throw unableToExport(e); - } finally { - IOUtils.closeQuietly(writer); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java index 0b9547e0e0e..4b40db12e86 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java @@ -44,7 +44,6 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java index ad079758ee7..f9ce4d7444c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java @@ -19,10 +19,13 @@ */ package org.sonar.api.utils.command; +import com.google.common.base.Charsets; import com.google.common.io.Closeables; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nullable; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -137,7 +140,7 @@ public class CommandExecutor { return execute(command, new DefaultConsumer(), new DefaultConsumer(), timeoutMilliseconds); } - private void closeStreams(Process process) { + private void closeStreams(@Nullable Process process) { if (process != null) { Closeables.closeQuietly(process.getInputStream()); Closeables.closeQuietly(process.getInputStream()); @@ -146,7 +149,7 @@ public class CommandExecutor { } } - private void waitUntilFinish(StreamGobbler thread) { + private void waitUntilFinish(@Nullable StreamGobbler thread) { if (thread != null) { try { thread.join(); @@ -169,19 +172,13 @@ public class CommandExecutor { @Override public void run() { - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8))) { String line; while ((line = br.readLine()) != null) { consumeLine(line); } } catch (IOException ioe) { exception = ioe; - - } finally { - Closeables.closeQuietly(br); - Closeables.closeQuietly(isr); } } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java index fdb16924b63..00b333144ad 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java @@ -24,10 +24,8 @@ import org.apache.commons.io.IOUtils; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; -import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; @@ -76,7 +74,7 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { // And fail only if there are missing keys return missingKeys.isEmpty(); } catch (IOException e) { - fail("An error occured while reading the bundles: " + e.getMessage()); + fail("An error occurred while reading the bundles: " + e.getMessage()); return false; } finally { IOUtils.closeQuietly(bundleInputStream); @@ -123,19 +121,15 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { dumpFile.delete(); } dumpFile.getParentFile().mkdirs(); - Writer writer = null; - try { - writer = new OutputStreamWriter(new FileOutputStream(dumpFile), Charsets.UTF_8); + try (Writer writer = new OutputStreamWriter(new FileOutputStream(dumpFile), Charsets.UTF_8)) { writer.write(details); } catch (IOException e) { throw new IllegalStateException("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'", e); - } finally { - IOUtils.closeQuietly(writer); } } protected static SortedMap<String, String> retrieveMissingTranslations(InputStream bundle, InputStream referenceBundle) throws IOException { - SortedMap<String, String> missingKeys = new TreeMap(); + SortedMap<String, String> missingKeys = new TreeMap<>(); Properties bundleProps = loadProperties(bundle); Properties referenceProperties = loadProperties(referenceBundle); |