3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import com.google.common.base.Function;
23 import javax.annotation.Nonnull;
24 import org.sonar.core.util.CloseableIterator;
25 import org.sonar.scanner.protocol.output.ScannerReport;
26 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
27 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
28 import org.sonar.server.computation.task.projectanalysis.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
30 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
32 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
33 import org.sonar.server.computation.task.projectanalysis.duplication.DetailedTextBlock;
34 import org.sonar.server.computation.task.projectanalysis.duplication.Duplicate;
35 import org.sonar.server.computation.task.projectanalysis.duplication.Duplication;
36 import org.sonar.server.computation.task.projectanalysis.duplication.DuplicationRepository;
37 import org.sonar.server.computation.task.projectanalysis.duplication.InProjectDuplicate;
38 import org.sonar.server.computation.task.projectanalysis.duplication.InnerDuplicate;
39 import org.sonar.server.computation.task.projectanalysis.duplication.TextBlock;
40 import org.sonar.server.computation.task.step.ComputationStep;
42 import static com.google.common.base.Preconditions.checkArgument;
43 import static com.google.common.collect.FluentIterable.from;
44 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
47 * Loads duplication information from the report and loads them into the {@link DuplicationRepository}.
49 public class LoadDuplicationsFromReportStep implements ComputationStep {
50 private final TreeRootHolder treeRootHolder;
51 private final BatchReportReader batchReportReader;
52 private final DuplicationRepository duplicationRepository;
53 private final AnalysisMetadataHolder analysisMetadataHolder;
55 public LoadDuplicationsFromReportStep(TreeRootHolder treeRootHolder, BatchReportReader batchReportReader,
56 DuplicationRepository duplicationRepository, AnalysisMetadataHolder analysisMetadataHolder) {
57 this.treeRootHolder = treeRootHolder;
58 this.batchReportReader = batchReportReader;
59 this.duplicationRepository = duplicationRepository;
60 this.analysisMetadataHolder = analysisMetadataHolder;
64 public String getDescription() {
65 return "Load inner file and in project duplications";
69 public void execute() {
70 if (!analysisMetadataHolder.isIncrementalAnalysis()) {
71 new DepthTraversalTypeAwareCrawler(
72 new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
74 public void visitFile(Component file) {
75 try (CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) {
77 while (duplications.hasNext()) {
78 loadDuplications(file, duplications.next(), idGenerator);
83 }).visit(treeRootHolder.getRoot());
87 private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
88 duplicationRepository.add(file,
90 convert(duplication.getOriginPosition(), id),
91 from(duplication.getDuplicateList())
92 .transform(new BatchDuplicateToCeDuplicate(file))));
95 private static TextBlock convert(ScannerReport.TextRange textRange) {
96 return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
99 private static DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
100 return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());
103 private class BatchDuplicateToCeDuplicate implements Function<ScannerReport.Duplicate, Duplicate> {
104 private final Component file;
106 private BatchDuplicateToCeDuplicate(Component file) {
112 public Duplicate apply(@Nonnull ScannerReport.Duplicate input) {
113 if (input.getOtherFileRef() != 0) {
114 checkArgument(input.getOtherFileRef() != file.getReportAttributes().getRef(), "file and otherFile references can not be the same");
115 return new InProjectDuplicate(
116 treeRootHolder.getComponentByRef(input.getOtherFileRef()),
117 convert(input.getRange()));
119 return new InnerDuplicate(convert(input.getRange()));