Clover coverage report - PMD - 3.7
Coverage timestamp: Wed May 31 2006 09:25:59 EDT

file stats: | LOC: | 51 |  | Methods: | 3
| NCLOC: | 38 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

NullAssignmentRule.java

91.7%

100%

100%

96.7%

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.symboltable.VariableNameDeclaration;
14 |  |
15 |  | // Would this be simplified by using DFA somehow?
16 |  |
17 |  | public class NullAssignmentRule extends AbstractRule {
18 |  |
19 | 9 | public Object visit(ASTNullLiteral node, Object data) {
20 | 9 | if (node.getNthParent(5) instanceof ASTStatementExpression) {
21 | 2 | ASTStatementExpression n = (ASTStatementExpression) node.getNthParent(5);
22 |  |
23 | 2 | if (isAssignmentToFinalField(n)) {
24 | 1 | return data;
25 |  | }
26 |  |
27 | 1 | if (n.jjtGetNumChildren() > 2 && n.jjtGetChild(1) instanceof ASTAssignmentOperator) {
28 | 1 | addViolation(data, node);
29 |  | }
30 | 7 | } else if (node.getNthParent(4) instanceof ASTConditionalExpression) {
31 | 1 | checkTernary((ASTConditionalExpression) node.getNthParent(4), data, node);
32 | 6 | } else if (node.getNthParent(5) instanceof ASTConditionalExpression) {
33 | 2 | checkTernary((ASTConditionalExpression) node.getNthParent(5), data, node);
34 |  | }
35 |  |
36 | 8 | return data;
37 |  | }
38 |  |
39 | 2 | private boolean isAssignmentToFinalField(ASTStatementExpression n) {
40 | 2 | ASTName name = (ASTName) n.getFirstChildOfType(ASTName.class);
41 | 2 | return name != null
42 |  | && name.getNameDeclaration() instanceof VariableNameDeclaration
43 |  | && ((VariableNameDeclaration) name.getNameDeclaration()).getAccessNodeParent().isFinal();
44 |  | }
45 |  |
46 | 3 | private void checkTernary(ASTConditionalExpression n, Object data, ASTNullLiteral node) {
47 | 3 | if (n.isTernary() && !(n.jjtGetChild(0) instanceof ASTEqualityExpression)) {
48 | 2 | addViolation(data, node);
49 |  | }
50 |  | }
51 |  | }