Skip to content

Commit dd0f76a

Browse files
IDEA-227152 [java]: don't break methods call with unnecessary argument
for "Move Instance Method" refactoring GitOrigin-RevId: e6c49f9107a0597480e5ef88511f7465b4ff578b
1 parent 3ead992 commit dd0f76a

4 files changed

Lines changed: 48 additions & 7 deletions

File tree

java/java-impl-refactorings/src/com/intellij/refactoring/move/moveInstanceMethod/MoveInstanceMethodProcessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ private void correctMethodCall(final PsiMethodCallExpression expression, final b
387387
}
388388

389389
PsiExpression newArgument = null;
390-
391-
if (classReferencedByThis != null) {
390+
if (classReferencedByThis != null && !myOldClassParameterNames.isEmpty()) {
392391
@NonNls String thisArgumentText = null;
393392
if (manager.areElementsEquivalent(myMethod.getContainingClass(), classReferencedByThis)) {
394393
if (myOldClassParameterNames.containsKey(myMethod.getContainingClass())) {
@@ -420,7 +419,6 @@ private void correctMethodCall(final PsiMethodCallExpression expression, final b
420419
}
421420
}
422421

423-
424422
if (newArgument != null) {
425423
expression.getArgumentList().add(newArgument);
426424
}
@@ -650,7 +648,7 @@ private void addParameters(final PsiElementFactory factory, final PsiMethod meth
650648
}
651649
}
652650

653-
private String getParameterNameToCreate(@NotNull PsiClass aClass) {
651+
private String getParameterNameToCreate(PsiClass aClass) {
654652
return myOldClassParameterNames.get(aClass);
655653
}
656654
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class AbstractMoveInstanceMethod {
2+
public String code;
3+
4+
protected void <caret>setupApplication(Application customApplication, String code, Integer properties) {
5+
customApplication.setCode(code);
6+
customApplication.setProperties(properties);
7+
customApplication.start();
8+
}
9+
}
10+
11+
class MoveInstanceMethod extends AbstractMoveInstanceMethod {
12+
void setup(Application application, Integer properties) {
13+
setupApplication(application, code, properties);
14+
}
15+
}
16+
17+
abstract class Application {
18+
abstract void setCode(String code);
19+
abstract void setProperties(Integer properties);
20+
abstract void start();
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class AbstractMoveInstanceMethod {
2+
public String code;
3+
4+
}
5+
6+
class MoveInstanceMethod extends AbstractMoveInstanceMethod {
7+
void setup(Application application, Integer properties) {
8+
application.setupApplication(code, properties);
9+
}
10+
}
11+
12+
abstract class Application {
13+
abstract void setCode(String code);
14+
abstract void setProperties(Integer properties);
15+
abstract void start();
16+
17+
protected void setupApplication(String code, Integer properties) {
18+
setCode(code);
19+
setProperties(properties);
20+
start();
21+
}
22+
}

java/java-tests/testSrc/com/intellij/java/refactoring/moveMethod/MoveInstanceMethodTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class MoveInstanceMethodTest extends LightJavaCodeInsightTestCase {
5656
public void testOverloadingMethods() { doTest(true, 0); }
5757
public void testOverloadingMethods1() { doTest(true, 0); }
5858
public void testMoveAbstractMethod() { doTest(true, 0); }
59+
public void testNoIncorrectParameterAdded() { doTest(true, 0); }
5960
public void testPolyadicExpr() { doTest(true, 0); }
6061

6162
public void testIOOBE_MovingInvalidCode() { doTest(true, 0); }
@@ -140,10 +141,9 @@ private void doTest(boolean isTargetParameter, int targetIndex, String newVisibi
140141
PsiMethod method = (PsiMethod) targetElement;
141142
final PsiVariable targetVariable = isTargetParameter ? method.getParameterList().getParameters()[targetIndex] :
142143
method.getContainingClass().getFields()[targetIndex];
143-
new MoveInstanceMethodProcessor(getProject(),
144-
method, targetVariable, newVisibility, MoveInstanceMethodHandler.suggestParameterNames (method, targetVariable)).run();
144+
new MoveInstanceMethodProcessor(getProject(), method, targetVariable, newVisibility,
145+
MoveInstanceMethodHandler.suggestParameterNames (method, targetVariable)).run();
145146
checkResultByFile(filePath + ".after");
146-
147147
}
148148

149149
@Override

0 commit comments

Comments
 (0)