aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-cpd-plugin
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-02-06 12:43:18 +0400
committerFabrice Bellingard <bellingard@gmail.com>2012-02-06 16:27:33 +0100
commitec02a8faea5132013a21349ef75872bbb888a8f4 (patch)
tree1e414c7eba7669741c6f1493cdbe0e8dfe567520 /plugins/sonar-cpd-plugin
parent128122b05d5452ef932bb9cf2b0c02838c14988f (diff)
downloadsonarqube-ec02a8faea5132013a21349ef75872bbb888a8f4.tar.gz
sonarqube-ec02a8faea5132013a21349ef75872bbb888a8f4.zip
Fix some quality flaws
Diffstat (limited to 'plugins/sonar-cpd-plugin')
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java8
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java36
2 files changed, 25 insertions, 19 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
index 2e736acde3f..cd53e73f14e 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
@@ -24,6 +24,7 @@ import com.google.common.collect.Iterables;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.*;
+import org.sonar.duplications.DuplicationPredicates;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.detector.suffixtree.SuffixTreeCloneDetectionAlgorithm;
import org.sonar.duplications.index.CloneGroup;
@@ -75,12 +76,7 @@ public class SonarBridgeEngine extends CpdEngine {
}
// Detect
- final int minimumTokens = PmdEngine.getMinimumTokens(project);
- Predicate<CloneGroup> minimumTokensPredicate = new Predicate<CloneGroup>() {
- public boolean apply(CloneGroup input) {
- return input.getLengthInUnits() >= minimumTokens;
- }
- };
+ Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(PmdEngine.getMinimumTokens(project));
for (InputFile inputFile : inputFiles) {
Resource resource = mapping.createResource(inputFile.getFile(), fileSystem.getSourceDirs());
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
index 7fe2852931f..2fbdde1ed4d 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
@@ -19,6 +19,7 @@
*/
package org.sonar.plugins.cpd;
+import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -88,8 +89,11 @@ public class SonarEngine extends CpdEngine {
if (inputFiles.isEmpty()) {
return;
}
+ SonarDuplicationsIndex index = createIndex(project, inputFiles);
+ detect(index, context, project, inputFiles);
+ }
- // Create index
+ private SonarDuplicationsIndex createIndex(Project project, List<InputFile> inputFiles) {
final SonarDuplicationsIndex index = indexFactory.create(project);
TokenChunker tokenChunker = JavaTokenProducer.build();
@@ -117,7 +121,10 @@ public class SonarEngine extends CpdEngine {
index.insert(resource, blocks);
}
- // Detect
+ return index;
+ }
+
+ private void detect(SonarDuplicationsIndex index, SensorContext context, Project project, List<InputFile> inputFiles) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
for (InputFile inputFile : inputFiles) {
@@ -164,14 +171,14 @@ public class SonarEngine extends CpdEngine {
return JavaFile.fromRelativePath(inputFile.getRelativePath(), false);
}
- static void save(SensorContext context, Resource resource, Iterable<CloneGroup> clones) {
- if (clones == null || !clones.iterator().hasNext()) {
+ static void save(SensorContext context, Resource resource, Iterable<CloneGroup> duplications) {
+ if (Iterables.isEmpty(duplications)) {
return;
}
// Calculate number of lines and blocks
Set<Integer> duplicatedLines = new HashSet<Integer>();
double duplicatedBlocks = 0;
- for (CloneGroup clone : clones) {
+ for (CloneGroup clone : duplications) {
ClonePart origin = clone.getOriginPart();
for (ClonePart part : clone.getCloneParts()) {
if (part.getResourceId().equals(origin.getResourceId())) {
@@ -182,12 +189,19 @@ public class SonarEngine extends CpdEngine {
}
}
}
- // Build XML
+ // Save
+ context.saveMeasure(resource, CoreMetrics.DUPLICATED_FILES, 1.0);
+ context.saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, (double) duplicatedLines.size());
+ context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, duplicatedBlocks);
+ context.saveMeasure(resource, new Measure(CoreMetrics.DUPLICATIONS_DATA, toXml(duplications)));
+ }
+
+ private static String toXml(Iterable<CloneGroup> duplications) {
StringBuilder xml = new StringBuilder();
xml.append("<duplications>");
- for (CloneGroup clone : clones) {
+ for (CloneGroup duplication : duplications) {
xml.append("<g>");
- for (ClonePart part : clone.getCloneParts()) {
+ for (ClonePart part : duplication.getCloneParts()) {
xml.append("<b s=\"").append(part.getStartLine())
.append("\" l=\"").append(part.getLines())
.append("\" r=\"").append(part.getResourceId())
@@ -196,11 +210,7 @@ public class SonarEngine extends CpdEngine {
xml.append("</g>");
}
xml.append("</duplications>");
- // Save
- context.saveMeasure(resource, CoreMetrics.DUPLICATED_FILES, 1d);
- context.saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, (double) duplicatedLines.size());
- context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, duplicatedBlocks);
- context.saveMeasure(resource, new Measure(CoreMetrics.DUPLICATIONS_DATA, xml.toString()));
+ return xml.toString();
}
}