aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-cpd-plugin/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-09-01 17:27:45 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-09-01 17:28:09 +0200
commita9bd7ecdd2b0f138275c1bb2954ab6ee9b804844 (patch)
treefb1759dc69571ef8050ced531609c69045578b25 /plugins/sonar-cpd-plugin/src
parented80a6430099a0dfd3bc7f016a8392bf22896b74 (diff)
downloadsonarqube-a9bd7ecdd2b0f138275c1bb2954ab6ee9b804844.tar.gz
sonarqube-a9bd7ecdd2b0f138275c1bb2954ab6ee9b804844.zip
Fix some quality flaws
Diffstat (limited to 'plugins/sonar-cpd-plugin/src')
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DefaultCpdEngine.java35
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java30
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java2
3 files changed, 37 insertions, 30 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DefaultCpdEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DefaultCpdEngine.java
index 887c886ac1e..925ecb87638 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DefaultCpdEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DefaultCpdEngine.java
@@ -110,22 +110,7 @@ public class DefaultCpdEngine extends CpdEngine {
// Create index
SonarDuplicationsIndex index = indexFactory.create(project, languageKey);
-
- TokenizerBridge bridge = null;
- if (mapping != null) {
- bridge = new TokenizerBridge(mapping.getTokenizer(), fs.encoding().name(), getBlockSize(languageKey));
- }
- for (InputFile inputFile : sourceFiles) {
- LOG.debug("Populating index from {}", inputFile);
- String resourceEffectiveKey = ((DeprecatedDefaultInputFile) inputFile).key();
- FileBlocks fileBlocks = duplicationCache.byComponent(resourceEffectiveKey);
- if (fileBlocks != null) {
- index.insert(inputFile, fileBlocks.blocks());
- } else if (bridge != null) {
- List<Block> blocks2 = bridge.chunk(resourceEffectiveKey, inputFile.file());
- index.insert(inputFile, blocks2);
- }
- }
+ populateIndex(languageKey, sourceFiles, mapping, index);
// Detect
Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(languageKey));
@@ -157,6 +142,24 @@ public class DefaultCpdEngine extends CpdEngine {
}
}
+ private void populateIndex(String languageKey, List<InputFile> sourceFiles, CpdMapping mapping, SonarDuplicationsIndex index) {
+ TokenizerBridge bridge = null;
+ if (mapping != null) {
+ bridge = new TokenizerBridge(mapping.getTokenizer(), fs.encoding().name(), getBlockSize(languageKey));
+ }
+ for (InputFile inputFile : sourceFiles) {
+ LOG.debug("Populating index from {}", inputFile);
+ String resourceEffectiveKey = ((DeprecatedDefaultInputFile) inputFile).key();
+ FileBlocks fileBlocks = duplicationCache.byComponent(resourceEffectiveKey);
+ if (fileBlocks != null) {
+ index.insert(inputFile, fileBlocks.blocks());
+ } else if (bridge != null) {
+ List<Block> blocks2 = bridge.chunk(resourceEffectiveKey, inputFile.file());
+ index.insert(inputFile, blocks2);
+ }
+ }
+ }
+
@VisibleForTesting
int getBlockSize(String languageKey) {
int blockSize = settings.getInt("sonar.cpd." + languageKey + ".minimumLines");
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java
index 43585d4db4f..a5770634444 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java
@@ -198,20 +198,8 @@ public class JavaCpdEngine extends CpdEngine {
if (duplications == null || Iterables.isEmpty(duplications)) {
return;
}
- // Calculate number of lines and blocks
Set<Integer> duplicatedLines = new HashSet<Integer>();
- int duplicatedBlocks = 0;
- for (CloneGroup clone : duplications) {
- ClonePart origin = clone.getOriginPart();
- for (ClonePart part : clone.getCloneParts()) {
- if (part.getResourceId().equals(origin.getResourceId())) {
- duplicatedBlocks++;
- for (int duplicatedLine = part.getStartLine(); duplicatedLine < part.getStartLine() + part.getLines(); duplicatedLine++) {
- duplicatedLines.add(duplicatedLine);
- }
- }
- }
- }
+ int duplicatedBlocks = computeBlockAndLineCount(duplications, duplicatedLines);
FileLinesContext linesContext = contextFactory.createFor(inputFile);
for (int i = 1; i <= inputFile.lines(); i++) {
linesContext.setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, i, duplicatedLines.contains(i) ? 1 : 0);
@@ -246,4 +234,20 @@ public class JavaCpdEngine extends CpdEngine {
context.saveDuplications(inputFile, builder.build());
}
+ private static int computeBlockAndLineCount(Iterable<CloneGroup> duplications, Set<Integer> duplicatedLines) {
+ int duplicatedBlocks = 0;
+ for (CloneGroup clone : duplications) {
+ ClonePart origin = clone.getOriginPart();
+ for (ClonePart part : clone.getCloneParts()) {
+ if (part.getResourceId().equals(origin.getResourceId())) {
+ duplicatedBlocks++;
+ for (int duplicatedLine = part.getStartLine(); duplicatedLine < part.getStartLine() + part.getLines(); duplicatedLine++) {
+ duplicatedLines.add(duplicatedLine);
+ }
+ }
+ }
+ }
+ return duplicatedBlocks;
+ }
+
}
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java
index 6d5a7f1201d..9830f4b1dd0 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java
@@ -64,7 +64,7 @@ public class IndexFactory implements BatchComponent {
}
@VisibleForTesting
- boolean verifyCrossProject(Project project, Logger logger) {
+ boolean verifyCrossProject(@Nullable Project project, Logger logger) {
boolean crossProject = false;
if (settings.getBoolean(CoreProperties.CPD_CROSS_PROJECT)) {