1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
26 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27 import com.puppycrawl.tools.checkstyle.api.DetailAST;
28 import com.puppycrawl.tools.checkstyle.api.FullIdent;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 @FileStatefulCheck
64 public class EqualsHashCodeCheck
65 extends AbstractCheck {
66
67
68
69
70
71
72
73
74 public static final String MSG_KEY_HASHCODE = "equals.noHashCode";
75
76
77
78
79
80 public static final String MSG_KEY_EQUALS = "equals.noEquals";
81
82
83 private final Map<DetailAST, DetailAST> objBlockWithEquals = new HashMap<>();
84
85
86 private final Map<DetailAST, DetailAST> objBlockWithHashCode = new HashMap<>();
87
88 @Override
89 public int[] getDefaultTokens() {
90 return getRequiredTokens();
91 }
92
93 @Override
94 public int[] getAcceptableTokens() {
95 return getRequiredTokens();
96 }
97
98 @Override
99 public int[] getRequiredTokens() {
100 return new int[] {TokenTypes.METHOD_DEF};
101 }
102
103 @Override
104 public void beginTree(DetailAST rootAST) {
105 objBlockWithEquals.clear();
106 objBlockWithHashCode.clear();
107 }
108
109 @Override
110 public void visitToken(DetailAST ast) {
111 if (isEqualsMethod(ast)) {
112 objBlockWithEquals.put(ast.getParent(), ast);
113 }
114 else if (isHashCodeMethod(ast)) {
115 objBlockWithHashCode.put(ast.getParent(), ast);
116 }
117 }
118
119
120
121
122
123
124
125 private static boolean isEqualsMethod(DetailAST ast) {
126 final DetailAST modifiers = ast.getFirstChild();
127 final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
128
129 return CheckUtil.isEqualsMethod(ast)
130 && isObjectParam(parameters.getFirstChild())
131 && (ast.findFirstToken(TokenTypes.SLIST) != null
132 || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
133 }
134
135
136
137
138
139
140
141 private static boolean isHashCodeMethod(DetailAST ast) {
142 final DetailAST modifiers = ast.getFirstChild();
143 final DetailAST methodName = ast.findFirstToken(TokenTypes.IDENT);
144 final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
145
146 return "hashCode".equals(methodName.getText())
147 && parameters.getFirstChild() == null
148 && (ast.findFirstToken(TokenTypes.SLIST) != null
149 || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
150 }
151
152
153
154
155
156
157
158 private static boolean isObjectParam(DetailAST paramNode) {
159 final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE);
160 final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode);
161 final String name = fullIdent.getText();
162 return "Object".equals(name) || "java.lang.Object".equals(name);
163 }
164
165 @Override
166 public void finishTree(DetailAST rootAST) {
167 objBlockWithEquals
168 .entrySet().stream().filter(detailASTDetailASTEntry -> {
169 return objBlockWithHashCode.remove(detailASTDetailASTEntry.getKey()) == null;
170 }).forEach(detailASTDetailASTEntry -> {
171 final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
172 log(equalsAST, MSG_KEY_HASHCODE);
173 });
174 objBlockWithHashCode.forEach((key, equalsAST) -> log(equalsAST, MSG_KEY_EQUALS));
175 }
176
177 }