Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 54 |  | Methods: | 2
| NCLOC: | 38 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
CompareObjectsWithEquals.java | 83.3% | 94.1% | 100% | 90.3% |

1 |  | package net.sourceforge.pmd.rules.design;
2 |  |
3 |  | import net.sourceforge.pmd.AbstractRule;
4 |  | import net.sourceforge.pmd.ast.ASTEqualityExpression;
5 |  | import net.sourceforge.pmd.ast.ASTInitializer;
6 |  | import net.sourceforge.pmd.ast.ASTName;
7 |  | import net.sourceforge.pmd.ast.Node;
8 |  | import net.sourceforge.pmd.ast.SimpleNode;
9 |  | import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
10 |  |
11 |  | public class CompareObjectsWithEquals extends AbstractRule {
12 |  |
13 | 16 | private boolean hasName(Node n) {
14 | 16 | return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTName;
15 |  | }
16 |  |
17 | 9 | public Object visit(ASTEqualityExpression node, Object data) {
18 |  | // skip if either child is not a simple name
19 | 9 | if (!hasName(((SimpleNode) node.jjtGetChild(0)).jjtGetChild(0)) || !hasName(((SimpleNode) node.jjtGetChild(1)).jjtGetChild(0))) {
20 | 3 | return data;
21 |  | }
22 |  |
23 |  | // skip if either is a qualified name
24 | 6 | if (((SimpleNode) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).getImage().indexOf(".") != -1
25 |  | || ((SimpleNode) node.jjtGetChild(1).jjtGetChild(0).jjtGetChild(0)).getImage().indexOf(".") != -1) {
26 | 1 | return data;
27 |  | }
28 |  |
29 |  | // skip static initializers... missing some cases here
30 | 5 | if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
31 | 0 | return data;
32 |  | }
33 |  |
34 | 5 | ASTName n0 = (ASTName) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
35 | 5 | ASTName n1 = (ASTName) node.jjtGetChild(1).jjtGetChild(0).jjtGetChild(0);
36 |  |
37 | 5 | if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) {
38 | 5 | VariableNameDeclaration nd0 = (VariableNameDeclaration) n0.getNameDeclaration();
39 | 5 | VariableNameDeclaration nd1 = (VariableNameDeclaration) n1.getNameDeclaration();
40 |  |
41 |  | // skip array dereferences... this misses some cases
42 |  | // FIXME catch comparisons btwn array elements of reference types
43 | 5 | if (nd0.isArray() || nd1.isArray()) {
44 | 1 | return data;
45 |  | }
46 |  |
47 | 4 | if (nd0.isReferenceType() && nd1.isReferenceType()) {
48 | 3 | addViolation(data, node);
49 |  | }
50 |  | }
51 |  |
52 | 4 | return data;
53 |  | }
54 |  | }