introduce parameter: do not insert additional arg when selection was in the self call (IDEA-66896)

This commit is contained in:
anna
2012-04-16 09:21:10 +02:00
parent 488e3c72b6
commit 584cf8b16b
8 changed files with 88 additions and 3 deletions
@@ -473,16 +473,41 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem
if (element.getParent() instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element.getParent();
if (myMethodToReplaceIn == myMethodToSearchFor && PsiTreeUtil.isAncestor(methodCall, myParameterInitializer, false)) return;
PsiElementFactory factory = JavaPsiFacade.getInstance(methodCall.getProject()).getElementFactory();
PsiExpression expression = factory.createExpressionFromText(myParameterName, null);
final PsiExpressionList argList = methodCall.getArgumentList();
final PsiExpression[] exprs = argList.getExpressions();
if (exprs.length > 0) {
argList.addAfter(expression, exprs[exprs.length - 1]);
boolean first = false;
PsiElement anchor = null;
if (myMethodToSearchFor.isVarArgs()) {
final int oldParamCount = myMethodToSearchFor.getParameterList().getParametersCount() - 1;
if (exprs.length >= oldParamCount) {
if (oldParamCount > 1) {
anchor = exprs[oldParamCount - 2];
}
else {
first = true;
anchor = null;
}
} else {
anchor = exprs[exprs.length -1];
}
} else if (exprs.length > 0) {
anchor = exprs[exprs.length - 1];
}
if (anchor != null) {
argList.addAfter(expression, anchor);
}
else {
argList.add(expression);
if (first && exprs.length > 0) {
argList.addBefore(expression, exprs[0]);
} else {
argList.add(expression);
}
}
removeParametersFromCall(argList);
@@ -0,0 +1,9 @@
class Test {
void foo(boolean flag, int i, final int anObject) {
if (flag) {
foo(false, i, anObject);
}
System.out.println();
foo(false, i, anObject);
}
}
@@ -0,0 +1,9 @@
class Test {
void foo(boolean flag, final String anObject, int... i) {
if (flag) {
foo(false, anObject, i);
}
System.out.println();
foo(flag, anObject, i);
}
}
@@ -0,0 +1,6 @@
class Test {
void foo(final String anObject, int... i) {
foo(anObject, 1);
foo(anObject, 3);
}
}
@@ -0,0 +1,9 @@
class Test {
void foo(boolean flag, int i) {
if (flag) {
foo(false, i, <selection>i + 1</selection>);
}
System.out.println();
foo(false, i);
}
}
@@ -0,0 +1,9 @@
class Test {
void foo(boolean flag, int... i) {
if (flag) {
foo(false, <selection>""</selection>, i);
}
System.out.println();
foo(flag, i);
}
}
@@ -0,0 +1,6 @@
class Test {
void foo(int... i) {
foo(<selection>""</selection>, 1);
foo(3);
}
}
@@ -283,6 +283,18 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, true);
}
public void testSelfReference() throws Exception {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
}
public void testSelfReferenceVarargs() throws Exception {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
}
public void testSelfReferenceVarargs1() throws Exception {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
}
private void doTestThroughHandler() throws Exception {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
boolean enabled = true;