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
64
65
66 @FileStatefulCheck
67 public class EqualsHashCodeCheck
68 extends AbstractCheck {
69
70
71
72
73
74
75
76
77 public static final String MSG_KEY_HASHCODE = "equals.noHashCode";
78
79
80
81
82
83 public static final String MSG_KEY_EQUALS = "equals.noEquals";
84
85
86 private final Map<DetailAST, DetailAST> objBlockWithEquals = new HashMap<>();
87
88
89 private final Map<DetailAST, DetailAST> objBlockWithHashCode = new HashMap<>();
90
91 @Override
92 public int[] getDefaultTokens() {
93 return getRequiredTokens();
94 }
95
96 @Override
97 public int[] getAcceptableTokens() {
98 return getRequiredTokens();
99 }
100
101 @Override
102 public int[] getRequiredTokens() {
103 return new int[] {TokenTypes.METHOD_DEF};
104 }
105
106 @Override
107 public void beginTree(DetailAST rootAST) {
108 objBlockWithEquals.clear();
109 objBlockWithHashCode.clear();
110 }
111
112 @Override
113 public void visitToken(DetailAST ast) {
114 if (isEqualsMethod(ast)) {
115 objBlockWithEquals.put(ast.getParent(), ast);
116 }
117 else if (isHashCodeMethod(ast)) {
118 objBlockWithHashCode.put(ast.getParent(), ast);
119 }
120 }
121
122
123
124
125
126
127
128 private static boolean isEqualsMethod(DetailAST ast) {
129 final DetailAST modifiers = ast.getFirstChild();
130 final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
131
132 return CheckUtil.isEqualsMethod(ast)
133 && isObjectParam(parameters.getFirstChild())
134 && (ast.findFirstToken(TokenTypes.SLIST) != null
135 || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
136 }
137
138
139
140
141
142
143
144 private static boolean isHashCodeMethod(DetailAST ast) {
145 final DetailAST modifiers = ast.getFirstChild();
146 final DetailAST methodName = ast.findFirstToken(TokenTypes.IDENT);
147 final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
148
149 return "hashCode".equals(methodName.getText())
150 && parameters.getFirstChild() == null
151 && (ast.findFirstToken(TokenTypes.SLIST) != null
152 || modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) != null);
153 }
154
155
156
157
158
159
160
161 private static boolean isObjectParam(DetailAST paramNode) {
162 final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE);
163 final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode);
164 final String name = fullIdent.getText();
165 return "Object".equals(name) || "java.lang.Object".equals(name);
166 }
167
168 @Override
169 public void finishTree(DetailAST rootAST) {
170 objBlockWithEquals
171 .entrySet().stream().filter(detailASTDetailASTEntry -> {
172 return objBlockWithHashCode.remove(detailASTDetailASTEntry.getKey()) == null;
173 }).forEach(detailASTDetailASTEntry -> {
174 final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
175 log(equalsAST, MSG_KEY_HASHCODE);
176 });
177 objBlockWithHashCode.forEach((key, equalsAST) -> log(equalsAST, MSG_KEY_EQUALS));
178 }
179
180 }