]> source.dussan.org Git - sonarqube.git/blob
ba541186689d379567a3897bd470c94c7f1496a0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
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;
41
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;
45
46 /**
47  * Loads duplication information from the report and loads them into the {@link DuplicationRepository}.
48  */
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;
54
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;
61   }
62
63   @Override
64   public String getDescription() {
65     return "Load inner file and in project duplications";
66   }
67
68   @Override
69   public void execute() {
70     if (!analysisMetadataHolder.isIncrementalAnalysis()) {
71       new DepthTraversalTypeAwareCrawler(
72         new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
73           @Override
74           public void visitFile(Component file) {
75             CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef());
76             try {
77               int idGenerator = 1;
78               while (duplications.hasNext()) {
79                 loadDuplications(file, duplications.next(), idGenerator);
80                 idGenerator++;
81               }
82             } finally {
83               duplications.close();
84             }
85           }
86         }).visit(treeRootHolder.getRoot());
87     }
88   }
89
90   private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
91     duplicationRepository.add(file,
92       new Duplication(
93         convert(duplication.getOriginPosition(), id),
94         from(duplication.getDuplicateList())
95           .transform(new BatchDuplicateToCeDuplicate(file))));
96   }
97
98   private static TextBlock convert(ScannerReport.TextRange textRange) {
99     return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
100   }
101
102   private static DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
103     return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());
104   }
105
106   private class BatchDuplicateToCeDuplicate implements Function<ScannerReport.Duplicate, Duplicate> {
107     private final Component file;
108
109     private BatchDuplicateToCeDuplicate(Component file) {
110       this.file = file;
111     }
112
113     @Override
114     @Nonnull
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()));
121       }
122       return new InnerDuplicate(convert(input.getRange()));
123     }
124   }
125 }