IDEA-247279 - fixed the choice of suggested methods when records implement some interface

GitOrigin-RevId: 1a452fdf44fe0ca20935b9129565117ab0d6a85c
This commit is contained in:
Ilyas Selimov
2020-08-04 18:48:59 +07:00
committed by intellij-monorepo-bot
parent ab344fd654
commit 23003595bd
5 changed files with 58 additions and 2 deletions

View File

@@ -12,11 +12,14 @@ import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -90,7 +93,15 @@ public final class JavaOverrideImplementMemberChooser extends MemberChooser<PsiM
if (onlyPrimary.length == 0) {
javaOverrideImplementMemberChooser.selectElements(new ClassMember[] {all[0]});
} else {
javaOverrideImplementMemberChooser.selectElements(onlyPrimary);
PsiClass currClass = ObjectUtils.tryCast(aClass, PsiClass.class);
if (currClass != null && currClass.isRecord()) {
PsiMethodMember[] toImplementMembers = ContainerUtil
.filter(onlyPrimary, m -> !OverrideImplementExploreUtil.belongsToRecord(m.getElement()))
.toArray(new PsiMethodMember[0]);
javaOverrideImplementMemberChooser.selectElements(ArrayUtil.isEmpty(toImplementMembers) ? onlyPrimary : toImplementMembers);
} else {
javaOverrideImplementMemberChooser.selectElements(onlyPrimary);
}
}
}

View File

@@ -169,7 +169,11 @@ public class OverrideImplementExploreUtil {
return isDefaultMethod(aClass, abstractOne) ||
// abstract methods from java.lang.Record (equals/hashCode/toString) are implicitly implemented in subclasses
// so it could be reasonable to expect them in 'override' method dialog
CommonClassNames.JAVA_LANG_RECORD.equals(Objects.requireNonNull(abstractOne.getContainingClass()).getQualifiedName());
belongsToRecord(abstractOne);
}
static boolean belongsToRecord(@NotNull PsiMethod method) {
return CommonClassNames.JAVA_LANG_RECORD.equals(Objects.requireNonNull(method.getContainingClass()).getQualifiedName());
}
private static boolean preferLeftForImplement(@NotNull PsiMethod left, @NotNull PsiMethod right) {

View File

@@ -0,0 +1,26 @@
record R() implements Nameable, Sizable {
@Override
public String name() {
return null;
}
@Override
public String lastName() {
return null;
}
@Override
public int size() {
return 0;
}
}
interface Nameable {
String name();
String lastName();
}
interface Sizable {
int size();
}

View File

@@ -0,0 +1,13 @@
record R() implements Nameable, Sizable {
<caret>
}
interface Nameable {
String name();
String lastName();
}
interface Sizable {
int size();
}

View File

@@ -39,6 +39,8 @@ class OverrideImplementTest extends LightJavaCodeInsightFixtureTestCase {
void testImplementRecordMethods() { addRecordClass();doTest(true) }
void testImplementInterfaceMethodsInRecord() { addRecordClass();doTest(true) }
void testOverrideRecordMethods() { addRecordClass();doTest(false) }
void testImplementExtensionMethods() { doTest(true) }