Java: make auto-import action available on method references to non-static methods (IDEA-336191)

GitOrigin-RevId: 2441af053858efe56887a5ea8dc34cfd08878261
This commit is contained in:
Bas Leijdekkers
2024-09-19 16:45:07 +02:00
committed by intellij-monorepo-bot
parent 7083706121
commit ba90fcc173
2 changed files with 27 additions and 6 deletions

View File

@@ -194,8 +194,8 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
facade.arePackagesTheSame(aClass, myReferenceElement) ||
PsiTreeUtil.getParentOfType(aClass, PsiImplicitClass.class) != null) &&
!isAccessible(aClass, myReferenceElement));
filterByRequiredMemberName(classList);
boolean needsStatic = !(parent instanceof PsiMethodReferenceExpression);
filterByRequiredMemberName(classList, needsStatic);
Collection<PsiClass> filtered = filterByContext(classList, myReferenceElement);
if (!filtered.isEmpty()) {
@@ -248,18 +248,20 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
return true;
}
private void filterByRequiredMemberName(@NotNull List<PsiClass> classList) {
private void filterByRequiredMemberName(@NotNull List<PsiClass> classList, boolean needsStatic) {
String memberName = getRequiredMemberName(myReferenceElement);
if (memberName != null) {
classList.removeIf(psiClass -> {
PsiField field = psiClass.findFieldByName(memberName, true);
if (field != null && field.hasModifierProperty(PsiModifier.STATIC) && isAccessible(field, myReferenceElement)) return false;
if (field != null && (!needsStatic || field.hasModifierProperty(PsiModifier.STATIC)) && isAccessible(field, myReferenceElement)) {
return false;
}
PsiClass inner = psiClass.findInnerClassByName(memberName, true);
if (inner != null && isAccessible(inner, myReferenceElement)) return false;
for (PsiMethod method : psiClass.findMethodsByName(memberName, true)) {
if (method.hasModifierProperty(PsiModifier.STATIC) && isAccessible(method, myReferenceElement)) return false;
if ((!needsStatic || method.hasModifierProperty(PsiModifier.STATIC)) && isAccessible(method, myReferenceElement)) return false;
}
return true;
});

View File

@@ -1,10 +1,11 @@
// 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.intention;
import com.intellij.application.options.CodeStyle;
import com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.IntentionActionDelegate;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl;
import com.intellij.pom.java.JavaFeature;
@@ -75,6 +76,24 @@ public class AddImportActionTest extends LightJavaCodeInsightFixtureTestCase {
""");
}
public void testMethodReference() {
myFixture.configureByText(JavaFileType.INSTANCE, """
public class Importing {
void x(List<Object> list) {
list.stream().map(Random.class::cast).map(Ra<caret>ndom::toString).forEach(System.out::println);
}
}""");
importClass();
myFixture.checkResult("""
import java.util.Random;
public class Importing {
void x(List<Object> list) {
list.stream().map(Random.class::cast).map(Random::toString).forEach(System.out::println);
}
}""");
}
public void testStringValue() {
myFixture.addClass("package java.lang; class StringValue {}");
myFixture.addClass("package foo; public class StringValue {}");