From ba90fcc173ea4f47c2a1a02b6d0e00fbce2ef134 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 19 Sep 2024 16:45:07 +0200 Subject: [PATCH] Java: make auto-import action available on method references to non-static methods (IDEA-336191) GitOrigin-RevId: 2441af053858efe56887a5ea8dc34cfd08878261 --- .../impl/quickfix/ImportClassFixBase.java | 12 ++++++----- .../intention/AddImportActionTest.java | 21 ++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java index 320d3c6270c3..c3056f248640 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java @@ -194,8 +194,8 @@ public abstract class ImportClassFixBase filtered = filterByContext(classList, myReferenceElement); if (!filtered.isEmpty()) { @@ -248,18 +248,20 @@ public abstract class ImportClassFixBase classList) { + private void filterByRequiredMemberName(@NotNull List 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; }); diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/AddImportActionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/AddImportActionTest.java index d4af7972b593..b11a37bffddc 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/AddImportActionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/AddImportActionTest.java @@ -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 list) { + list.stream().map(Random.class::cast).map(Random::toString).forEach(System.out::println); + } + }"""); + importClass(); + myFixture.checkResult(""" + import java.util.Random; + + public class Importing { + void x(List 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 {}");