-
Notifications
You must be signed in to change notification settings - Fork 638
/
EqualsAndHashCodeProcessor.java
363 lines (310 loc) · 19.1 KB
/
EqualsAndHashCodeProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package de.plushnikov.intellij.plugin.processor.clazz;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTypesUtil;
import de.plushnikov.intellij.plugin.lombokconfig.ConfigDiscovery;
import de.plushnikov.intellij.plugin.lombokconfig.ConfigKey;
import de.plushnikov.intellij.plugin.problem.ProblemBuilder;
import de.plushnikov.intellij.plugin.processor.LombokPsiElementUsage;
import de.plushnikov.intellij.plugin.processor.handler.EqualsAndHashCodeToStringHandler;
import de.plushnikov.intellij.plugin.processor.handler.EqualsAndHashCodeToStringHandler.MemberInfo;
import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder;
import de.plushnikov.intellij.plugin.quickfix.PsiQuickFixFactory;
import de.plushnikov.intellij.plugin.util.PsiAnnotationSearchUtil;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import de.plushnikov.intellij.plugin.util.PsiClassUtil;
import de.plushnikov.intellij.plugin.util.PsiMethodUtil;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Inspect and validate @EqualsAndHashCode lombok annotation on a class
* Creates equals/hashcode method for fields of this class
*
* @author Plushnikov Michail
*/
public class EqualsAndHashCodeProcessor extends AbstractClassProcessor {
private static final String EQUALS_METHOD_NAME = "equals";
private static final String HASH_CODE_METHOD_NAME = "hashCode";
private static final String CAN_EQUAL_METHOD_NAME = "canEqual";
private static final String INCLUDE_ANNOTATION_METHOD = "replaces";
private static final String EQUALSANDHASHCODE_INCLUDE = EqualsAndHashCode.Include.class.getName().replace("$", ".");
private static final String EQUALSANDHASHCODE_EXCLUDE = EqualsAndHashCode.Exclude.class.getName().replace("$", ".");
private final EqualsAndHashCodeToStringHandler handler;
public EqualsAndHashCodeProcessor() {
super(PsiMethod.class, EqualsAndHashCode.class);
handler = new EqualsAndHashCodeToStringHandler();
}
@Override
protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) {
final boolean result = validateAnnotationOnRightType(psiClass, builder);
if (result) {
validateExistingMethods(psiClass, builder);
}
final Collection<String> excludeProperty = PsiAnnotationUtil.getAnnotationValues(psiAnnotation, "exclude", String.class);
final Collection<String> ofProperty = PsiAnnotationUtil.getAnnotationValues(psiAnnotation, "of", String.class);
if (!excludeProperty.isEmpty() && !ofProperty.isEmpty()) {
builder.addWarning("exclude and of are mutually exclusive; the 'exclude' parameter will be ignored",
PsiQuickFixFactory.createChangeAnnotationParameterFix(psiAnnotation, "exclude", null));
} else {
validateExcludeParam(psiClass, builder, psiAnnotation, excludeProperty);
}
validateOfParam(psiClass, builder, psiAnnotation, ofProperty);
validateCallSuperParamIntern(psiAnnotation, psiClass, builder);
validateCallSuperParamForObject(psiAnnotation, psiClass, builder);
return result;
}
private void validateCallSuperParamIntern(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) {
validateCallSuperParam(psiAnnotation, psiClass, builder,
PsiQuickFixFactory.createChangeAnnotationParameterFix(psiAnnotation, "callSuper", "false"),
PsiQuickFixFactory.createChangeAnnotationParameterFix(psiAnnotation, "callSuper", "true"));
}
void validateCallSuperParamExtern(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) {
validateCallSuperParam(psiAnnotation, psiClass, builder,
PsiQuickFixFactory.createAddAnnotationQuickFix(psiClass, "lombok.EqualsAndHashCode", "callSuper = true"));
}
private void validateCallSuperParam(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass, @NotNull ProblemBuilder builder, LocalQuickFix... quickFixes) {
final Boolean declaredBooleanAnnotationValue = PsiAnnotationUtil.getDeclaredBooleanAnnotationValue(psiAnnotation, "callSuper");
if (null == declaredBooleanAnnotationValue) {
final String configProperty = ConfigDiscovery.getInstance().getStringLombokConfigProperty(ConfigKey.EQUALSANDHASHCODE_CALL_SUPER, psiClass);
if (!"CALL".equalsIgnoreCase(configProperty) && !"SKIP".equalsIgnoreCase(configProperty) && PsiClassUtil.hasSuperClass(psiClass) && !hasOneOfMethodsDefined(psiClass)) {
builder.addWarning("Generating equals/hashCode implementation but without a call to superclass, " +
"even though this class does not extend java.lang.Object. If this is intentional, add '(callSuper=false)' to your type.",
quickFixes);
}
}
}
private void validateCallSuperParamForObject(PsiAnnotation psiAnnotation, PsiClass psiClass, ProblemBuilder builder) {
boolean callSuperProperty = PsiAnnotationUtil.getBooleanAnnotationValue(psiAnnotation, "callSuper", false);
if (callSuperProperty && !PsiClassUtil.hasSuperClass(psiClass)) {
builder.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless.",
PsiQuickFixFactory.createChangeAnnotationParameterFix(psiAnnotation, "callSuper", "false"),
PsiQuickFixFactory.createChangeAnnotationParameterFix(psiAnnotation, "callSuper", null));
}
}
private boolean validateAnnotationOnRightType(@NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) {
boolean result = true;
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum()) {
builder.addError("@EqualsAndHashCode is only supported on a class type");
result = false;
}
return result;
}
private boolean validateExistingMethods(@NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) {
if (hasOneOfMethodsDefined(psiClass)) {
builder.addWarning("Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated).");
return false;
}
return true;
}
private boolean hasOneOfMethodsDefined(@NotNull PsiClass psiClass) {
final Collection<PsiMethod> classMethodsIntern = PsiClassUtil.collectClassMethodsIntern(psiClass);
return PsiMethodUtil.hasMethodByName(classMethodsIntern, EQUALS_METHOD_NAME, HASH_CODE_METHOD_NAME);
}
protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {
target.addAll(createEqualAndHashCode(psiClass, psiAnnotation));
}
protected Collection<PsiMethod> createEqualAndHashCode(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation) {
if (hasOneOfMethodsDefined(psiClass)) {
return Collections.emptyList();
}
final Collection<MemberInfo> memberInfos = handler.filterFields(psiClass, psiAnnotation, true, INCLUDE_ANNOTATION_METHOD);
final boolean shouldGenerateCanEqual = shouldGenerateCanEqual(psiClass);
Collection<PsiMethod> result = new ArrayList<PsiMethod>(3);
result.add(createEqualsMethod(psiClass, psiAnnotation, shouldGenerateCanEqual, memberInfos));
final Collection<PsiMethod> classMethods = PsiClassUtil.collectClassMethodsIntern(psiClass);
if (shouldGenerateCanEqual && !PsiMethodUtil.hasMethodByName(classMethods, CAN_EQUAL_METHOD_NAME)) {
result.add(createCanEqualMethod(psiClass, psiAnnotation));
}
result.add(createHashCodeMethod(psiClass, psiAnnotation, memberInfos));
return result;
}
@SuppressWarnings("deprecation")
private boolean shouldGenerateCanEqual(@NotNull PsiClass psiClass) {
final boolean isNotDirectDescendantOfObject = PsiClassUtil.hasSuperClass(psiClass);
if (isNotDirectDescendantOfObject) {
return true;
}
final boolean isFinal = psiClass.hasModifierProperty(PsiModifier.FINAL) ||
(PsiAnnotationSearchUtil.isAnnotatedWith(psiClass, Value.class, lombok.experimental.Value.class) && PsiAnnotationSearchUtil.isNotAnnotatedWith(psiClass, NonFinal.class));
return !isFinal;
}
@NotNull
private PsiMethod createEqualsMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean hasCanEqualMethod, Collection<MemberInfo> memberInfos) {
final PsiManager psiManager = psiClass.getManager();
return new LombokLightMethodBuilder(psiManager, EQUALS_METHOD_NAME)
.withModifier(PsiModifier.PUBLIC)
.withMethodReturnType(PsiType.BOOLEAN)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withFinalParameter("o", PsiType.getJavaLangObject(psiManager, GlobalSearchScope.allScope(psiClass.getProject())))
.withBody(createEqualsCodeBlock(psiClass, psiAnnotation, hasCanEqualMethod, memberInfos));
}
@NotNull
private PsiCodeBlock createEqualsCodeBlock(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean hasCanEqualMethod, Collection<MemberInfo> memberInfos) {
final String blockText = createEqualsBlockString(psiClass, psiAnnotation, hasCanEqualMethod, memberInfos);
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
}
@NotNull
private PsiMethod createHashCodeMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, Collection<MemberInfo> memberInfos) {
final PsiManager psiManager = psiClass.getManager();
return new LombokLightMethodBuilder(psiManager, HASH_CODE_METHOD_NAME)
.withModifier(PsiModifier.PUBLIC)
.withMethodReturnType(PsiType.INT)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withBody(createHashCodeBlock(psiClass, psiAnnotation, memberInfos));
}
@NotNull
private PsiCodeBlock createHashCodeBlock(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, Collection<MemberInfo> memberInfos) {
final String blockText = createHashcodeBlockString(psiClass, psiAnnotation, memberInfos);
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
}
@NotNull
private PsiMethod createCanEqualMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation) {
final PsiManager psiManager = psiClass.getManager();
return new LombokLightMethodBuilder(psiManager, CAN_EQUAL_METHOD_NAME)
.withModifier(PsiModifier.PROTECTED)
.withMethodReturnType(PsiType.BOOLEAN)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withFinalParameter("other", PsiType.getJavaLangObject(psiManager, GlobalSearchScope.allScope(psiClass.getProject())))
.withBody(createCanEqualCodeBlock(psiClass));
}
@NotNull
private PsiCodeBlock createCanEqualCodeBlock(@NotNull PsiClass psiClass) {
final String blockText = String.format("return other instanceof %s;", PsiTypesUtil.getClassType(psiClass).getCanonicalText());
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
}
private String createEqualsBlockString(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean hasCanEqualMethod, Collection<MemberInfo> memberInfos) {
final boolean callSuper = readCallSuperAnnotationOrConfigProperty(psiAnnotation, psiClass);
final boolean doNotUseGetters = readAnnotationOrConfigProperty(psiAnnotation, psiClass, "doNotUseGetters", ConfigKey.EQUALSANDHASHCODE_DO_NOT_USE_GETTERS);
final String canonicalClassName = PsiTypesUtil.getClassType(psiClass).getCanonicalText();
final String canonicalWildcardClassName = PsiClassUtil.getWildcardClassType(psiClass).getCanonicalText();
final StringBuilder builder = new StringBuilder();
builder.append("if (o == this) return true;\n");
builder.append("if (!(o instanceof ").append(canonicalClassName).append(")) return false;\n");
builder.append("final ").append(canonicalWildcardClassName).append(" other = (").append(canonicalWildcardClassName).append(")o;\n");
if (hasCanEqualMethod) {
builder.append("if (!other.canEqual((java.lang.Object)this)) return false;\n");
}
if (callSuper) {
builder.append("if (!super.equals(o)) return false;\n");
}
for (MemberInfo memberInfo : memberInfos) {
final String memberAccessor = handler.getMemberAccessorName(memberInfo, doNotUseGetters, psiClass);
final PsiType memberType = memberInfo.getType();
if (memberType instanceof PsiPrimitiveType) {
if (PsiType.FLOAT.equals(memberType)) {
builder.append("if (java.lang.Float.compare(this.").append(memberAccessor).append(", other.").append(memberAccessor).append(") != 0) return false;\n");
} else if (PsiType.DOUBLE.equals(memberType)) {
builder.append("if (java.lang.Double.compare(this.").append(memberAccessor).append(", other.").append(memberAccessor).append(") != 0) return false;\n");
} else {
builder.append("if (this.").append(memberAccessor).append(" != other.").append(memberAccessor).append(") return false;\n");
}
} else if (memberType instanceof PsiArrayType) {
final PsiType componentType = ((PsiArrayType) memberType).getComponentType();
if (componentType instanceof PsiPrimitiveType) {
builder.append("if (!java.util.Arrays.equals(this.").append(memberAccessor).append(", other.").append(memberAccessor).append(")) return false;\n");
} else {
builder.append("if (!java.util.Arrays.deepEquals(this.").append(memberAccessor).append(", other.").append(memberAccessor).append(")) return false;\n");
}
} else {
final String memberName = memberInfo.getName();
builder.append("final java.lang.Object this$").append(memberName).append(" = this.").append(memberAccessor).append(";\n");
builder.append("final java.lang.Object other$").append(memberName).append(" = other.").append(memberAccessor).append(";\n");
builder.append("if (this$").append(memberName).append(" == null ? other$").append(memberName).append(" != null : !this$")
.append(memberName).append(".equals(other$").append(memberName).append(")) return false;\n");
}
}
builder.append("return true;\n");
return builder.toString();
}
private static final int PRIME_FOR_HASHCODE = 59;
private static final int PRIME_FOR_TRUE = 79;
private static final int PRIME_FOR_FALSE = 97;
private static final int PRIME_FOR_NULL = 43;
private String createHashcodeBlockString(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, Collection<MemberInfo> memberInfos) {
final boolean callSuper = readCallSuperAnnotationOrConfigProperty(psiAnnotation, psiClass);
final boolean doNotUseGetters = readAnnotationOrConfigProperty(psiAnnotation, psiClass, "doNotUseGetters", ConfigKey.EQUALSANDHASHCODE_DO_NOT_USE_GETTERS);
final StringBuilder builder = new StringBuilder();
if (!memberInfos.isEmpty()) {
builder.append("final int PRIME = ").append(PRIME_FOR_HASHCODE).append(";\n");
}
builder.append("int result = ");
if (callSuper) {
builder.append("super.hashCode();\n");
} else {
builder.append("1;\n");
}
for (MemberInfo memberInfo : memberInfos) {
final String memberAccessor = handler.getMemberAccessorName(memberInfo, doNotUseGetters, psiClass);
final String memberName = memberInfo.getMethod() == null ? memberInfo.getName() : "$" + memberInfo.getName();
final PsiType classFieldType = memberInfo.getType();
if (classFieldType instanceof PsiPrimitiveType) {
if (PsiType.BOOLEAN.equals(classFieldType)) {
builder.append("result = result * PRIME + (this.").append(memberAccessor).append(" ? ").append(PRIME_FOR_TRUE).append(" : ").append(PRIME_FOR_FALSE).append(");\n");
} else if (PsiType.LONG.equals(classFieldType)) {
builder.append("final long $").append(memberName).append(" = this.").append(memberAccessor).append(";\n");
builder.append("result = result * PRIME + (int)($").append(memberName).append(" >>> 32 ^ $").append(memberName).append(");\n");
} else if (PsiType.FLOAT.equals(classFieldType)) {
builder.append("result = result * PRIME + java.lang.Float.floatToIntBits(this.").append(memberAccessor).append(");\n");
} else if (PsiType.DOUBLE.equals(classFieldType)) {
builder.append("final long $").append(memberName).append(" = java.lang.Double.doubleToLongBits(this.").append(memberAccessor).append(");\n");
builder.append("result = result * PRIME + (int)($").append(memberName).append(" >>> 32 ^ $").append(memberName).append(");\n");
} else {
builder.append("result = result * PRIME + this.").append(memberAccessor).append(";\n");
}
} else if (classFieldType instanceof PsiArrayType) {
final PsiType componentType = ((PsiArrayType) classFieldType).getComponentType();
if (componentType instanceof PsiPrimitiveType) {
builder.append("result = result * PRIME + java.util.Arrays.hashCode(this.").append(memberAccessor).append(");\n");
} else {
builder.append("result = result * PRIME + java.util.Arrays.deepHashCode(this.").append(memberAccessor).append(");\n");
}
} else {
builder.append("final java.lang.Object $").append(memberName).append(" = this.").append(memberAccessor).append(";\n");
builder.append("result = result * PRIME + ($").append(memberName).append(" == null ? " + PRIME_FOR_NULL + " : $").append(memberName).append(".hashCode());\n");
}
}
builder.append("return result;\n");
return builder.toString();
}
private boolean readCallSuperAnnotationOrConfigProperty(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass) {
final boolean result;
final Boolean declaredAnnotationValue = PsiAnnotationUtil.getDeclaredBooleanAnnotationValue(psiAnnotation, "callSuper");
if (null == declaredAnnotationValue) {
final String configProperty = ConfigDiscovery.getInstance().getStringLombokConfigProperty(ConfigKey.EQUALSANDHASHCODE_CALL_SUPER, psiClass);
result = PsiClassUtil.hasSuperClass(psiClass) && "CALL".equalsIgnoreCase(configProperty);
} else {
result = declaredAnnotationValue;
}
return result;
}
@NotNull
@Override
public Collection<PsiAnnotation> collectProcessedAnnotations(@NotNull PsiClass psiClass) {
final Collection<PsiAnnotation> result = super.collectProcessedAnnotations(psiClass);
addFieldsAnnotation(result, psiClass, EQUALSANDHASHCODE_INCLUDE, EQUALSANDHASHCODE_EXCLUDE);
return result;
}
@Override
public LombokPsiElementUsage checkFieldUsage(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation) {
final PsiClass containingClass = psiField.getContainingClass();
if (null != containingClass) {
final String psiFieldName = StringUtil.notNullize(psiField.getName());
if (handler.filterFields(containingClass, psiAnnotation, true, INCLUDE_ANNOTATION_METHOD).stream()
.map(MemberInfo::getName).anyMatch(psiFieldName::equals)) {
return LombokPsiElementUsage.READ;
}
}
return LombokPsiElementUsage.NONE;
}
}