mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method object from anonymous class: update method call site when context should be changed; do not change call site though
This commit is contained in:
+8
-2
@@ -707,6 +707,11 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
|
||||
return myOutputVariables;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isNeedToChangeCallContext() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void declareNecessaryVariablesAfterCall(final PsiVariable outputVariable) throws IncorrectOperationException {
|
||||
if (myMultipleExitPoints) {
|
||||
@@ -718,11 +723,12 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
|
||||
if (methodCallStatement instanceof PsiIfStatement) {
|
||||
methodCallStatement.getParent().addBefore(declarationStatement, methodCallStatement);
|
||||
final PsiExpression conditionExpression = ((PsiIfStatement)methodCallStatement).getCondition();
|
||||
conditionExpression.replace(myElementFactory.createExpressionFromText(object + ".is()", myInnerMethod));
|
||||
setMethodCall((PsiMethodCallExpression)conditionExpression.replace(myElementFactory.createExpressionFromText(object + ".is()", myInnerMethod)));
|
||||
} else if (myElements[0] instanceof PsiExpression){
|
||||
methodCallStatement.getParent().addBefore(declarationStatement, methodCallStatement);
|
||||
} else {
|
||||
methodCallStatement.replace(declarationStatement);
|
||||
final PsiDeclarationStatement replace = (PsiDeclarationStatement)methodCallStatement.replace(declarationStatement);
|
||||
setMethodCall((PsiMethodCallExpression)((PsiLocalVariable)replace.getDeclaredElements()[0]).getInitializer());
|
||||
}
|
||||
|
||||
final List<PsiVariable> usedVariables = myControlFlowWrapper.getUsedVariables();
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
<selection>int i = 0;
|
||||
int j = 0;</selection>
|
||||
System.out.println(i + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
Inner inner = new Inner().invoke();
|
||||
int i = inner.getI();
|
||||
int j = inner.getJ();
|
||||
System.out.println(i + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Inner {
|
||||
private int i;
|
||||
private int j;
|
||||
|
||||
public int getI() {
|
||||
return i;
|
||||
}
|
||||
|
||||
public int getJ() {
|
||||
return j;
|
||||
}
|
||||
|
||||
public Inner invoke() {
|
||||
i = 0;
|
||||
j = 0;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
<selection>String var = null;
|
||||
if (var == null) {
|
||||
return;
|
||||
}</selection>
|
||||
System.out.println(var);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
Inner inner = new Inner().invoke();
|
||||
if (inner.is()) return;
|
||||
String var = inner.getVar();
|
||||
System.out.println(var);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private class Inner {
|
||||
private boolean myResult;
|
||||
private String var;
|
||||
|
||||
boolean is() {
|
||||
return myResult;
|
||||
}
|
||||
|
||||
public String getVar() {
|
||||
return var;
|
||||
}
|
||||
|
||||
public Inner invoke() {
|
||||
var = null;
|
||||
if (var == null) {
|
||||
myResult = true;
|
||||
return this;
|
||||
}
|
||||
myResult = false;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -102,4 +102,12 @@ public class ExtractMethodObjectWithMultipleExitPointsTest extends LightCodeInsi
|
||||
public void testNonCanonicalNaming() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testExtractFromAnonymous() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testExtractFromIfStatementInsideAnonymous() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user