mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
Java: use correct variable prefixes in Generate Equals (IDEA-265430)
GitOrigin-RevId: 04f96ac8c0bafb53370d8075bb39730e5f870136
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9c6f41248b
commit
9b2fe7815f
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.generation;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
@@ -163,14 +163,17 @@ public class GenerateEqualsHelper implements Runnable {
|
||||
|
||||
final PsiType classType = JavaPsiFacade.getElementFactory(myClass.getProject()).createType(myClass);
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myClass.getProject());
|
||||
String[] nameSuggestions = codeStyleManager
|
||||
.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, classType).names;
|
||||
String instanceBaseName = nameSuggestions.length > 0 && nameSuggestions[0].length() < 10 ? nameSuggestions[0] : "that";
|
||||
String[] nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, classType).names;
|
||||
String instanceBaseName = nameSuggestions.length > 0 && nameSuggestions[0].length() < 10
|
||||
? nameSuggestions[0]
|
||||
: styleSettings.LOCAL_VARIABLE_NAME_PREFIX + "that" + styleSettings.LOCAL_VARIABLE_NAME_SUFFIX;
|
||||
contextMap.put(INSTANCE_NAME, instanceBaseName);
|
||||
|
||||
final PsiType objectType = PsiType.getJavaLangObject(myClass.getManager(), myClass.getResolveScope());
|
||||
nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, objectType).names;
|
||||
final String objectBaseName = nameSuggestions.length > 0 ? nameSuggestions[0] : "object";
|
||||
final String objectBaseName = nameSuggestions.length > 0
|
||||
? nameSuggestions[0]
|
||||
: styleSettings.PARAMETER_NAME_PREFIX + "object" + styleSettings.PARAMETER_NAME_SUFFIX;
|
||||
contextMap.put(BASE_PARAM_NAME, objectBaseName);
|
||||
final MethodSignature equalsSignature = getEqualsSignature(myProject, myClass.getResolveScope());
|
||||
|
||||
@@ -212,7 +215,7 @@ public class GenerateEqualsHelper implements Runnable {
|
||||
PsiClass containingClass = superEquals.getContainingClass();
|
||||
//implicit equals e.g. generated by lombok
|
||||
if (containingClass == myClass) {
|
||||
return Arrays.stream(superEquals.findSuperMethods()).anyMatch(superMethod -> {
|
||||
return ContainerUtil.exists(superEquals.findSuperMethods(), superMethod -> {
|
||||
PsiClass superClass = superMethod.getContainingClass();
|
||||
return !superMethod.hasModifierProperty(PsiModifier.ABSTRACT) &&
|
||||
superClass != null &&
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
class Simple {
|
||||
private int simple = 1;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object p_o_r) {
|
||||
if (this == p_o_r) return true;
|
||||
if (p_o_r == null || getClass() != p_o_r.getClass()) return false;
|
||||
|
||||
final Simple l_that_v = (Simple) p_o_r;
|
||||
|
||||
if (simple != l_that_v.simple) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return simple;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Simple {
|
||||
private int simple = 1;
|
||||
|
||||
}
|
||||
@@ -12,12 +12,11 @@ import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.Functions;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
|
||||
@@ -95,11 +94,11 @@ public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
}
|
||||
|
||||
public void testDifferentTypes() {
|
||||
doTest(Functions.id(), Functions.id(), fields -> PsiField.EMPTY_ARRAY, true);
|
||||
doTest(Function.identity(), Function.identity(), fields -> PsiField.EMPTY_ARRAY, true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesGetters() {
|
||||
doTest(Functions.id(), Functions.id(), fields -> PsiField.EMPTY_ARRAY, true, true);
|
||||
doTest(Function.identity(), Function.identity(), fields -> PsiField.EMPTY_ARRAY, true, true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesAllNotNull() {
|
||||
@@ -111,15 +110,24 @@ public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
}
|
||||
|
||||
public void testDifferentTypesNoDouble() {
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
doTest(Function.identity(), Function.identity(), Function.identity(), true);
|
||||
}
|
||||
|
||||
public void testNameConflicts() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testPrefixes() {
|
||||
JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject());
|
||||
settings.LOCAL_VARIABLE_NAME_PREFIX = "l_";
|
||||
settings.LOCAL_VARIABLE_NAME_SUFFIX = "_v";
|
||||
settings.PARAMETER_NAME_PREFIX = "p_";
|
||||
settings.PARAMETER_NAME_SUFFIX = "_r";
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testClassWithTypeParams() {
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
doTest(Function.identity(), Function.identity(), Function.identity(), true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCodeApache3() {
|
||||
@@ -153,7 +161,7 @@ public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
private void doTestWithTemplate(String templateName) {
|
||||
try {
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(templateName);
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
doTest(Function.identity(), Function.identity(), Function.identity(), true);
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
try (InputStream is = GenerateMembersUtil.class.getResourceAsStream("equalsHelper.vm")) {
|
||||
@@ -210,7 +218,7 @@ public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
|
||||
if (aClass == null) return;
|
||||
PsiField[] fields = aClass.getFields();
|
||||
new GenerateEqualsHelper(getProject(), aClass, equals.fun(fields), hashCode.fun(fields), nonNull.fun(fields), false, useAccessors).invoke();
|
||||
new GenerateEqualsHelper(getProject(), aClass, equals.apply(fields), hashCode.apply(fields), nonNull.apply(fields), false, useAccessors).invoke();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package org.jetbrains.java.generate.element;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -16,7 +16,6 @@ public final class GenerationHelper {
|
||||
//used in generate equals/hashCode
|
||||
@SuppressWarnings("unused")
|
||||
public static String getUniqueLocalVarName(String base, List<? extends Element> elements, JavaCodeStyleSettings settings) {
|
||||
base = settings.LOCAL_VARIABLE_NAME_PREFIX + base;
|
||||
String id = base;
|
||||
int index = 0;
|
||||
while (true) {
|
||||
@@ -34,7 +33,6 @@ public final class GenerationHelper {
|
||||
if (!anyEqual) break;
|
||||
}
|
||||
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user