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.batch.BatchReportReader;
27 import org.sonar.server.computation.task.projectanalysis.component.Component;
28 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
29 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
30 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
31 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
32 import org.sonar.server.computation.task.projectanalysis.duplication.DetailedTextBlock;
33 import org.sonar.server.computation.task.projectanalysis.duplication.Duplicate;
34 import org.sonar.server.computation.task.projectanalysis.duplication.Duplication;
35 import org.sonar.server.computation.task.projectanalysis.duplication.DuplicationRepository;
36 import org.sonar.server.computation.task.projectanalysis.duplication.InProjectDuplicate;
37 import org.sonar.server.computation.task.projectanalysis.duplication.InnerDuplicate;
38 import org.sonar.server.computation.task.projectanalysis.duplication.TextBlock;
39 import org.sonar.server.computation.task.step.ComputationStep;
41 import static com.google.common.base.Preconditions.checkArgument;
42 import static com.google.common.collect.FluentIterable.from;
43 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
46 * Loads duplication information from the report and loads them into the {@link DuplicationRepository}.
48 public class LoadDuplicationsFromReportStep implements ComputationStep {
49 private final TreeRootHolder treeRootHolder;
50 private final BatchReportReader batchReportReader;
51 private final DuplicationRepository duplicationRepository;
53 public LoadDuplicationsFromReportStep(TreeRootHolder treeRootHolder, BatchReportReader batchReportReader, DuplicationRepository duplicationRepository) {
54 this.treeRootHolder = treeRootHolder;
55 this.batchReportReader = batchReportReader;
56 this.duplicationRepository = duplicationRepository;
60 public String getDescription() {
61 return "Load inner file and in project duplications";
65 public void execute() {
66 new DepthTraversalTypeAwareCrawler(
67 new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
69 public void visitFile(Component file) {
70 try (CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) {
72 while (duplications.hasNext()) {
73 loadDuplications(file, duplications.next(), idGenerator);
78 }).visit(treeRootHolder.getRoot());
81 private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
82 duplicationRepository.add(file,
84 convert(duplication.getOriginPosition(), id),
85 from(duplication.getDuplicateList())
86 .transform(new BatchDuplicateToCeDuplicate(file))));
89 private static TextBlock convert(ScannerReport.TextRange textRange) {
90 return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
93 private static DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
94 return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());
97 private class BatchDuplicateToCeDuplicate implements Function<ScannerReport.Duplicate, Duplicate> {
98 private final Component file;
100 private BatchDuplicateToCeDuplicate(Component file) {
106 public Duplicate apply(@Nonnull ScannerReport.Duplicate input) {
107 if (input.getOtherFileRef() != 0) {
108 checkArgument(input.getOtherFileRef() != file.getReportAttributes().getRef(), "file and otherFile references can not be the same");
109 return new InProjectDuplicate(
110 treeRootHolder.getComponentByRef(input.getOtherFileRef()),
111 convert(input.getRange()));
113 return new InnerDuplicate(convert(input.getRange()));