Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 160   Methods: 11
NCLOC: 130   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
CyclomaticComplexity.java 69.2% 98.6% 100% 91.6%
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.RuleViolation;
 9    import net.sourceforge.pmd.ast.ASTBlockStatement;
 10    import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
 11    import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
 12    import net.sourceforge.pmd.ast.ASTForStatement;
 13    import net.sourceforge.pmd.ast.ASTIfStatement;
 14    import net.sourceforge.pmd.ast.ASTMethodDeclaration;
 15    import net.sourceforge.pmd.ast.ASTMethodDeclarator;
 16    import net.sourceforge.pmd.ast.ASTSwitchLabel;
 17    import net.sourceforge.pmd.ast.ASTSwitchStatement;
 18    import net.sourceforge.pmd.ast.ASTWhileStatement;
 19    import net.sourceforge.pmd.ast.Node;
 20    import net.sourceforge.pmd.ast.SimpleNode;
 21   
 22    import java.text.MessageFormat;
 23    import java.util.Stack;
 24   
 25    /**
 26    * @author Donald A. Leckie
 27    * @version $Revision: 1.5 $, $Date: 2005/03/24 21:53:50 $
 28    * @since January 14, 2003
 29    */
 30    public class CyclomaticComplexity extends AbstractRule {
 31   
 32    private static class Entry {
 33    private SimpleNode node;
 34    private int decisionPoints = 1;
 35    public int highestDecisionPoints;
 36    public int methodCount;
 37   
 38  8 private Entry(SimpleNode node) {
 39  8 this.node = node;
 40    }
 41   
 42  11 public void bumpDecisionPoints() {
 43  11 decisionPoints++;
 44    }
 45   
 46  3 public void bumpDecisionPoints(int size) {
 47  3 decisionPoints += size;
 48    }
 49   
 50  7 public int getComplexityAverage() {
 51  7 return ((double) methodCount == 0) ? 1 : (int) (Math.rint((double) decisionPoints / (double) methodCount));
 52    }
 53    }
 54   
 55    private Stack entryStack = new Stack();
 56   
 57  5 public Object visit(ASTIfStatement node, Object data) {
 58  5 ((Entry) entryStack.peek()).bumpDecisionPoints();
 59  5 super.visit(node, data);
 60  5 return data;
 61    }
 62   
 63  1 public Object visit(ASTForStatement node, Object data) {
 64  1 ((Entry) entryStack.peek()).bumpDecisionPoints();
 65  1 super.visit(node, data);
 66  1 return data;
 67    }
 68   
 69  1 public Object visit(ASTSwitchStatement node, Object data) {
 70  1 Entry entry = (Entry) entryStack.peek();
 71  1 int childCount = node.jjtGetNumChildren();
 72  1 int lastIndex = childCount - 1;
 73  1 for (int n = 0; n < lastIndex; n++) {
 74  12 Node childNode = node.jjtGetChild(n);
 75  12 if (childNode instanceof ASTSwitchLabel) {
 76  4 childNode = node.jjtGetChild(n + 1);
 77  4 if (childNode instanceof ASTBlockStatement) {
 78  4 entry.bumpDecisionPoints();
 79    }
 80    }
 81    }
 82  1 super.visit(node, data);
 83  1 return data;
 84    }
 85   
 86  1 public Object visit(ASTWhileStatement node, Object data) {
 87  1 ((Entry) entryStack.peek()).bumpDecisionPoints();
 88  1 super.visit(node, data);
 89  1 return data;
 90    }
 91   
 92  4 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
 93  4 if (node.isInterface()) {
 94  0 return data;
 95    }
 96   
 97  4 entryStack.push(new Entry(node));
 98  4 super.visit(node, data);
 99  4 Entry classEntry = (Entry) entryStack.pop();
 100  4 if ((classEntry.getComplexityAverage() >= getIntProperty("reportLevel")) || (classEntry.highestDecisionPoints >= getIntProperty("reportLevel"))) {
 101  3 RuleContext ruleContext = (RuleContext) data;
 102  3 String[] args = {"class", node.getImage(), String.valueOf(classEntry.getComplexityAverage()) + " (Highest = " + String.valueOf(classEntry.highestDecisionPoints) + ")"};
 103  3 RuleViolation ruleViolation = createRuleViolation(ruleContext, node, MessageFormat.format(getMessage(), args));
 104  3 ruleContext.getReport().addRuleViolation(ruleViolation);
 105    }
 106  4 return data;
 107    }
 108   
 109  3 public Object visit(ASTMethodDeclaration node, Object data) {
 110  3 entryStack.push(new Entry(node));
 111  3 super.visit(node, data);
 112  3 Entry methodEntry = (Entry) entryStack.pop();
 113  3 int methodDecisionPoints = methodEntry.decisionPoints;
 114  3 Entry classEntry = (Entry) entryStack.peek();
 115  3 classEntry.methodCount++;
 116  3 classEntry.bumpDecisionPoints(methodDecisionPoints);
 117   
 118  3 if (methodDecisionPoints > classEntry.highestDecisionPoints) {
 119  3 classEntry.highestDecisionPoints = methodDecisionPoints;
 120    }
 121   
 122  3 ASTMethodDeclarator methodDeclarator = null;
 123  6 for (int n = 0; n < node.jjtGetNumChildren(); n++) {
 124  6 Node childNode = node.jjtGetChild(n);
 125  6 if (childNode instanceof ASTMethodDeclarator) {
 126  3 methodDeclarator = (ASTMethodDeclarator) childNode;
 127  3 break;
 128    }
 129    }
 130   
 131  3 if (methodEntry.decisionPoints >= getIntProperty("reportLevel")) {
 132  2 RuleContext ruleContext = (RuleContext) data;
 133  2 String[] args = {"method", (methodDeclarator == null) ? "" : methodDeclarator.getImage(), String.valueOf(methodEntry.decisionPoints)};
 134  2 ruleContext.getReport().addRuleViolation(createRuleViolation(ruleContext, node, MessageFormat.format(getMessage(), args)));
 135    }
 136   
 137  3 return data;
 138    }
 139   
 140  1 public Object visit(ASTConstructorDeclaration node, Object data) {
 141  1 entryStack.push(new Entry(node));
 142  1 super.visit(node, data);
 143  1 Entry constructorEntry = (Entry) entryStack.pop();
 144  1 int constructorDecisionPointCount = constructorEntry.decisionPoints;
 145  1 Entry classEntry = (Entry) entryStack.peek();
 146  1 classEntry.methodCount++;
 147  1 classEntry.decisionPoints += constructorDecisionPointCount;
 148  1 if (constructorDecisionPointCount > classEntry.highestDecisionPoints) {
 149  1 classEntry.highestDecisionPoints = constructorDecisionPointCount;
 150    }
 151  1 if (constructorEntry.decisionPoints >= getIntProperty("reportLevel")) {
 152  1 RuleContext ruleContext = (RuleContext) data;
 153  1 String[] args = {"constructor", classEntry.node.getImage(), String.valueOf(constructorDecisionPointCount)};
 154  1 RuleViolation ruleViolation = createRuleViolation(ruleContext, node, MessageFormat.format(getMessage(), args));
 155  1 ruleContext.getReport().addRuleViolation(ruleViolation);
 156    }
 157  1 return data;
 158    }
 159   
 160    }