rename: ensure class access is qualified if conflicted with local (IDEA-155040)

This commit is contained in:
Anna.Kozlova
2016-05-12 16:17:23 +02:00
parent cd068b6545
commit 78b56192d2
4 changed files with 48 additions and 3 deletions
@@ -45,7 +45,7 @@ class JavaResolveSnapshot extends ResolveSnapshotProvider.ResolveSnapshot {
if (!refExpr.isQualified()) {
JavaResolveResult resolveResult = refExpr.advancedResolve(false);
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiField && resolveResult.isStaticsScopeCorrect()) {
if ((resolved instanceof PsiField || resolved instanceof PsiClass) && resolveResult.isStaticsScopeCorrect()) {
SmartPsiElementPointer key = pointerManager.createSmartPsiElementPointer(refExpr);
SmartPsiElementPointer value = pointers.get(resolved);
if (value == null) {
@@ -68,12 +68,12 @@ class JavaResolveSnapshot extends ResolveSnapshotProvider.ResolveSnapshot {
}
private static void qualify(PsiElement referent, PsiElement referee, String hidingLocalName) {
if (referent instanceof PsiReferenceExpression && referee instanceof PsiField) {
if (referent instanceof PsiReferenceExpression && referee instanceof PsiMember) {
PsiReferenceExpression ref = ((PsiReferenceExpression) referent);
if (!ref.isQualified() && hidingLocalName.equals(ref.getReferenceName())) {
final PsiElement newlyResolved = ref.resolve();
if (referee.getManager().areElementsEquivalent(newlyResolved, referee)) return;
RenameJavaMemberProcessor.qualifyMember((PsiField)referee, referent, hidingLocalName);
RenameJavaMemberProcessor.qualifyMember((PsiMember)referee, referent, hidingLocalName);
}
}
}
@@ -0,0 +1,12 @@
class Enclosing {
static class constants {
public static final String CONSTANT = null;
}
}
class Test {
void f() {
System.out.println(Enclosing.constants.CON<caret>STANT);
}
}
@@ -0,0 +1,14 @@
import Enclosing.constants;
class Enclosing {
static class constants {
public static final String CONSTANT = null;
}
}
class Test {
void f() {
String constants = Enclosing.constants.CONSTANT;
System.out.println(constants);
}
}
@@ -26,6 +26,8 @@ import com.intellij.openapi.util.Pass;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
@@ -60,6 +62,23 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe
});
}
public void testConflictingInnerClassName() throws Exception {
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
final boolean oldOption = settings.INSERT_INNER_CLASS_IMPORTS;
try {
settings.INSERT_INNER_CLASS_IMPORTS = true;
doTest(new Pass<AbstractInplaceIntroducer>() {
@Override
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
type("constants");
}
});
}
finally {
settings.INSERT_INNER_CLASS_IMPORTS = oldOption;
}
}
public void testInsideInjectedString() throws Exception {
doTestInsideInjection(new Pass<AbstractInplaceIntroducer>() {
@Override