]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 6 Jan 2015 22:18:38 +0000 (23:18 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 6 Jan 2015 22:18:38 +0000 (23:18 +0100)
server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
sonar-application/src/main/java/org/sonar/application/PropsBuilder.java
sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java
sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginsConfigurator.java
sonar-batch/src/main/java/org/sonar/batch/scan2/AnalysisPublisher.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java

index 8185c7828a185951d4be29df8f825af602d603c8..2f75323511bcdd4e013eec5465920e55a66151cf 100644 (file)
@@ -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;
index 1873448fb96d6c24d274caaf028c49bf3f79ea33..652dd4cf81491e4e8f845366f5a0745cf26777ce 100644 (file)
  */
 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;
index 2886cfbc28c87f413f6d6cad4a360ba8555663d4..f2ccf54b18a8b9229aaf64fa6e6f3e3ca2e0ce8f 100644 (file)
@@ -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) {
index 11f0b82df8f55b0a859367db8a598cff9ea506f9..57cb762c02ff9a9e22e91eb2de2b12f6dfc346e4 100644 (file)
@@ -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);
       }
     }
   }
index f5596acbb52cad3cedd5ace723562aec17124f61..621668853d3495eef6a18b1a974e929858cd6496 100644 (file)
@@ -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);
     }
   }
 
index 0b9547e0e0ece376ac4a040f6bfba0dfe9e2f045..4b40db12e86e32a237ccce10bcf076f15b17f8f6 100644 (file)
@@ -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;
index ad079758ee78c247a8fc9729fc47df2a5c3e2a0f..f9ce4d7444c709cd9f6dce8ed0a01020471c9a8e 100644 (file)
  */
 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);
       }
     }
 
index fdb16924b632f54f5f4c57ab8ee3125687b9c031..00b333144ad21e33a2040bf8f47d4d7eb7a662d5 100644 (file)
@@ -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);