optimize imports: check accessibility before expanding on-demand to single-static-import (IDEA-157833)

This commit is contained in:
Anna.Kozlova
2016-11-24 20:25:45 +01:00
parent 261e309dd7
commit c45ee97f93
4 changed files with 50 additions and 3 deletions

View File

@@ -247,6 +247,8 @@ public class ImportHelper{
continue;
}
}
PsiResolveHelper resolveHelper = facade.getResolveHelper();
for (int i = 0; i < onDemandImportsList.size(); i++) {
String onDemandName = onDemandImportsList.get(i);
if (prefix.equals(onDemandName)) continue;
@@ -254,18 +256,18 @@ public class ImportHelper{
PsiClass aClass = onDemandElements.get(i);
if (aClass != null) {
PsiField field = aClass.findFieldByName(shortName, true);
if (field != null && field.hasModifierProperty(PsiModifier.STATIC)) {
if (field != null && field.hasModifierProperty(PsiModifier.STATIC) && resolveHelper.isAccessible(field, file, null)) {
namesToUseSingle.add(name);
}
else {
PsiClass inner = aClass.findInnerClassByName(shortName, true);
if (inner != null && inner.hasModifierProperty(PsiModifier.STATIC)) {
if (inner != null && inner.hasModifierProperty(PsiModifier.STATIC) && resolveHelper.isAccessible(inner, file, null)) {
namesToUseSingle.add(name);
}
else {
PsiMethod[] methods = aClass.findMethodsByName(shortName, true);
for (PsiMethod method : methods) {
if (method.hasModifierProperty(PsiModifier.STATIC)) {
if (method.hasModifierProperty(PsiModifier.STATIC) && resolveHelper.isAccessible(method, file, null)) {
namesToUseSingle.add(name);
}
}

View File

@@ -0,0 +1,22 @@
package test;
import static test.a.A.*;
import static test.b.B.*;
class Test {
public static void main(String[] args) {
System.out.println(A_CONST);
System.out.println(B_CONST);
System.out.println(SHARED_CONST);
}
}
class A {
public static final int A_CONST = 1;
public static final int SHARED_CONST = 2;
}
class B {
public static final int B_CONST = 1;
private static final int SHARED_CONST = 2;
}

View File

@@ -0,0 +1,22 @@
package test;
import static test.a.A.*;
import static test.b.B.*;
class Test {
public static void main(String[] args) {
System.out.println(A_CONST);
System.out.println(B_CONST);
System.out.println(SHARED_CONST);
}
}
class A {
public static final int A_CONST = 1;
public static final int SHARED_CONST = 2;
}
class B {
public static final int B_CONST = 1;
private static final int SHARED_CONST = 2;
}

View File

@@ -39,6 +39,7 @@ public class OptimizeImportsTest extends OptimizeImportsTestCase {
public void testNewImportListIsEmptyAndJavaDocWithInvalidCodePreserved() throws Exception { doTest(); }
public void testDontCollapseToOnDemandImport() throws Exception { doTest(); }
public void testIgnoreInaccessible() throws Exception{ doTest();}
private void doTest() throws Exception {
doTest(".java");