3 * Copyright (C) 2009-2023 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.duplication;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.function.Function;
27 import java.util.function.Predicate;
28 import javax.annotation.Nonnull;
29 import org.sonar.api.CoreProperties;
30 import org.sonar.api.config.Configuration;
31 import org.sonar.api.utils.System2;
32 import org.sonar.api.utils.log.Logger;
33 import org.sonar.api.utils.log.Loggers;
34 import org.sonar.ce.task.log.CeTaskMessages;
35 import org.sonar.ce.task.projectanalysis.component.Component;
36 import org.sonar.duplications.block.Block;
37 import org.sonar.duplications.detector.suffixtree.SuffixTreeCloneDetectionAlgorithm;
38 import org.sonar.duplications.index.CloneGroup;
39 import org.sonar.duplications.index.CloneIndex;
40 import org.sonar.duplications.index.ClonePart;
41 import org.sonar.duplications.index.PackedMemoryCloneIndex;
44 * Transform a list of duplication blocks into clone groups, then add these clone groups into the duplication repository.
46 public class IntegrateCrossProjectDuplications {
48 private static final Logger LOGGER = Loggers.get(IntegrateCrossProjectDuplications.class);
49 private static final String JAVA_KEY = "java";
50 private static final String DEPRECATED_WARNING = "This analysis uses the deprecated cross-project duplication feature.";
51 private static final String DEPRECATED_WARNING_DASHBOARD = "This project uses the deprecated cross-project duplication feature.";
53 private static final int MAX_CLONE_GROUP_PER_FILE = 100;
54 private static final int MAX_CLONE_PART_PER_GROUP = 100;
56 private final Configuration config;
57 private final DuplicationRepository duplicationRepository;
59 private Map<String, NumberOfUnitsNotLessThan> numberOfUnitsByLanguage = new HashMap<>();
61 public IntegrateCrossProjectDuplications(Configuration config, DuplicationRepository duplicationRepository, CeTaskMessages ceTaskMessages, System2 system) {
63 this.duplicationRepository = duplicationRepository;
64 if (config.getBoolean(CoreProperties.CPD_CROSS_PROJECT).orElse(false)) {
65 LOGGER.warn(DEPRECATED_WARNING);
66 ceTaskMessages.add(new CeTaskMessages.Message(DEPRECATED_WARNING_DASHBOARD, system.now()));
70 public void computeCpd(Component component, Collection<Block> originBlocks, Collection<Block> duplicationBlocks) {
71 CloneIndex duplicationIndex = new PackedMemoryCloneIndex();
72 populateIndex(duplicationIndex, originBlocks);
73 populateIndex(duplicationIndex, duplicationBlocks);
75 List<CloneGroup> duplications = SuffixTreeCloneDetectionAlgorithm.detect(duplicationIndex, originBlocks);
76 Iterable<CloneGroup> filtered = duplications.stream()
77 .filter(getNumberOfUnitsNotLessThan(component.getFileAttributes().getLanguageKey()))
79 addDuplications(component, filtered);
82 private static void populateIndex(CloneIndex duplicationIndex, Collection<Block> duplicationBlocks) {
83 for (Block block : duplicationBlocks) {
84 duplicationIndex.insert(block);
88 private void addDuplications(Component file, Iterable<CloneGroup> duplications) {
89 int cloneGroupCount = 0;
90 for (CloneGroup duplication : duplications) {
92 if (cloneGroupCount > MAX_CLONE_GROUP_PER_FILE) {
93 LOGGER.warn("Too many duplication groups on file {}. Keeping only the first {} groups.", file.getKey(), MAX_CLONE_GROUP_PER_FILE);
96 addDuplication(file, duplication);
100 private void addDuplication(Component file, CloneGroup duplication) {
101 ClonePart originPart = duplication.getOriginPart();
102 List<Duplicate> duplicates = convertClonePartsToDuplicates(file, duplication);
103 if (!duplicates.isEmpty()) {
104 duplicationRepository.add(
106 new Duplication(new TextBlock(originPart.getStartLine(), originPart.getEndLine()), duplicates));
110 private static List<Duplicate> convertClonePartsToDuplicates(final Component file, CloneGroup duplication) {
111 final ClonePart originPart = duplication.getOriginPart();
112 return duplication.getCloneParts().stream()
113 .filter(new DoesNotMatchSameComponentKey(originPart.getResourceId()))
114 .filter(new DuplicateLimiter(file, originPart))
115 .map(ClonePartToCrossProjectDuplicate.INSTANCE)
119 private NumberOfUnitsNotLessThan getNumberOfUnitsNotLessThan(String language) {
120 NumberOfUnitsNotLessThan numberOfUnitsNotLessThan = numberOfUnitsByLanguage.get(language);
121 if (numberOfUnitsNotLessThan == null) {
122 numberOfUnitsNotLessThan = new NumberOfUnitsNotLessThan(getMinimumTokens(language));
123 numberOfUnitsByLanguage.put(language, numberOfUnitsNotLessThan);
125 return numberOfUnitsNotLessThan;
128 private int getMinimumTokens(String languageKey) {
129 // The java language is an exception : it doesn't compute tokens but statement, so the settings could not be used.
130 if (languageKey.equalsIgnoreCase(JAVA_KEY)) {
133 return config.getInt("sonar.cpd." + languageKey + ".minimumTokens").orElse(100);
136 private static class NumberOfUnitsNotLessThan implements Predicate<CloneGroup> {
137 private final int min;
139 NumberOfUnitsNotLessThan(int min) {
144 public boolean test(@Nonnull CloneGroup input) {
145 return input.getLengthInUnits() >= min;
149 private static class DoesNotMatchSameComponentKey implements Predicate<ClonePart> {
150 private final String componentKey;
152 private DoesNotMatchSameComponentKey(String componentKey) {
153 this.componentKey = componentKey;
157 public boolean test(@Nonnull ClonePart part) {
158 return !part.getResourceId().equals(componentKey);
162 private static class DuplicateLimiter implements Predicate<ClonePart> {
163 private final Component file;
164 private final ClonePart originPart;
165 private int counter = 0;
167 DuplicateLimiter(Component file, ClonePart originPart) {
169 this.originPart = originPart;
173 public boolean test(@Nonnull ClonePart input) {
174 if (counter == MAX_CLONE_PART_PER_GROUP) {
175 LOGGER.warn("Too many duplication references on file {} for block at line {}. Keeping only the first {} references.",
176 file.getKey(), originPart.getStartLine(), MAX_CLONE_PART_PER_GROUP);
178 boolean res = counter < MAX_CLONE_GROUP_PER_FILE;
184 private enum ClonePartToCrossProjectDuplicate implements Function<ClonePart, Duplicate> {
189 public Duplicate apply(@Nonnull ClonePart input) {
190 return new CrossProjectDuplicate(
191 input.getResourceId(),
192 new TextBlock(input.getStartLine(), input.getEndLine()));