ensure redundant on demund imports are not created when shorten class refs would find a conflict with class in same package/implicitly imported package (IDEA-169190)

This commit is contained in:
Anna.Kozlova
2017-03-16 15:03:15 +01:00
parent 59bf111265
commit 9a3f46076c
3 changed files with 26 additions and 6 deletions
@@ -445,8 +445,8 @@ public class ImportHelper{
!mySettings.PACKAGES_TO_USE_IMPORT_ON_DEMAND.contains(packageName)) {
useOnDemand = false;
}
// name of class we try to import is the same as of the class defined in this file
if (containsInCurrentFile(file, curRefClass)) {
// name of class we try to import is the same as of the class defined in this package
if (containsInCurrentPackage(file, curRefClass)) {
useOnDemand = true;
}
// check conflicts
@@ -464,10 +464,19 @@ public class ImportHelper{
if (useOnDemand &&
refClass.getContainingClass() != null &&
mySettings.INSERT_INNER_CLASS_IMPORTS &&
containsInCurrentFile(file, curRefClass)) {
containsInCurrentPackage(file, curRefClass)) {
return false;
}
if (curRefClass != null) {
if (!classesToReimport.isEmpty()) {
return false;
}
else {
useOnDemand = false;
}
}
try {
PsiImportList importList = file.getImportList();
assert importList != null : file;
@@ -499,7 +508,7 @@ public class ImportHelper{
return true;
}
private static boolean containsInCurrentFile(@NotNull PsiJavaFile file, PsiClass curRefClass) {
private static boolean containsInCurrentPackage(@NotNull PsiJavaFile file, PsiClass curRefClass) {
if (curRefClass != null) {
final String curRefClassQualifiedName = curRefClass.getQualifiedName();
if (curRefClassQualifiedName != null &&
@@ -1,6 +1,5 @@
package pack2;
import java.util.List;
import java.util.*;
public class Usage2 {
int method() {
@@ -81,6 +81,18 @@ public class ShortenClassReferencesTest extends LightCodeInsightFixtureTestCase
"class Outer1 {class Inner {} {boolean b = new Inner() instanceof Inner;}}");
}
public void testConflictingClassInSamePackage() throws Exception {
myFixture.addClass("package p1; public class Outer{}");
myFixture.addClass("package p2; public class Outer{}");
myFixture.configureByText("a.java", "package p2; class Outer1 extends p1.Outer {}");
doShortenRefs();
myFixture.checkResult("package p2;\n" +
"\n" +
"import p1.Outer;\n" +
"\n" +
"class Outer1 extends Outer {}");
}
public void testWhiteSpaceForMovedTypeAnnotations() throws Exception {
myFixture.configureByFile(getTestName(false) + ".java");
PsiElement elementAtCaret = myFixture.getElementAtCaret();