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 CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef());
78 while (duplications.hasNext()) {
79 loadDuplications(file, duplications.next(), idGenerator);
86 }).visit(treeRootHolder.getRoot());
90 private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
91 duplicationRepository.add(file,
93 convert(duplication.getOriginPosition(), id),
94 from(duplication.getDuplicateList())
95 .transform(new BatchDuplicateToCeDuplicate(file))));
98 private static TextBlock convert(ScannerReport.TextRange textRange) {
99 return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
102 private static DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
103 return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());
106 private class BatchDuplicateToCeDuplicate implements Function<ScannerReport.Duplicate, Duplicate> {
107 private final Component file;
109 private BatchDuplicateToCeDuplicate(Component file) {
115 public Duplicate apply(@Nonnull ScannerReport.Duplicate input) {
116 if (input.getOtherFileRef() != 0) {
117 checkArgument(input.getOtherFileRef() != file.getReportAttributes().getRef(), "file and otherFile references can not be the same");
118 return new InProjectDuplicate(
119 treeRootHolder.getComponentByRef(input.getOtherFileRef()),
120 convert(input.getRange()));
122 return new InnerDuplicate(convert(input.getRange()));