a nice refactoring, and more tests

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1081 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-10-08 16:43:26 +00:00
parent cc0195eaa0
commit 519a8b9c4a
4 changed files with 61 additions and 24 deletions

View File

@ -4,12 +4,14 @@ import net.sourceforge.pmd.ast.*;
import java.util.Set;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
public class SimpleNodeTest
extends ParserTst
{
public void testMethodDiffLines()
public void testMethodDiffLines()
throws Throwable
{
String javaCode = "public class Test {\n";
@ -26,7 +28,7 @@ public class SimpleNodeTest
2, 3, 4, 2 );
}
public void testMethodSameColumn()
public void testMethodSameColumn()
throws Throwable
{
String javaCode = "public class Test {\n";
@ -42,7 +44,7 @@ public class SimpleNodeTest
2, 1, 3, 1 );
}
public void testMethodSameLine()
public void testMethodSameLine()
throws Throwable
{
String javaCode = "public class Test {\n";
@ -68,7 +70,7 @@ public class SimpleNodeTest
assertTrue( iter.hasNext() );
verifyNode( (SimpleNode) iter.next(),
1, 8, 1, 20 );
}
public void testNames() throws Throwable {
@ -117,6 +119,36 @@ public class SimpleNodeTest
endLine, node.getEndLine() );
assertEquals( "Wrong Column provide for End: ",
endCol, node.getEndColumn() );
}
public void testFindChildrenOfType() {
ASTBlock block = new ASTBlock(2);
block.jjtAddChild(new ASTReturnStatement(1), 0);
List nodes = new ArrayList();
block.findChildrenOfType(ASTReturnStatement.class, nodes);
assertEquals(1, nodes.size());
}
public void testFindChildrenOfTypeMultiple() {
ASTBlock block = new ASTBlock(1);
block.jjtAddChild(new ASTBlockStatement(2), 0);
block.jjtAddChild(new ASTBlockStatement(3), 1);
List nodes = new ArrayList();
block.findChildrenOfType(ASTBlockStatement.class, nodes);
assertEquals(2, nodes.size());
}
public void testFindChildrenOfTypeRecurse() {
ASTBlock block = new ASTBlock(1);
ASTBlock childBlock = new ASTBlock(2);
block.jjtAddChild(childBlock, 0);
childBlock.jjtAddChild(new ASTMethodDeclaration(3), 0);
List nodes = new ArrayList();
block.findChildrenOfType(ASTMethodDeclaration.class, nodes);
assertEquals(1, nodes.size());
}
}

View File

@ -3,6 +3,8 @@ package net.sourceforge.pmd.ast;
import net.sourceforge.pmd.symboltable.Scope;
import java.util.List;
public class SimpleNode implements Node {
protected Node parent;
protected Node[] children;
@ -97,6 +99,26 @@ public class SimpleNode implements Node {
public int getEndColumn() {
return endColumn;
}
public void findChildrenOfType(Class targetType, List results) {
findChildrenOfType(this, targetType, results);
}
private void findChildrenOfType(Node node, Class targetType, List results) {
if (node.getClass().equals(targetType)) {
results.add(node);
}
for (int i=0; i<node.jjtGetNumChildren(); i++) {
Node child = (Node)node.jjtGetChild(i);
if (child.jjtGetNumChildren()>0) {
findChildrenOfType(child, targetType, results);
} else {
if (child.getClass().equals(targetType)) {
results.add(child);
}
}
}
}
// NEW STUFF
public void jjtSetParent(Node n) { parent = n; }

View File

@ -96,8 +96,7 @@ public class SimplifyBooleanReturnsRule extends AbstractRule {
private SimpleNode getLastChild(SimpleNode node) {
if (node.jjtGetNumChildren() == 0) {
return node;
} else {
return getLastChild((SimpleNode)node.jjtGetChild(0));
}
return getLastChild((SimpleNode)node.jjtGetChild(0));
}
}

View File

@ -25,7 +25,7 @@ public class OnlyOneReturnRule extends AbstractRule {
}
List returnNodes = new ArrayList();
findReturns(node, returnNodes);
node.findChildrenOfType(ASTReturnStatement.class, returnNodes);
if (returnNodes.size() > 1) {
RuleContext ctx = (RuleContext)data;
for (Iterator i = returnNodes.iterator(); i.hasNext();) {
@ -46,20 +46,4 @@ public class OnlyOneReturnRule extends AbstractRule {
}
return data;
}
private void findReturns(Node node, List returnNodes) {
if (node instanceof ASTReturnStatement) {
returnNodes.add(node);
}
for (int i=0; i<node.jjtGetNumChildren(); i++) {
Node child = (Node)node.jjtGetChild(i);
if (child.jjtGetNumChildren()>0) {
findReturns(child, returnNodes);
} else {
if (child instanceof ASTReturnStatement) {
returnNodes.add(child);
}
}
}
}
}