updated rule with property descriptors

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4667 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2006-10-15 05:31:22 +00:00
parent ab83afb522
commit 73262fea13
2 changed files with 40 additions and 5 deletions

View File

@ -3,16 +3,31 @@
*/
package net.sourceforge.pmd.rules;
import java.util.Map;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.ASTIfStatement;
import net.sourceforge.pmd.properties.IntegerProperty;
public class AvoidDeeplyNestedIfStmtsRule extends AbstractRule {
private int depth;
private int depthLimit;
private static final PropertyDescriptor problemDepthDescriptor = new IntegerProperty(
"problemDepth",
"Maximum allowable statement depth",
0,
1.0f
);
private static final Map propertyDescriptorsByName = asFixedMap(problemDepthDescriptor);
public Object visit(ASTCompilationUnit node, Object data) {
depth = 0;
depthLimit = getIntProperty("problemDepth");
return super.visit(node, data);
}
@ -21,11 +36,17 @@ public class AvoidDeeplyNestedIfStmtsRule extends AbstractRule {
depth++;
}
super.visit(node, data);
if (depth == getIntProperty("problemDepth")) {
if (depth == depthLimit) {
addViolation(data, node);
}
depth--;
return data;
}
/**
* @return Map
*/
protected Map propertiesByName() {
return propertyDescriptorsByName;
}
}

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.rules.strings;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.ast.ASTAdditiveExpression;
import net.sourceforge.pmd.ast.ASTArgumentList;
import net.sourceforge.pmd.ast.ASTDoStatement;
@ -20,6 +21,7 @@ import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.ASTWhileStatement;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.properties.IntegerProperty;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import java.util.HashSet;
@ -64,6 +66,16 @@ public class ConsecutiveLiteralAppends extends AbstractRule {
blockParents.add(ASTSwitchStatement.class);
blockParents.add(ASTMethodDeclaration.class);
}
private static final PropertyDescriptor thresholdDescriptor = new IntegerProperty(
"threshold",
"?",
1,
1.0f
);
private static final Map propertyDescriptorsByName = asFixedMap(thresholdDescriptor);
private int threshold = 1;
@ -297,12 +309,14 @@ public class ConsecutiveLiteralAppends extends AbstractRule {
}
private static boolean isStringBuffer(ASTVariableDeclaratorId node) {
SimpleNode nn = (SimpleNode) node.getTypeNameNode();
SimpleNode nn = node.getTypeNameNode();
if (nn.jjtGetNumChildren() == 0) {
return false;
}
return "StringBuffer".equals(((SimpleNode) nn.jjtGetChild(0))
.getImage());
return "StringBuffer".equals(((SimpleNode) nn.jjtGetChild(0)).getImage());
}
protected Map propertiesByName() {
return propertyDescriptorsByName;
}
}