]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9734 use try-with-resources instead of finally blocks
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Aug 2017 20:09:41 +0000 (22:09 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 21 Aug 2017 16:09:34 +0000 (18:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/source/SourceHashRepositoryImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistCrossProjectDuplicationIndexStep.java

index b281b86dcef1c76a5d9545fd0fccf1566c825d0b..b2e2e840157d040aace570700af453e77210f40c 100644 (file)
@@ -59,14 +59,11 @@ public class SourceHashRepositoryImpl implements SourceHashRepository {
 
   private String computeRawSourceHash(Component file) {
     SourceHashComputer sourceHashComputer = new SourceHashComputer();
-    CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
-    try {
+    try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file)) {
       while (linesIterator.hasNext()) {
         sourceHashComputer.addLine(linesIterator.next(), linesIterator.hasNext());
       }
       return sourceHashComputer.getHash();
-    } finally {
-      linesIterator.close();
     }
   }
 
index ba541186689d379567a3897bd470c94c7f1496a0..5c7c70a58f12d617c2abb01ab747adf507889bfe 100644 (file)
@@ -72,15 +72,12 @@ public class LoadDuplicationsFromReportStep implements ComputationStep {
         new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
           @Override
           public void visitFile(Component file) {
-            CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef());
-            try {
+            try (CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) {
               int idGenerator = 1;
               while (duplications.hasNext()) {
                 loadDuplications(file, duplications.next(), idGenerator);
                 idGenerator++;
               }
-            } finally {
-              duplications.close();
             }
           }
         }).visit(treeRootHolder.getRoot());
index 2a44cf87bce9ebb52527801c87987ebb4042c01b..fa9518c73a48ae975da7931b1f8e8395f2f2a836 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.server.computation.task.projectanalysis.step;
 
+import java.util.List;
+import javax.annotation.Nullable;
 import org.sonar.core.util.CloseableIterator;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -28,20 +30,16 @@ import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
-import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
 import org.sonar.server.computation.task.step.ComputationStep;
 
 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
 
-import java.util.List;
-
-import javax.annotation.Nullable;
-
 /**
  * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
  */
@@ -106,8 +104,7 @@ public class PersistCrossProjectDuplicationIndexStep implements ComputationStep
 
     private void readFromReport(Component component) {
       int indexInFile = 0;
-      CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef());
-      try {
+      try (CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) {
         while (blocks.hasNext()) {
           ScannerReport.CpdTextBlock block = blocks.next();
           dbClient.duplicationDao().insert(
@@ -121,8 +118,6 @@ public class PersistCrossProjectDuplicationIndexStep implements ComputationStep
               .setComponentUuid(component.getUuid()));
           indexInFile++;
         }
-      } finally {
-        blocks.close();
       }
     }