Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 69   Methods: 5
NCLOC: 54   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NullAssignmentRule.java 83.3% 100% 100% 93.5%
coverage coverage
 1    /**
 2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 3    */
 4    package net.sourceforge.pmd.rules.design;
 5   
 6    import net.sourceforge.pmd.AbstractRule;
 7    import net.sourceforge.pmd.ast.ASTAssignmentOperator;
 8    import net.sourceforge.pmd.ast.ASTConditionalExpression;
 9    import net.sourceforge.pmd.ast.ASTEqualityExpression;
 10    import net.sourceforge.pmd.ast.ASTName;
 11    import net.sourceforge.pmd.ast.ASTNullLiteral;
 12    import net.sourceforge.pmd.ast.ASTStatementExpression;
 13    import net.sourceforge.pmd.ast.Node;
 14    import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
 15   
 16    import java.util.Iterator;
 17    import java.util.Map;
 18   
 19    // Would this be simplified by using DFA somehow?
 20    public class NullAssignmentRule extends AbstractRule {
 21   
 22  9 public Object visit(ASTNullLiteral node, Object data) {
 23  9 if (get5thParent(node) instanceof ASTStatementExpression) {
 24  2 ASTStatementExpression n = (ASTStatementExpression)get5thParent(node);
 25   
 26  2 if (isAssignmentToFinalField(n)) {
 27  1 return data;
 28    }
 29   
 30  1 if (n.jjtGetNumChildren() > 2 && n.jjtGetChild(1) instanceof ASTAssignmentOperator) {
 31  1 addViolation(data, node);
 32    }
 33  7 } else if (get4thParent(node) instanceof ASTConditionalExpression) {
 34  1 checkTernary((ASTConditionalExpression)get4thParent(node), data, node);
 35  6 } else if (get5thParent(node) instanceof ASTConditionalExpression) {
 36  2 checkTernary((ASTConditionalExpression)get5thParent(node), data, node);
 37    }
 38   
 39  8 return data;
 40    }
 41   
 42  2 private boolean isAssignmentToFinalField(ASTStatementExpression n) {
 43  2 ASTName name = (ASTName)n.getFirstChildOfType(ASTName.class);
 44  2 if (name != null) {
 45  2 Map vars = name.getScope().getEnclosingClassScope().getVariableDeclarations();
 46  2 for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
 47  1 VariableNameDeclaration vnd = (VariableNameDeclaration)i.next();
 48  1 if (vnd.getImage().equals(name.getImage()) && vnd.getAccessNodeParent().isFinal()) {
 49  1 return true;
 50    }
 51    }
 52    }
 53  1 return false;
 54    }
 55   
 56  27 private Node get4thParent(ASTNullLiteral node) {
 57  27 return node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent();
 58    }
 59   
 60  19 private Node get5thParent(ASTNullLiteral node) {
 61  19 return get4thParent(node).jjtGetParent();
 62    }
 63   
 64  3 private void checkTernary(ASTConditionalExpression n, Object data, ASTNullLiteral node) {
 65  3 if (n.isTernary() && !(n.jjtGetChild(0) instanceof ASTEqualityExpression)) {
 66  2 addViolation(data, node);
 67    }
 68    }
 69    }