3 * Copyright (C) 2009-2019 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.ce.task.projectanalysis.step;
22 import java.util.function.Function;
23 import java.util.stream.Collectors;
24 import javax.annotation.Nonnull;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
26 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
27 import org.sonar.ce.task.projectanalysis.component.Component;
28 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
29 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
30 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
31 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
32 import org.sonar.ce.task.projectanalysis.duplication.DetailedTextBlock;
33 import org.sonar.ce.task.projectanalysis.duplication.Duplicate;
34 import org.sonar.ce.task.projectanalysis.duplication.Duplication;
35 import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepository;
36 import org.sonar.ce.task.projectanalysis.duplication.InExtendedProjectDuplicate;
37 import org.sonar.ce.task.projectanalysis.duplication.InProjectDuplicate;
38 import org.sonar.ce.task.projectanalysis.duplication.InnerDuplicate;
39 import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
40 import org.sonar.ce.task.step.ComputationStep;
41 import org.sonar.core.util.CloseableIterator;
42 import org.sonar.scanner.protocol.output.ScannerReport;
44 import static com.google.common.base.Preconditions.checkArgument;
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 AnalysisMetadataHolder analysisMetadataHolder;
52 private final BatchReportReader batchReportReader;
53 private final DuplicationRepository duplicationRepository;
55 public LoadDuplicationsFromReportStep(TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder, BatchReportReader batchReportReader,
56 DuplicationRepository duplicationRepository) {
57 this.treeRootHolder = treeRootHolder;
58 this.analysisMetadataHolder = analysisMetadataHolder;
59 this.batchReportReader = batchReportReader;
60 this.duplicationRepository = duplicationRepository;
64 public String getDescription() {
65 return "Load duplications";
69 public void execute(ComputationStep.Context context) {
70 DuplicationVisitor visitor = new DuplicationVisitor();
71 new DepthTraversalTypeAwareCrawler(visitor).visit(treeRootHolder.getReportTreeRoot());
72 context.getStatistics().add("duplications", visitor.count);
75 private class BatchDuplicateToCeDuplicate implements Function<ScannerReport.Duplicate, Duplicate> {
76 private final Component file;
78 private BatchDuplicateToCeDuplicate(Component file) {
84 public Duplicate apply(@Nonnull ScannerReport.Duplicate input) {
85 if (input.getOtherFileRef() != 0) {
86 checkArgument(input.getOtherFileRef() != file.getReportAttributes().getRef(), "file and otherFile references can not be the same");
87 Component otherComponent = treeRootHolder.getReportTreeComponentByRef(input.getOtherFileRef());
88 if ((analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) && otherComponent.getStatus() == Component.Status.SAME) {
89 return new InExtendedProjectDuplicate(otherComponent, convert(input.getRange()));
91 return new InProjectDuplicate(otherComponent, convert(input.getRange()));
94 return new InnerDuplicate(convert(input.getRange()));
97 private TextBlock convert(ScannerReport.TextRange textRange) {
98 return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
102 private class DuplicationVisitor extends TypeAwareVisitorAdapter {
103 private int count = 0;
105 private DuplicationVisitor() {
106 super(CrawlerDepthLimit.FILE, Order.POST_ORDER);
110 public void visitFile(Component file) {
111 try (CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) {
113 while (duplications.hasNext()) {
114 loadDuplications(file, duplications.next(), idGenerator);
121 private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
122 duplicationRepository.add(file,
124 convert(duplication.getOriginPosition(), id),
125 duplication.getDuplicateList().stream()
126 .map(new BatchDuplicateToCeDuplicate(file)).collect(Collectors.toList())));
129 private DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
130 return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());