Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 145   Methods: 10
NCLOC: 91   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CouplingBetweenObjects.java 45.5% 61% 60% 56.2%
coverage coverage
 1    /**
 2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 3    */
 4    package net.sourceforge.pmd.rules;
 5   
 6    import net.sourceforge.pmd.AbstractRule;
 7    import net.sourceforge.pmd.RuleContext;
 8    import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
 9    import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
 10    import net.sourceforge.pmd.ast.ASTCompilationUnit;
 11    import net.sourceforge.pmd.ast.ASTFieldDeclaration;
 12    import net.sourceforge.pmd.ast.ASTFormalParameter;
 13    import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
 14    import net.sourceforge.pmd.ast.ASTReferenceType;
 15    import net.sourceforge.pmd.ast.ASTResultType;
 16    import net.sourceforge.pmd.ast.ASTType;
 17    import net.sourceforge.pmd.ast.SimpleNode;
 18   
 19    import java.util.HashSet;
 20    import java.util.Set;
 21   
 22   
 23    /**
 24    * CouplingBetweenObjects attempts to capture all unique Class attributes,
 25    * local variables, and return types to determine how many objects a class is
 26    * coupled to. This is only a guage and isn't a hard and fast rule. The threshold
 27    * value is configurable and should be determined accordingly
 28    *
 29    * @author aglover
 30    * @since Feb 20, 2003
 31    */
 32    public class CouplingBetweenObjects extends AbstractRule {
 33   
 34    private int couplingCount;
 35    private Set typesFoundSoFar;
 36   
 37  3 public Object visit(ASTCompilationUnit cu, Object data) {
 38  3 this.typesFoundSoFar = new HashSet();
 39  3 this.couplingCount = 0;
 40   
 41  3 Object returnObj = cu.childrenAccept(this, data);
 42   
 43  3 if (this.couplingCount > getIntProperty("threshold")) {
 44  1 RuleContext ctx = (RuleContext) data;
 45  1 ctx.getReport().addRuleViolation(createRuleViolation(ctx, cu, "A value of " + this.couplingCount + " may denote a high amount of coupling within the class"));
 46    }
 47   
 48  3 return returnObj;
 49    }
 50   
 51  3 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
 52  3 if (node.isInterface()) {
 53  1 return data;
 54    }
 55  2 return super.visit(node, data);
 56    }
 57   
 58  3 public Object visit(ASTResultType node, Object data) {
 59  3 for (int x = 0; x < node.jjtGetNumChildren(); x++) {
 60  3 SimpleNode tNode = (SimpleNode) node.jjtGetChild(x);
 61  3 if (tNode instanceof ASTType) {
 62  3 SimpleNode reftypeNode = (SimpleNode) tNode.jjtGetChild(0);
 63  3 if (reftypeNode instanceof ASTReferenceType) {
 64  3 SimpleNode classOrIntType = (SimpleNode)reftypeNode.jjtGetChild(0);
 65  3 if (classOrIntType instanceof ASTClassOrInterfaceType) {
 66  3 SimpleNode nameNode = (ASTClassOrInterfaceType)classOrIntType;
 67  3 this.checkVariableType(nameNode, nameNode.getImage());
 68    }
 69    }
 70    }
 71    }
 72  3 return super.visit(node, data);
 73    }
 74   
 75  0 public Object visit(ASTLocalVariableDeclaration node, Object data) {
 76  0 this.handleASTTypeChildren(node);
 77  0 return super.visit(node, data);
 78    }
 79   
 80  0 public Object visit(ASTFormalParameter node, Object data) {
 81  0 this.handleASTTypeChildren(node);
 82  0 return super.visit(node, data);
 83    }
 84   
 85  0 public Object visit(ASTFieldDeclaration node, Object data) {
 86  0 for (int x = 0; x < node.jjtGetNumChildren(); ++x) {
 87  0 SimpleNode firstStmt = (SimpleNode) node.jjtGetChild(x);
 88  0 if (firstStmt instanceof ASTType) {
 89  0 ASTType tp = (ASTType) firstStmt;
 90  0 SimpleNode nd = (SimpleNode) tp.jjtGetChild(0);
 91  0 this.checkVariableType(nd, nd.getImage());
 92    }
 93    }
 94   
 95  0 return super.visit(node, data);
 96    }
 97   
 98    /**
 99    * convience method to handle hierarchy. This is probably too much
 100    * work and will go away once I figure out the framework
 101    */
 102  0 private void handleASTTypeChildren(SimpleNode node) {
 103  0 for (int x = 0; x < node.jjtGetNumChildren(); x++) {
 104  0 SimpleNode sNode = (SimpleNode) node.jjtGetChild(x);
 105  0 if (sNode instanceof ASTType) {
 106  0 SimpleNode nameNode = (SimpleNode) sNode.jjtGetChild(0);
 107  0 this.checkVariableType(nameNode, nameNode.getImage());
 108    }
 109    }
 110    }
 111   
 112    /**
 113    * performs a check on the variable and updates the couter. Counter is
 114    * instance for a class and is reset upon new class scan.
 115    *
 116    * @param String variableType
 117    */
 118  3 private void checkVariableType(SimpleNode nameNode, String variableType) {
 119    //if the field is of any type other than the class type
 120    //increment the count
 121  3 if (!nameNode.getScope().getEnclosingClassScope().getClassName().equals(variableType) && (!this.filterTypes(variableType)) && !this.typesFoundSoFar.contains(variableType)) {
 122  3 this.couplingCount++;
 123  3 this.typesFoundSoFar.add(variableType);
 124    }
 125    }
 126   
 127    /**
 128    * Filters variable type - we don't want primatives, wrappers, strings, etc.
 129    * This needs more work. I'd like to filter out super types and perhaps interfaces
 130    *
 131    * @param String variableType
 132    * @return boolean true if variableType is not what we care about
 133    */
 134  3 private boolean filterTypes(String variableType) {
 135  3 return variableType != null && (variableType.startsWith("java.lang.") || (variableType.equals("String")) || filterPrimitivesAndWrappers(variableType));
 136    }
 137   
 138    /**
 139    * @param String variableType
 140    * @return boolean true if variableType is a primative or wrapper
 141    */
 142  3 private boolean filterPrimitivesAndWrappers(String variableType) {
 143  3 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"));
 144    }
 145    }