Clean up ReportTree.addRuleViolation, to stop using String.split, and += on Strings. This removes a regex, and vastly reduces the number of memory/buffer allocations that are going on.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6932 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Ryan Gustafson
2009-05-01 04:57:10 +00:00
parent 6157a3ce2c
commit 614d00b258

View File

@ -1,9 +1,9 @@
package net.sourceforge.pmd.lang.dfa.report;
import net.sourceforge.pmd.RuleViolation;
import java.util.Iterator;
import net.sourceforge.pmd.RuleViolation;
public class ReportTree {
private PackageNode rootNode = new PackageNode("");
@ -24,7 +24,6 @@ public class ReportTree {
}
public RuleViolation next() {
if (!this.hasNextFlag) {
this.getNext();
} else {
@ -83,7 +82,6 @@ public class ReportTree {
}
}
public Iterator<RuleViolation> iterator() {
return new TreeIterator();
}
@ -106,31 +104,33 @@ public class ReportTree {
* package, class and violation gets there own tree node.
*/
public void addRuleViolation(RuleViolation violation) {
String pack = violation.getPackageName();
String[] a = {};
if (pack == null) {
a = new String[]{""};
} else if (pack.indexOf('.') != -1) {
String[] tmp = pack.split("\\.");
a = new String[tmp.length];
System.arraycopy(tmp, 0, a, 0, tmp.length);
} else {
a = new String[]{pack};
String packageName = violation.getPackageName();
if (packageName == null) {
packageName = "";
}
this.level = this.rootNode;
String plugedPackageName = "";
for (int i = 0; i < a.length; i++) {
String packageName = a[i];
plugedPackageName += packageName + '.';
int endIndex = packageName.indexOf('.');
while (true) {
String parentPackage;
if (endIndex < 0) {
parentPackage = packageName;
} else {
parentPackage = packageName.substring(0, endIndex);
}
if (!this.isStringInLevel(plugedPackageName)) {
PackageNode node = new PackageNode(plugedPackageName);
if (!this.isStringInLevel(parentPackage)) {
PackageNode node = new PackageNode(parentPackage);
this.level.addFirst(node);
// gotoLevel
this.level = node;
}
if (endIndex < 0) {
break;
}
endIndex = packageName.indexOf('.', endIndex + 1);
}
String cl = violation.getClassName();
@ -143,7 +143,7 @@ public class ReportTree {
}
/*
* Filters dublicated rule violations. Like the comparator in
* Filters duplicated rule violations. Like the comparator in
* RuleViolation if he already exists.
*/
ViolationNode tmp = new ViolationNode(violation);
@ -172,21 +172,17 @@ public class ReportTree {
private boolean isStringInLevel(String str) {
for (int i = 0; i < this.level.getChildCount(); i++) {
AbstractReportNode child = this.level.getChildAt(i);
String tmp = null;
final AbstractReportNode child = this.level.getChildAt(i);
final String tmp;
if (child instanceof PackageNode) {
tmp = ((PackageNode) child).getPackageName();
}
if (child instanceof ClassNode) {
} else if (child instanceof ClassNode) {
tmp = ((ClassNode) child).getClassName();
}
if (tmp == null) {
} else {
return false;
}
if (tmp.equals(str)) {
if (tmp != null && tmp.equals(str)) {
// goto level
this.level = child;
return true;