protected LineHashSequence loadLineHashSequence() {
List<String> lines;
if (component.getType() == Component.Type.FILE) {
- lines = newArrayList(sourceLinesRepository.readLines(component));
+ try (CloseableIterator<String> linesIt = sourceLinesRepository.readLines(component)) {
+ lines = newArrayList(linesIt);
+ }
} else {
lines = Collections.emptyList();
}
import javax.annotation.Nonnull;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.CloseableIterator;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.duplication.DuplicationUnitDto;
@Override
public void visitFile(Component file) {
- List<CpdTextBlock> cpdTextBlocks = newArrayList(reportReader.readCpdTextBlocks(file.getReportAttributes().getRef()));
- LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey());
- if (cpdTextBlocks.isEmpty()) {
- return;
+ List<CpdTextBlock> cpdTextBlocks;
+ try (CloseableIterator<CpdTextBlock> blocksIt = reportReader.readCpdTextBlocks(file.getReportAttributes().getRef())) {
+ cpdTextBlocks = newArrayList(blocksIt);
+ LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey());
+ if (cpdTextBlocks.isEmpty()) {
+ return;
+ }
}
Collection<String> hashes = from(cpdTextBlocks).transform(CpdTextBlockToHash.INSTANCE).toList();
return;
}
- CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
- LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file);
- try {
+ try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
+ LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file)) {
ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), file.getFileAttributes().getLines());
ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute();
persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange());
} catch (Exception e) {
throw new IllegalStateException(String.format("Cannot persist sources of %s", file.getKey()), e);
- } finally {
- linesIterator.close();
- lineReaders.close();
}
}
}
}
- private static class LineReaders {
+ private static class LineReaders implements AutoCloseable {
private final List<LineReader> readers = new ArrayList<>();
private final List<CloseableIterator<?>> closeables = new ArrayList<>();
@CheckForNull
return readers;
}
- void close() {
+ @Override
+ public void close() {
for (CloseableIterator<?> reportIterator : closeables) {
reportIterator.close();
}