move instance method should qualify field access if there is a parameter with the same name (IDEA-43009)

This commit is contained in:
anna
2010-01-22 13:14:34 +03:00
parent bce7f8b69b
commit 04130b5cb1
6 changed files with 65 additions and 1 deletions
@@ -388,12 +388,20 @@ public class MoveInstanceMethodProcessor extends BaseRefactoringProcessor{
@Override public void visitReferenceExpression(PsiReferenceExpression expression) {
try {
final PsiExpression qualifier = expression.getQualifierExpression();
final PsiElement resolved = expression.resolve();
if (qualifier instanceof PsiReferenceExpression && ((PsiReferenceExpression)qualifier).isReferenceTo(myTargetVariable)) {
if (resolved instanceof PsiField) {
for (PsiParameter parameter : myMethod.getParameterList().getParameters()) {
if (Comparing.strEqual(parameter.getName(), ((PsiField)resolved).getName())) {
qualifier.replace(factory.createExpressionFromText("this", null));
return;
}
}
}
//Target is a field, replace target.m -> m
qualifier.delete();
return;
}
final PsiElement resolved = expression.resolve();
if (myTargetVariable.equals(resolved)) {
PsiThisExpression thisExpression = (PsiThisExpression)factory.createExpressionFromText("this", null);
expression.replace(thisExpression);
@@ -0,0 +1,12 @@
public class Test {
private Bar bar = new Bar();
public void <caret>foo(int x) {
bar.x = x;
}
private static class Bar {
private int x;
}
}
@@ -0,0 +1,12 @@
public class Test {
private Bar bar = new Bar();
private static class Bar {
private int x;
public void foo(int x) {
this.x = x;
}
}
}
@@ -0,0 +1,12 @@
public class Test {
private Bar bar = new Bar();
public void <caret>foo(int y) {
bar.x = y;
}
private static class Bar {
private int x;
}
}
@@ -0,0 +1,12 @@
public class Test {
private Bar bar = new Bar();
private static class Bar {
private int x;
public void foo(int y) {
x = y;
}
}
}
@@ -57,6 +57,14 @@ public class MoveInstanceMethodTest extends LightCodeInsightTestCase {
doTest(true, 0);
}
public void testQualifyFieldAccess() throws Exception {
doTest(false, 0);
}
public void testStripFieldQualifier() throws Exception {
doTest(false, 0);
}
private void doTest(boolean isTargetParameter, final int targetIndex) throws Exception {
doTest(isTargetParameter, targetIndex, null);
}