mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
ensure method, imported statically, is not hidden by a local one (IDEA-175967)
This commit is contained in:
@@ -245,20 +245,16 @@ public class AddSingleMemberStaticImportAction extends BaseElementAtCaretIntenti
|
||||
PsiElement referent = reference.getUserData(TEMP_REFERENT_USER_DATA);
|
||||
if (!reference.isQualified()) {
|
||||
if (referent instanceof PsiMember && referent != reference.resolve()) {
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(reference.getProject()).getElementFactory();
|
||||
try {
|
||||
final PsiClass containingClass = ((PsiMember)referent).getContainingClass();
|
||||
if (containingClass != null) {
|
||||
PsiReferenceExpression copy = (PsiReferenceExpression)factory.createExpressionFromText("A." + reference.getReferenceName(), null);
|
||||
reference = (PsiReferenceExpression)reference.replace(copy);
|
||||
((PsiReferenceExpression)reference.getQualifier()).bindToElement(containingClass);
|
||||
reference = rebind(reference, containingClass);
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error (e);
|
||||
}
|
||||
}
|
||||
reference.putUserData(TEMP_REFERENT_USER_DATA, null);
|
||||
}
|
||||
else if (referent == null || referent instanceof PsiMember && ((PsiMember)referent).hasModifierProperty(PsiModifier.STATIC)) {
|
||||
if (qualifierExpression instanceof PsiJavaCodeReferenceElement) {
|
||||
@@ -273,6 +269,9 @@ public class AddSingleMemberStaticImportAction extends BaseElementAtCaretIntenti
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
if (reference.resolve() != referent) {
|
||||
reference = rebind(reference, resolvedClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -286,6 +285,14 @@ public class AddSingleMemberStaticImportAction extends BaseElementAtCaretIntenti
|
||||
});
|
||||
}
|
||||
|
||||
private static PsiJavaCodeReferenceElement rebind(PsiJavaCodeReferenceElement reference, PsiClass targetClass) {
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(reference.getProject()).getElementFactory();
|
||||
PsiReferenceExpression copy = (PsiReferenceExpression)factory.createExpressionFromText("A." + reference.getReferenceName(), null);
|
||||
reference = (PsiReferenceExpression)reference.replace(copy);
|
||||
((PsiReferenceExpression)reference.getQualifier()).bindToElement(targetClass);
|
||||
return reference;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
|
||||
invoke(element.getContainingFile(), element);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package foo;
|
||||
|
||||
class Test {
|
||||
|
||||
public void foo() {
|
||||
X.te<caret>st("bla");
|
||||
}
|
||||
|
||||
static class Y {
|
||||
public void foo() {
|
||||
X.test("bla");
|
||||
}
|
||||
|
||||
public void test(int x) {}
|
||||
}
|
||||
|
||||
public static class X {
|
||||
public static void test(String x) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo;
|
||||
|
||||
import static foo.Test.X.test;
|
||||
|
||||
class Test {
|
||||
|
||||
public void foo() {
|
||||
test("bla");
|
||||
}
|
||||
|
||||
static class Y {
|
||||
public void foo() {
|
||||
X.test("bla");
|
||||
}
|
||||
|
||||
public void test(int x) {}
|
||||
}
|
||||
|
||||
public static class X {
|
||||
public static void test(String x) {}
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,16 @@ public class AddSingleStaticImportActionTest extends JavaCodeInsightFixtureTestC
|
||||
myFixture.checkResultByFile(getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testWrongCandidateAfterImport() {
|
||||
myFixture.addClass("package foo; class Empty {}"); //to ensure package is in the project
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
|
||||
final IntentionAction intentionAction = myFixture.findSingleIntention("Add static import for 'foo.Test.X.test'");
|
||||
assertNotNull(intentionAction);
|
||||
myFixture.launchAction(intentionAction);
|
||||
myFixture.checkResultByFile(getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testAllowStaticImportWhenAlreadyImported() {
|
||||
myFixture.addClass("package foo; " +
|
||||
"public class Clazz {\n" +
|
||||
|
||||
Reference in New Issue
Block a user