]> source.dussan.org Git - sonarqube.git/blob
e34bdc8b387bd2a406aa9ae24691ecbffca0339c
[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 java.util.Collection;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import org.sonar.api.utils.log.Logger;
27 import org.sonar.api.utils.log.Loggers;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.duplication.DuplicationUnitDto;
31 import org.sonar.duplications.block.Block;
32 import org.sonar.duplications.block.ByteArray;
33 import org.sonar.scanner.protocol.output.ScannerReport.CpdTextBlock;
34 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
35 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
36 import org.sonar.server.computation.task.projectanalysis.component.Component;
37 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
38 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
39 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
40 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
41 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
42 import org.sonar.server.computation.task.projectanalysis.duplication.IntegrateCrossProjectDuplications;
43 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
44 import org.sonar.server.computation.task.step.ComputationStep;
45
46 import static com.google.common.collect.FluentIterable.from;
47 import static com.google.common.collect.Lists.newArrayList;
48 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
49
50 /**
51  * Feed the duplications repository from the cross project duplication blocks computed with duplications blocks of the analysis report.
52  *
53  * Blocks can be empty if :
54  * - The file is excluded from the analysis using {@link org.sonar.api.CoreProperties#CPD_EXCLUSIONS}
55  * - On Java, if the number of statements of the file is too small, nothing will be sent.
56  */
57 public class LoadCrossProjectDuplicationsRepositoryStep implements ComputationStep {
58
59   private static final Logger LOGGER = Loggers.get(LoadCrossProjectDuplicationsRepositoryStep.class);
60
61   private final TreeRootHolder treeRootHolder;
62   private final BatchReportReader reportReader;
63   private final AnalysisMetadataHolder analysisMetadataHolder;
64   private final IntegrateCrossProjectDuplications integrateCrossProjectDuplications;
65   private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
66   private final DbClient dbClient;
67
68   public LoadCrossProjectDuplicationsRepositoryStep(TreeRootHolder treeRootHolder, BatchReportReader reportReader,
69     AnalysisMetadataHolder analysisMetadataHolder, CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder,
70     IntegrateCrossProjectDuplications integrateCrossProjectDuplications, DbClient dbClient) {
71     this.treeRootHolder = treeRootHolder;
72     this.reportReader = reportReader;
73     this.analysisMetadataHolder = analysisMetadataHolder;
74     this.integrateCrossProjectDuplications = integrateCrossProjectDuplications;
75     this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
76     this.dbClient = dbClient;
77   }
78
79   @Override
80   public void execute() {
81     if (crossProjectDuplicationStatusHolder.isEnabled()) {
82       new DepthTraversalTypeAwareCrawler(new CrossProjectDuplicationVisitor()).visit(treeRootHolder.getRoot());
83     }
84   }
85
86   @Override
87   public String getDescription() {
88     return "Compute cross project duplications";
89   }
90
91   private class CrossProjectDuplicationVisitor extends TypeAwareVisitorAdapter {
92
93     private CrossProjectDuplicationVisitor() {
94       super(CrawlerDepthLimit.FILE, PRE_ORDER);
95     }
96
97     @Override
98     public void visitFile(Component file) {
99       List<CpdTextBlock> cpdTextBlocks = newArrayList(reportReader.readCpdTextBlocks(file.getReportAttributes().getRef()));
100       LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey());
101       if (cpdTextBlocks.isEmpty()) {
102         return;
103       }
104
105       Collection<String> hashes = from(cpdTextBlocks).transform(CpdTextBlockToHash.INSTANCE).toList();
106       List<DuplicationUnitDto> dtos = selectDuplicates(file, hashes);
107       if (dtos.isEmpty()) {
108         return;
109       }
110
111       Collection<Block> duplicatedBlocks = from(dtos).transform(DtoToBlock.INSTANCE).toList();
112       Collection<Block> originBlocks = from(cpdTextBlocks).transform(new CpdTextBlockToBlock(file.getKey())).toList();
113       LOGGER.trace("Found {} duplicated cpd blocks on file {}", duplicatedBlocks.size(), file.getKey());
114
115       integrateCrossProjectDuplications.computeCpd(file, originBlocks, duplicatedBlocks);
116     }
117
118     private List<DuplicationUnitDto> selectDuplicates(Component file, Collection<String> hashes) {
119       try (DbSession dbSession = dbClient.openSession(false)) {
120         Analysis projectAnalysis = analysisMetadataHolder.getBaseAnalysis();
121         String analysisUuid = projectAnalysis == null ? null : projectAnalysis.getUuid();
122         return dbClient.duplicationDao().selectCandidates(dbSession, analysisUuid, file.getFileAttributes().getLanguageKey(), hashes);
123       }
124     }
125   }
126
127   private enum CpdTextBlockToHash implements Function<CpdTextBlock, String> {
128     INSTANCE;
129
130     @Override
131     public String apply(@Nonnull CpdTextBlock duplicationBlock) {
132       return duplicationBlock.getHash();
133     }
134   }
135
136   private enum DtoToBlock implements Function<DuplicationUnitDto, Block> {
137     INSTANCE;
138
139     @Override
140     public Block apply(@Nonnull DuplicationUnitDto dto) {
141       // Note that the dto doesn't contains start/end token indexes
142       return Block.builder()
143         .setResourceId(dto.getComponentKey())
144         .setBlockHash(new ByteArray(dto.getHash()))
145         .setIndexInFile(dto.getIndexInFile())
146         .setLines(dto.getStartLine(), dto.getEndLine())
147         .build();
148     }
149   }
150
151   private static class CpdTextBlockToBlock implements Function<CpdTextBlock, Block> {
152     private final String fileKey;
153     private int indexInFile = 0;
154
155     public CpdTextBlockToBlock(String fileKey) {
156       this.fileKey = fileKey;
157     }
158
159     @Override
160     public Block apply(@Nonnull CpdTextBlock duplicationBlock) {
161       Block block = Block.builder()
162         .setResourceId(fileKey)
163         .setBlockHash(new ByteArray(duplicationBlock.getHash()))
164         .setIndexInFile(indexInFile)
165         .setLines(duplicationBlock.getStartLine(), duplicationBlock.getEndLine())
166         .setUnit(duplicationBlock.getStartTokenIndex(), duplicationBlock.getEndTokenIndex())
167         .build();
168       indexInFile++;
169       return block;
170     }
171   }
172
173 }