|
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 |
| |
|
19 |
9
| if (!hasName(((SimpleNode) node.jjtGetChild(0)).jjtGetChild(0)) || !hasName(((SimpleNode) node.jjtGetChild(1)).jjtGetChild(0))) {
|
|
20 |
3
| return data;
|
|
21 |
| } |
|
22 |
| |
|
23 |
| |
|
24 |
6
| if (isQualifiedName((SimpleNode) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0))
|
|
25 |
| || isQualifiedName((SimpleNode) node.jjtGetChild(1).jjtGetChild(0).jjtGetChild(0))) { |
|
26 |
1
| return data;
|
|
27 |
| } |
|
28 |
| |
|
29 |
| |
|
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 |
| |
|
42 |
| |
|
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 |
| } |