]> source.dussan.org Git - sonarqube.git/commitdiff
Fix unclosed resource
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 12 Jul 2016 10:31:13 +0000 (12:31 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 12 Jul 2016 10:31:13 +0000 (12:31 +0200)
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/NoSonarSensor.java

index 45d6b9dcb7517a58bafff3775a82627d1d9a21b2..d57e2f1eb93af87e37a75e51225c7b5a59c09e05 100644 (file)
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.stream.Stream;
 import org.sonar.api.batch.Phase;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.sensor.Sensor;
@@ -57,12 +58,14 @@ public class NoSonarSensor implements Sensor {
     try {
       Set<Integer> noSonarLines = new HashSet<>();
       int[] lineCounter = {1};
-      Files.lines(inputFile.path(), context.fileSystem().encoding()).forEachOrdered(lineStr -> {
-        if (lineStr.contains("//NOSONAR")) {
-          noSonarLines.add(lineCounter[0]);
-        }
-        lineCounter[0]++;
-      });
+      try (Stream<String> stream = Files.lines(inputFile.path(), context.fileSystem().encoding())) {
+        stream.forEachOrdered(lineStr -> {
+          if (lineStr.contains("//NOSONAR")) {
+            noSonarLines.add(lineCounter[0]);
+          }
+          lineCounter[0]++;
+        });
+      }
       noSonarFilter.noSonarInFile(inputFile, noSonarLines);
     } catch (IOException e) {
       throw new IllegalStateException("Fail to process " + inputFile, e);