mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
qualify statically imported methods if they conflict with local members (IDEA-156273)
This commit is contained in:
@@ -24,7 +24,6 @@ import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -150,11 +149,23 @@ public abstract class RenameJavaMemberProcessor extends RenamePsiElementProcesso
|
||||
}
|
||||
}
|
||||
|
||||
protected static void findCollisionsAgainstNewName(final PsiMember memberToRename, final String newName, final List<? super MemberHidesStaticImportUsageInfo> result) {
|
||||
protected static void findCollisionsAgainstNewName(final PsiMember memberToRename, final String newName, final List<UsageInfo> result) {
|
||||
if (!memberToRename.isPhysical()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(memberToRename.getProject());
|
||||
final List<PsiReference> potentialConflicts = new ArrayList<PsiReference>();
|
||||
for (UsageInfo info : result) {
|
||||
final PsiElement element = info.getElement();
|
||||
if (element instanceof PsiReferenceExpression) {
|
||||
if (((PsiReferenceExpression)element).advancedResolve(false).getCurrentFileResolveScope() instanceof PsiImportStaticStatement &&
|
||||
referencesLocalMember(memberToRename, newName, elementFactory, element)) {
|
||||
potentialConflicts.add(info.getReference());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final PsiFile containingFile = memberToRename.getContainingFile();
|
||||
if (containingFile instanceof PsiJavaFile) {
|
||||
final PsiImportList importList = ((PsiJavaFile)containingFile).getImportList();
|
||||
@@ -206,6 +217,21 @@ public abstract class RenameJavaMemberProcessor extends RenamePsiElementProcesso
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean referencesLocalMember(PsiMember memberToRename,
|
||||
String newName,
|
||||
PsiElementFactory elementFactory,
|
||||
PsiElement context) {
|
||||
if (memberToRename instanceof PsiField) {
|
||||
return ((PsiReferenceExpression)elementFactory.createExpressionFromText(newName, context)).resolve() != null;
|
||||
}
|
||||
|
||||
if (memberToRename instanceof PsiMethod) {
|
||||
final PsiMethodCallExpression callExpression = (PsiMethodCallExpression)elementFactory.createExpressionFromText(newName + "()", context);
|
||||
return callExpression.getMethodExpression().multiResolve(false).length > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static void qualifyStaticImportReferences(final List<MemberHidesStaticImportUsageInfo> staticImportHides)
|
||||
throws IncorrectOperationException {
|
||||
for (MemberHidesStaticImportUsageInfo info : staticImportHides) {
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package bar;
|
||||
public class Bar {
|
||||
public static Object bar() {
|
||||
return new Object();
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package foo;
|
||||
import bar.Bar;
|
||||
|
||||
public class Foo {
|
||||
protected static Object bar() {
|
||||
return Bar.bar();
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package foo;
|
||||
import bar.Bar;
|
||||
|
||||
public class Foo {
|
||||
protected static Object bar(int i) {
|
||||
return Bar.bar();
|
||||
}
|
||||
|
||||
static void bar(int i, int j) {}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package bar;
|
||||
public class Bar {
|
||||
public static Object createBar() {
|
||||
return new Object();
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package foo;
|
||||
import static bar.Bar.createBar;
|
||||
public class Foo {
|
||||
protected static Object bar() {
|
||||
return createBar();
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package foo;
|
||||
import static bar.Bar.createBar;
|
||||
public class Foo {
|
||||
protected static Object bar(int i) {
|
||||
return createBar();
|
||||
}
|
||||
|
||||
static void bar(int i, int j) {}
|
||||
}
|
||||
@@ -97,6 +97,10 @@ public class RenameMethodMultiTest extends MultiFileTestCase {
|
||||
doTest("p.Foo", "void foo()", "bar");
|
||||
}
|
||||
|
||||
public void testExpandStaticImportToAvoidConflictingResolve() throws Exception {
|
||||
doTest("bar.Bar", "void createBar()", "bar");
|
||||
}
|
||||
|
||||
private void doTest(final String methodSignature, final String newName) throws Exception {
|
||||
doTest(getTestName(false), methodSignature, newName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user