diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-09-08 16:18:18 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-09-08 17:54:15 +0400 |
commit | 50632107856202e4955f5dc62fd946293c99dbcb (patch) | |
tree | 8ef1e91ccf9efa283b97ab82c7ef46275e3a7537 /plugins | |
parent | cf9829861fca004f21640c27d692adb064b0f098 (diff) | |
download | sonarqube-50632107856202e4955f5dc62fd946293c99dbcb.tar.gz sonarqube-50632107856202e4955f5dc62fd946293c99dbcb.zip |
SONAR-1091 Remove duplicated code in sonar-cpd-plugin
Diffstat (limited to 'plugins')
4 files changed, 123 insertions, 129 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java index 4214a736039..b50838ae34c 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java @@ -19,20 +19,21 @@ */ package org.sonar.plugins.cpd; +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + import net.sourceforge.pmd.cpd.TokenEntry; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.CpdMapping; import org.sonar.api.batch.SensorContext; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.duplications.cpd.Match; -import java.io.File; -import java.util.*; - public class CpdAnalyser { private static final Logger LOG = LoggerFactory.getLogger(CpdAnalyser.class); @@ -83,7 +84,7 @@ public class CpdAnalyser { } for (DuplicationsData data : duplicationsData.values()) { - data.saveUsing(context); + data.save(); } } @@ -96,50 +97,4 @@ public class CpdAnalyser { return data; } - private static final class DuplicationsData { - - protected Set<Integer> duplicatedLines = new HashSet<Integer>(); - protected double duplicatedBlocks = 0; - protected Resource resource; - private SensorContext context; - private List<StringBuilder> duplicationXMLEntries = new ArrayList<StringBuilder>(); - - private DuplicationsData(Resource resource, SensorContext context) { - this.context = context; - this.resource = resource; - } - - protected void cumulate(Resource targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) { - StringBuilder xml = new StringBuilder(); - xml.append("<duplication lines=\"").append(duplicatedLines).append("\" start=\"").append(duplicationStartLine) - .append("\" target-start=\"").append(targetDuplicationStartLine).append("\" target-resource=\"") - .append(context.saveResource(targetResource)).append("\"/>"); - - duplicationXMLEntries.add(xml); - - for (int duplicatedLine = duplicationStartLine; duplicatedLine < duplicationStartLine + duplicatedLines; duplicatedLine++) { - this.duplicatedLines.add(duplicatedLine); - } - } - - protected void incrementDuplicatedBlock() { - duplicatedBlocks++; - } - - protected void saveUsing(SensorContext context) { - 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, getDuplicationXMLData())); - } - - private String getDuplicationXMLData() { - StringBuilder duplicationXML = new StringBuilder("<duplications>"); - for (StringBuilder xmlEntry : duplicationXMLEntries) { - duplicationXML.append(xmlEntry); - } - duplicationXML.append("</duplications>"); - return duplicationXML.toString(); - } - } } diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java new file mode 100644 index 00000000000..1fc00423f99 --- /dev/null +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java @@ -0,0 +1,112 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.cpd; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.sonar.api.batch.SensorContext; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.Resource; + +public class DuplicationsData { + + private Resource resource; + private Set<Integer> duplicatedLines = new HashSet<Integer>(); + private double duplicatedBlocks; + private List<XmlEntry> duplicationXMLEntries = new ArrayList<XmlEntry>(); + + private SensorContext context; + + public DuplicationsData(Resource resource, SensorContext context) { + this.resource = resource; + this.context = context; + } + + public void cumulate(String targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) { + duplicationXMLEntries.add(new XmlEntry(targetResource, targetDuplicationStartLine, duplicationStartLine, duplicatedLines)); + for (int duplicatedLine = duplicationStartLine; duplicatedLine < duplicationStartLine + duplicatedLines; duplicatedLine++) { + this.duplicatedLines.add(duplicatedLine); + } + } + + public void cumulate(Resource targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) { + cumulate(context.saveResource(targetResource), targetDuplicationStartLine, duplicationStartLine, duplicatedLines); + } + + public void incrementDuplicatedBlock() { + duplicatedBlocks++; + } + + public void 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, getDuplicationXMLData())); + } + + private String getDuplicationXMLData() { + Collections.sort(duplicationXMLEntries); + StringBuilder duplicationXML = new StringBuilder("<duplications>"); + for (XmlEntry xmlEntry : duplicationXMLEntries) { + duplicationXML.append(xmlEntry.toString()); + } + duplicationXML.append("</duplications>"); + return duplicationXML.toString(); + } + + /** + * Note: this class has a natural ordering that is inconsistent with equals. + */ + private static final class XmlEntry implements Comparable<XmlEntry> { + private String target; + private int targetStartLine; + private int startLine; + private int lines; + + private XmlEntry(String target, int targetStartLine, int startLine, int lines) { + this.target = target; + this.targetStartLine = targetStartLine; + this.startLine = startLine; + this.lines = lines; + } + + @Override + public String toString() { + return new StringBuilder().append("<duplication lines=\"").append(lines) + .append("\" start=\"").append(startLine) + .append("\" target-start=\"").append(targetStartLine) + .append("\" target-resource=\"").append(target).append("\"/>") + .toString(); + } + + public int compareTo(XmlEntry o) { + if (startLine == o.startLine) { + return lines - o.lines; + } + return startLine - o.startLine; + } + } + +} 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 8376e0fb1cd..12de1033609 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 @@ -23,13 +23,8 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.Reader; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -37,8 +32,6 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.SensorContext; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; import org.sonar.api.resources.InputFile; import org.sonar.api.resources.Java; import org.sonar.api.resources.JavaFile; @@ -152,11 +145,11 @@ public class SonarEngine extends CpdEngine { List<CloneGroup> clones = OriginalCloneDetectionAlgorithm.detect(index, fileBlocks); if (!clones.isEmpty()) { // Save - DuplicationsData data = new DuplicationsData(); + DuplicationsData data = new DuplicationsData(resource, context); for (CloneGroup clone : clones) { poplulateData(data, clone); } - data.save(context, resource); + data.save(); } } } @@ -183,71 +176,4 @@ public class SonarEngine extends CpdEngine { } } - // TODO Godin: reuse this class for PMD-CPD - private static final class DuplicationsData { - - protected Set<Integer> duplicatedLines = new HashSet<Integer>(); - protected double duplicatedBlocks = 0; - private List<XmlEntry> duplicationXMLEntries = new ArrayList<XmlEntry>(); - - private static final class XmlEntry { - protected StringBuilder xml; - protected int startLine; - protected int lines; - - private XmlEntry(int startLine, int lines, StringBuilder xml) { - this.xml = xml; - this.startLine = startLine; - this.lines = lines; - } - } - - private DuplicationsData() { - } - - protected void cumulate(String targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) { - StringBuilder xml = new StringBuilder(); - xml.append("<duplication lines=\"").append(duplicatedLines).append("\" start=\"").append(duplicationStartLine) - .append("\" target-start=\"").append(targetDuplicationStartLine).append("\" target-resource=\"") - .append(targetResource).append("\"/>"); - - duplicationXMLEntries.add(new XmlEntry(duplicationStartLine, duplicatedLines, xml)); - - for (int duplicatedLine = duplicationStartLine; duplicatedLine < duplicationStartLine + duplicatedLines; duplicatedLine++) { - this.duplicatedLines.add(duplicatedLine); - } - } - - protected void incrementDuplicatedBlock() { - duplicatedBlocks++; - } - - protected void save(SensorContext context, Resource resource) { - 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, getDuplicationXMLData())); - } - - private String getDuplicationXMLData() { - StringBuilder duplicationXML = new StringBuilder("<duplications>"); - - Comparator<XmlEntry> comp = new Comparator<XmlEntry>() { - public int compare(XmlEntry o1, XmlEntry o2) { - if (o1.startLine == o2.startLine) { - return o2.lines - o1.lines; - } - return o1.startLine - o2.startLine; - } - }; - Collections.sort(duplicationXMLEntries, comp); - - for (XmlEntry xmlEntry : duplicationXMLEntries) { - duplicationXML.append(xmlEntry.xml); - } - duplicationXML.append("</duplications>"); - return duplicationXML.toString(); - } - } - } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java index 827c266119a..a5d3f07bba8 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java @@ -130,8 +130,9 @@ public class CpdAnalyserTest { verify(context).saveMeasure( eq(resource1), argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>" + + "<duplication lines=\"100\" start=\"5\" target-start=\"15\" target-resource=\"key3\"/>" + "<duplication lines=\"200\" start=\"5\" target-start=\"15\" target-resource=\"key2\"/>" - + "<duplication lines=\"100\" start=\"5\" target-start=\"15\" target-resource=\"key3\"/>" + "</duplications>"))); + + "</duplications>"))); verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_FILES, 1d); verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_LINES, 200d); |