mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method object from expression: declare necessary variables after call to replace invoke() with correct getter
This commit is contained in:
+11
-4
@@ -733,9 +733,15 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
PsiExpression expression2Replace = myExpression;
|
||||
if (myExpression instanceof PsiAssignmentExpression) {
|
||||
expression2Replace = ((PsiAssignmentExpression)myExpression).getRExpression();
|
||||
} else if (myExpression instanceof PsiPostfixExpression || myExpression instanceof PsiPrefixExpression) {
|
||||
PsiExpression operand = myExpression instanceof PsiPostfixExpression ? ((PsiPostfixExpression)myExpression).getOperand() :
|
||||
((PsiPrefixExpression)myExpression).getOperand();
|
||||
expression2Replace =
|
||||
((PsiBinaryExpression)myExpression.replace(myElementFactory.createExpressionFromText(operand.getText() + " + x", operand))).getROperand();
|
||||
}
|
||||
final PsiElement replacement = IntroduceVariableBase.replace(expression2Replace, myMethodCall, myProject);
|
||||
myMethodCall = PsiTreeUtil.getParentOfType(replacement.findElementAt(replacement.getText().indexOf(myMethodCall.getText())), PsiMethodCallExpression.class);
|
||||
myExpression = (PsiExpression)IntroduceVariableBase.replace(expression2Replace, myMethodCall, myProject);
|
||||
myMethodCall = PsiTreeUtil.getParentOfType(myExpression.findElementAt(myExpression.getText().indexOf(myMethodCall.getText())), PsiMethodCallExpression.class);
|
||||
declareNecessaryVariablesAfterCall(myOutputVariable);
|
||||
}
|
||||
|
||||
if (myAnchor instanceof PsiField) {
|
||||
@@ -874,9 +880,10 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
}
|
||||
}
|
||||
|
||||
protected PsiElement addToMethodCallLocation(PsiElement statement) throws IncorrectOperationException {
|
||||
protected PsiElement addToMethodCallLocation(PsiStatement statement) throws IncorrectOperationException {
|
||||
if (myEnclosingBlockStatement == null) {
|
||||
return myElements[0].getParent().addBefore(statement, myElements[0]);
|
||||
PsiStatement containingStatement = PsiTreeUtil.getParentOfType(myExpression != null ? myExpression : myElements[0], PsiStatement.class, false);
|
||||
return containingStatement.getParent().addBefore(statement, containingStatement);
|
||||
}
|
||||
else {
|
||||
return myEnclosingBlockStatement.getParent().addBefore(statement, myEnclosingBlockStatement);
|
||||
|
||||
+14
@@ -342,6 +342,13 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
|
||||
LOG.assertTrue(declaration != null);
|
||||
declaration.replace(assignmentStatement);
|
||||
} else {
|
||||
if (statement instanceof PsiReturnStatement) {
|
||||
final PsiExpression returnValue = ((PsiReturnStatement)statement).getReturnValue();
|
||||
if (!(returnValue instanceof PsiReferenceExpression || returnValue == null || returnValue instanceof PsiLiteralExpression)) {
|
||||
statement.getParent()
|
||||
.addBefore(myElementFactory.createStatementFromText(returnValue.getText() + ";", returnValue), statement);
|
||||
}
|
||||
}
|
||||
statement.replace(replacement);
|
||||
}
|
||||
} else {
|
||||
@@ -691,6 +698,8 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
|
||||
methodCallStatement.getParent().addBefore(declarationStatement, methodCallStatement);
|
||||
final PsiExpression conditionExpression = ((PsiIfStatement)methodCallStatement).getCondition();
|
||||
conditionExpression.replace(myElementFactory.createExpressionFromText(object + ".is()", myInnerMethod));
|
||||
} else if (myElements[0] instanceof PsiExpression){
|
||||
methodCallStatement.getParent().addBefore(declarationStatement, methodCallStatement);
|
||||
} else {
|
||||
methodCallStatement.replace(declarationStatement);
|
||||
}
|
||||
@@ -721,6 +730,11 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
|
||||
addToMethodCallLocation(st);
|
||||
}
|
||||
}
|
||||
if (myElements[0] instanceof PsiAssignmentExpression) {
|
||||
getMethodCall().getParent().replace(((PsiAssignmentExpression)getMethodCall().getParent()).getLExpression());
|
||||
} else if (myElements[0] instanceof PsiPostfixExpression || myElements[0] instanceof PsiPrefixExpression) {
|
||||
getMethodCall().getParent().replace(((PsiBinaryExpression)getMethodCall().getParent()).getLOperand());
|
||||
}
|
||||
}
|
||||
else {
|
||||
super.declareNecessaryVariablesAfterCall(outputVariable);
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
int y = <selection>x = 1</selection>;
|
||||
System.out.println(x + y);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
Inner inner = new Inner().invoke();
|
||||
x = inner.getX();
|
||||
int y = x;
|
||||
System.out.println(x + y);
|
||||
}
|
||||
|
||||
private class Inner {
|
||||
private int x;
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Inner invoke() {
|
||||
x = 1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
int y = <selection>x++</selection>;
|
||||
System.out.println(x + y);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
Inner inner = new Inner(x).invoke();
|
||||
x = inner.getX();
|
||||
int y = x;
|
||||
System.out.println(x + y);
|
||||
}
|
||||
|
||||
private class Inner {
|
||||
private int x;
|
||||
|
||||
public Inner(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Inner invoke() {
|
||||
x++;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -84,4 +84,12 @@ public class ExtractMethodObjectWithMultipleExitPointsTest extends LightCodeInsi
|
||||
public void testUniqueObjectName() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testExtractedAssignmentExpression() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testExtractedIncExpression() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user