Added another version of findChildrenOfType
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1463 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -131,10 +131,7 @@ public class SimpleNodeTest
|
|||||||
public void testFindChildrenOfType() {
|
public void testFindChildrenOfType() {
|
||||||
ASTBlock block = new ASTBlock(2);
|
ASTBlock block = new ASTBlock(2);
|
||||||
block.jjtAddChild(new ASTReturnStatement(1), 0);
|
block.jjtAddChild(new ASTReturnStatement(1), 0);
|
||||||
|
assertEquals(1, block.findChildrenOfType(ASTReturnStatement.class).size());
|
||||||
List nodes = new ArrayList();
|
|
||||||
block.findChildrenOfType(ASTReturnStatement.class, nodes);
|
|
||||||
assertEquals(1, nodes.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFindChildrenOfTypeMultiple() {
|
public void testFindChildrenOfTypeMultiple() {
|
||||||
|
@ -4,6 +4,7 @@ package net.sourceforge.pmd.ast;
|
|||||||
import net.sourceforge.pmd.symboltable.Scope;
|
import net.sourceforge.pmd.symboltable.Scope;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class SimpleNode implements Node {
|
public class SimpleNode implements Node {
|
||||||
protected Node parent;
|
protected Node parent;
|
||||||
@ -100,6 +101,12 @@ public class SimpleNode implements Node {
|
|||||||
return endColumn;
|
return endColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List findChildrenOfType(Class targetType) {
|
||||||
|
List list = new ArrayList();
|
||||||
|
findChildrenOfType(targetType, list);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
public void findChildrenOfType(Class targetType, List results) {
|
public void findChildrenOfType(Class targetType, List results) {
|
||||||
findChildrenOfType(this, targetType, results, true);
|
findChildrenOfType(this, targetType, results, true);
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,7 @@ public class AssignmentInOperandRule extends AbstractRule implements Rule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkForAssignmentInConditionalExpression(SimpleNode node, Object data) {
|
private void checkForAssignmentInConditionalExpression(SimpleNode node, Object data) {
|
||||||
List kids = new ArrayList();
|
List kids = ((SimpleNode)node.jjtGetChild(0)).findChildrenOfType(ASTAssignmentOperator.class);
|
||||||
SimpleNode ifExpression = (SimpleNode)node.jjtGetChild(0);
|
|
||||||
ifExpression.findChildrenOfType(ASTAssignmentOperator.class, kids);
|
|
||||||
if (!kids.isEmpty()) {
|
if (!kids.isEmpty()) {
|
||||||
RuleContext ctx = (RuleContext)data;
|
RuleContext ctx = (RuleContext)data;
|
||||||
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
|
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
|
||||||
|
@ -14,9 +14,7 @@ import java.util.List;
|
|||||||
public class AtLeastOneConstructorRule extends AbstractRule implements Rule {
|
public class AtLeastOneConstructorRule extends AbstractRule implements Rule {
|
||||||
|
|
||||||
public Object visit(ASTClassDeclaration node, Object data) {
|
public Object visit(ASTClassDeclaration node, Object data) {
|
||||||
List constructors = new ArrayList();
|
if (node.findChildrenOfType(ASTConstructorDeclaration.class).isEmpty()) {
|
||||||
node.findChildrenOfType(ASTConstructorDeclaration.class, constructors);
|
|
||||||
if (constructors.isEmpty()) {
|
|
||||||
RuleContext ctx = (RuleContext)data;
|
RuleContext ctx = (RuleContext)data;
|
||||||
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
|
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user