mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-03-22 15:19:59 +07:00
Java: generated equals(), hashCode() and toString() can now use getters in records as well (IDEA-359692)
GitOrigin-RevId: d94b6278d61e5a0497a402c8201e31d9165691ab
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d74484e368
commit
6c2a3df811
@@ -179,9 +179,7 @@ public class GenerateEqualsHelper implements Runnable {
|
||||
final MethodSignature equalsSignature = getEqualsSignature(myProject, myClass.getResolveScope());
|
||||
|
||||
PsiMethod superEquals = MethodSignatureUtil.findMethodBySignature(myClass, equalsSignature, true);
|
||||
if (superEquals != null) {
|
||||
contextMap.put(SUPER_PARAM_NAME, superEquals.getParameterList().getParameters()[0].getName());
|
||||
}
|
||||
contextMap.put(SUPER_PARAM_NAME, superEquals == null ? "obj" : superEquals.getParameterList().getParameters()[0].getName());
|
||||
|
||||
contextMap.put(SUPER_HAS_EQUALS, superMethodExists(equalsSignature));
|
||||
contextMap.put(CHECK_PARAMETER_WITH_INSTANCEOF, myCheckParameterWithInstanceof);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
record Point(int x, int y, int z) {
|
||||
|
||||
public final boolean equals(Object o) {
|
||||
if (!(o instanceof Point)) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
|
||||
final Point point = (Point) o;
|
||||
return x() == point.x() && y() == point.y() && z() == point.z();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + x();
|
||||
result = 31 * result + y();
|
||||
result = 31 * result + z();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
record Point(int x, int y, int z) {
|
||||
<caret>
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.codeInsight;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
@@ -108,6 +108,10 @@ public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
public void testDifferentTypesGetters() {
|
||||
doTest(Function.identity(), Function.identity(), fields -> PsiField.EMPTY_ARRAY, true, true, false);
|
||||
}
|
||||
|
||||
public void testRecordUseGettersJava21() {
|
||||
doTest(Function.identity(), Function.identity(), fields -> PsiField.EMPTY_ARRAY, true, true, true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesAllNotNull() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
|
||||
@@ -89,12 +89,21 @@ public final class ElementFactory {
|
||||
public static FieldElement newFieldElement(PsiField field, boolean useAccessor) {
|
||||
FieldElement fe = new FieldElement();
|
||||
fe.setName(field.getName());
|
||||
final PsiMethod getterForField = useAccessor ? PropertyUtilBase.findGetterForField(field) : null;
|
||||
PsiRecordComponent component = JavaPsiRecordUtil.getComponentForField(field);
|
||||
final PsiMethod getterForField;
|
||||
if (useAccessor) {
|
||||
getterForField = component != null
|
||||
? JavaPsiRecordUtil.getAccessorForRecordComponent(component)
|
||||
: PropertyUtilBase.findGetterForField(field);
|
||||
}
|
||||
else {
|
||||
getterForField = null;
|
||||
}
|
||||
fe.setAccessor(getterForField != null ? getterForField.getName() + "()" : field.getName());
|
||||
|
||||
if (PsiAdapter.isConstantField(field)) fe.setConstant(true);
|
||||
if (PsiAdapter.isEnumField(field)) fe.setEnum(true);
|
||||
if (JavaPsiRecordUtil.getComponentForField(field) != null) fe.setRecordComponent(true);
|
||||
if (component != null) fe.setRecordComponent(true);
|
||||
PsiModifierList modifiers = field.getModifierList();
|
||||
if (modifiers != null) {
|
||||
if (modifiers.hasModifierProperty(PsiModifier.TRANSIENT)) fe.setModifierTransient(true);
|
||||
|
||||
Reference in New Issue
Block a user