Java: fix Optimize Imports adding imports for java.lang classes unnecessarily (IDEA-176089)

GitOrigin-RevId: 8afc6f24ce66b01eb4825b704d03adcc7c164476
This commit is contained in:
Bas Leijdekkers
2023-10-07 19:14:12 +00:00
committed by intellij-monorepo-bot
parent ddca728306
commit f22bfa9e00
5 changed files with 46 additions and 12 deletions
@@ -227,13 +227,13 @@ public final class ImportHelper{
String shortName = PsiNameHelper.getShortClassName(name);
String thisPackageClass = !thisPackageName.isEmpty() ? thisPackageName + "." + shortName : shortName;
if (facade.findClass(thisPackageClass, resolveScope) != null) {
if (facade.findClass(thisPackageClass, resolveScope) != null && facade.findClass(name, resolveScope) != null) {
namesToUseSingle.add(name);
continue;
}
if (!isImplicitlyImported) {
String langPackageClass = JAVA_LANG_PACKAGE + "." + shortName; //TODO : JSP!
if (facade.findClass(langPackageClass, resolveScope) != null) {
if (facade.findClass(langPackageClass, resolveScope) != null && facade.findClass(name, resolveScope) != null) {
namesToUseSingle.add(name);
continue;
}
@@ -316,9 +316,7 @@ public final class ImportHelper{
}
else if ((aClass = facade.findClass(onDemand, resolveScope)) != null) { // import static foo.package1.Class1.*;
if (isStatic) {
PsiMember[][] memberArray = {aClass.getInnerClasses(), aClass.getMethods(), aClass.getFields()};
Set<String> set = Arrays.stream(memberArray)
.flatMap(Arrays::stream)
Set<String> set = Arrays.stream(aClass.getInnerClasses())
.filter(member -> member.hasModifierProperty(PsiModifier.STATIC))
.map(PsiMember::getName).collect(toSet());
classNames.put(onDemand, set);
@@ -0,0 +1,14 @@
import static imports.Values.*;
import static imports.Values.Double;
public class Other {
void x() {
System.out.println(Double);
System.out.println(Object);
System.out.println(String);
String s = "";
Object o = new Object();
Double d = 5.;
}
}
@@ -0,0 +1,13 @@
import static imports.Values.*;
public class Other {
void x() {
System.out.println(Double);
System.out.println(Object);
System.out.println(String);
String s = "";
Object o = new Object();
Double d = 5.;
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2018 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.java.codeInsight.daemon;
import com.intellij.ide.highlighter.JavaFileType;
@@ -90,11 +90,11 @@ public class LightOptimizeImportsTest extends LightJavaCodeInsightFixtureTestCas
public void testStaticImportsOrder() throws Exception {
myFixture.addClass("package p; public class C1 {" +
" public static String Byte;\n" +
" public static class Byte {}\n" +
" public static String Field2;" +
"}");
myFixture.addClass("package p; public class C2 { " +
" public static String Long;\n" +
" public static class Long {}\n" +
" public static String Field4;" +
"}");
@@ -107,9 +107,9 @@ public class LightOptimizeImportsTest extends LightJavaCodeInsightFixtureTestCas
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" System.out.println(Byte);\n" +
" System.out.println(Byte.class);\n" +
" System.out.println(Field2);\n" +
" System.out.println(Long);\n" +
" System.out.println(Long.class);\n" +
" System.out.println(Field4);\n" +
" }\n" +
"}";
@@ -128,9 +128,9 @@ public class LightOptimizeImportsTest extends LightJavaCodeInsightFixtureTestCas
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" System.out.println(Byte);\n" +
" System.out.println(Byte.class);\n" +
" System.out.println(Field2);\n" +
" System.out.println(Long);\n" +
" System.out.println(Long.class);\n" +
" System.out.println(Field4);\n" +
" }\n" +
"}";
@@ -94,6 +94,15 @@ public class OptimizeImportsTest extends OptimizeImportsTestCase {
public void testNewImportListIsEmptyAndJavaDocWithInvalidCodePreserved() { doTest(); }
public void testDontCollapseToOnDemandImport() { doTest(); }
public void testDontInsertRedundantJavaLangImports() {
myFixture.addClass("""
package imports;
public enum Values {
String, Object, Double
}""");
doTest();
}
public void testIgnoreInaccessible() { doTest();}
public void testEnsureConflictingImportsNotCollapsed() {