added property descriptors & references to them

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4698 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2006-10-17 02:28:09 +00:00
parent a2744c7eae
commit 1ea1900e09

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
@ -14,9 +15,11 @@ import net.sourceforge.pmd.ast.ASTReferenceType;
import net.sourceforge.pmd.ast.ASTResultType;
import net.sourceforge.pmd.ast.ASTType;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.properties.IntegerProperty;
import net.sourceforge.pmd.symboltable.ClassScope;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -34,14 +37,21 @@ public class CouplingBetweenObjects extends AbstractRule {
private int couplingCount;
private Set typesFoundSoFar;
private static final PropertyDescriptor thresholdDescriptor = new IntegerProperty(
"threshold", "Coupling threshold value", 2, 1.0f
);
private static final Map propertyDescriptorsByName = asFixedMap(thresholdDescriptor);
public Object visit(ASTCompilationUnit cu, Object data) {
this.typesFoundSoFar = new HashSet();
this.couplingCount = 0;
typesFoundSoFar = new HashSet();
couplingCount = 0;
Object returnObj = cu.childrenAccept(this, data);
if (this.couplingCount > getIntProperty("threshold")) {
addViolation(data, cu, "A value of " + this.couplingCount + " may denote a high amount of coupling within the class");
if (couplingCount > getIntProperty(thresholdDescriptor)) {
addViolation(data, cu, "A value of " + couplingCount + " may denote a high amount of coupling within the class");
}
return returnObj;
@ -62,7 +72,7 @@ public class CouplingBetweenObjects extends AbstractRule {
if (reftypeNode instanceof ASTReferenceType) {
SimpleNode classOrIntType = (SimpleNode) reftypeNode.jjtGetChild(0);
if (classOrIntType instanceof ASTClassOrInterfaceType) {
SimpleNode nameNode = (ASTClassOrInterfaceType) classOrIntType;
SimpleNode nameNode = classOrIntType;
this.checkVariableType(nameNode, nameNode.getImage());
}
}
@ -72,12 +82,12 @@ public class CouplingBetweenObjects extends AbstractRule {
}
public Object visit(ASTLocalVariableDeclaration node, Object data) {
this.handleASTTypeChildren(node);
handleASTTypeChildren(node);
return super.visit(node, data);
}
public Object visit(ASTFormalParameter node, Object data) {
this.handleASTTypeChildren(node);
handleASTTypeChildren(node);
return super.visit(node, data);
}
@ -87,7 +97,7 @@ public class CouplingBetweenObjects extends AbstractRule {
if (firstStmt instanceof ASTType) {
ASTType tp = (ASTType) firstStmt;
SimpleNode nd = (SimpleNode) tp.jjtGetChild(0);
this.checkVariableType(nd, nd.getImage());
checkVariableType(nd, nd.getImage());
}
}
@ -103,7 +113,7 @@ public class CouplingBetweenObjects extends AbstractRule {
SimpleNode sNode = (SimpleNode) node.jjtGetChild(x);
if (sNode instanceof ASTType) {
SimpleNode nameNode = (SimpleNode) sNode.jjtGetChild(0);
this.checkVariableType(nameNode, nameNode.getImage());
checkVariableType(nameNode, nameNode.getImage());
}
}
}
@ -123,8 +133,8 @@ public class CouplingBetweenObjects extends AbstractRule {
//increment the count
ClassScope clzScope = nameNode.getScope().getEnclosingClassScope();
if (!clzScope.getClassName().equals(variableType) && (!this.filterTypes(variableType)) && !this.typesFoundSoFar.contains(variableType)) {
this.couplingCount++;
this.typesFoundSoFar.add(variableType);
couplingCount++;
typesFoundSoFar.add(variableType);
}
}
@ -146,4 +156,11 @@ public class CouplingBetweenObjects extends AbstractRule {
private boolean filterPrimitivesAndWrappers(String variableType) {
return (variableType.equals("int") || variableType.equals("Integer") || variableType.equals("char") || variableType.equals("Character") || variableType.equalsIgnoreCase("double") || variableType.equalsIgnoreCase("long") || variableType.equalsIgnoreCase("short") || variableType.equalsIgnoreCase("float") || variableType.equalsIgnoreCase("byte") || variableType.equalsIgnoreCase("boolean"));
}
/**
* @return Map
*/
protected Map propertiesByName() {
return propertyDescriptorsByName;
}
}