|
1 |
| package net.sourceforge.pmd.rules; |
|
2 |
| |
|
3 |
| import net.sourceforge.pmd.AbstractRule; |
|
4 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
5 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceType; |
|
6 |
| import net.sourceforge.pmd.ast.ASTFormalParameter; |
|
7 |
| import net.sourceforge.pmd.ast.ASTFormalParameters; |
|
8 |
| import net.sourceforge.pmd.ast.ASTImplementsList; |
|
9 |
| import net.sourceforge.pmd.ast.ASTMethodDeclarator; |
|
10 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
11 |
| |
|
12 |
| import java.util.List; |
|
13 |
| |
|
14 |
| public class OverrideBothEqualsAndHashcode extends AbstractRule { |
|
15 |
| |
|
16 |
| private boolean implementsComparable = false; |
|
17 |
| |
|
18 |
| private boolean containsEquals = false; |
|
19 |
| |
|
20 |
| private boolean containsHashCode = false; |
|
21 |
| |
|
22 |
| private SimpleNode nodeFound = null; |
|
23 |
| |
|
24 |
14
| public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
|
25 |
14
| if (node.isInterface()) {
|
|
26 |
1
| return data;
|
|
27 |
| } |
|
28 |
13
| super.visit(node, data);
|
|
29 |
13
| if (!implementsComparable && (containsEquals ^ containsHashCode)) {
|
|
30 |
5
| if(nodeFound == null){
|
|
31 |
0
| nodeFound = node;
|
|
32 |
| } |
|
33 |
5
| addViolation(data, nodeFound);
|
|
34 |
| } |
|
35 |
13
| implementsComparable = containsEquals = containsHashCode = false;
|
|
36 |
13
| nodeFound = null;
|
|
37 |
13
| return data;
|
|
38 |
| } |
|
39 |
| |
|
40 |
2
| public Object visit(ASTImplementsList node, Object data) {
|
|
41 |
2
| for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
|
|
42 |
2
| if (node.jjtGetChild(ix).getClass().equals(ASTClassOrInterfaceType.class)
|
|
43 |
| && ((SimpleNode) node.jjtGetChild(ix)).hasImageEqualTo("Comparable")) { |
|
44 |
1
| implementsComparable = true;
|
|
45 |
1
| return data;
|
|
46 |
| } |
|
47 |
| } |
|
48 |
1
| return super.visit(node, data);
|
|
49 |
| } |
|
50 |
| |
|
51 |
20
| public Object visit(ASTMethodDeclarator node, Object data) {
|
|
52 |
20
| if (implementsComparable) {
|
|
53 |
2
| return data;
|
|
54 |
| } |
|
55 |
| |
|
56 |
18
| int iFormalParams = 0;
|
|
57 |
18
| String paramName = null;
|
|
58 |
18
| for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
|
|
59 |
18
| SimpleNode sn = (SimpleNode) node.jjtGetChild(ix);
|
|
60 |
18
| if (sn.getClass().equals(ASTFormalParameters.class)) {
|
|
61 |
18
| List allParams = ((ASTFormalParameters) sn).findChildrenOfType(ASTFormalParameter.class);
|
|
62 |
18
| for (int i = 0; i < allParams.size(); i++) {
|
|
63 |
16
| iFormalParams++;
|
|
64 |
16
| ASTFormalParameter formalParam = (ASTFormalParameter) allParams.get(i);
|
|
65 |
16
| ASTClassOrInterfaceType param = (ASTClassOrInterfaceType) formalParam.getFirstChildOfType(ASTClassOrInterfaceType.class);
|
|
66 |
16
| if (param != null) {
|
|
67 |
15
| paramName = param.getImage();
|
|
68 |
| } |
|
69 |
| } |
|
70 |
| } |
|
71 |
| } |
|
72 |
| |
|
73 |
18
| if (iFormalParams == 0 && node.hasImageEqualTo("hashCode")) {
|
|
74 |
4
| containsHashCode = true;
|
|
75 |
4
| nodeFound = node;
|
|
76 |
14
| } else if (iFormalParams == 1 && node.hasImageEqualTo("equals") && ("Object".equals(paramName) || "java.lang.Object".equals(paramName))) {
|
|
77 |
5
| containsEquals = true;
|
|
78 |
5
| nodeFound = node;
|
|
79 |
| } |
|
80 |
18
| return super.visit(node, data);
|
|
81 |
| } |
|
82 |
| |
|
83 |
| } |