extract functional parameter: ensure final inside anonymous, no additional qualifier (IDEA-138895)

This commit is contained in:
Anna Kozlova
2015-04-08 12:06:00 +02:00
parent e1e48eb1e8
commit 45c5c2163f
5 changed files with 35 additions and 3 deletions
@@ -47,7 +47,6 @@ import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.*;
import com.intellij.psi.controlFlow.ControlFlowUtil;
@@ -861,7 +860,7 @@ public class ExtractMethodProcessor implements MatchProvider {
final PsiStatement exitStatementCopy = prepareMethodBody(newMethod, true);
if (myExpression == null) {
if (myNeedChangeContext) {
if (myNeedChangeContext && isNeedToChangeCallContext()) {
for (PsiElement element : myElements) {
ChangeContextUtil.encodeContextInfo(element, false);
}
@@ -1387,7 +1386,7 @@ public class ExtractMethodProcessor implements MatchProvider {
else {
skipInstanceQualifier = instanceQualifier == null || instanceQualifier instanceof PsiThisExpression;
if (skipInstanceQualifier) {
if (myNeedChangeContext) {
if (isNeedToChangeCallContext() && myNeedChangeContext) {
boolean needsThisQualifier = false;
PsiElement parent = myCodeFragmentMember;
while (!myTargetClass.equals(parent)) {
@@ -437,6 +437,10 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
}
boolean mustBeFinal = false;
if (myExpr != null) {
final PsiElement parent = myExpr.getUserData(ElementToWorkOn.PARENT);
mustBeFinal = parent != null && PsiTreeUtil.getParentOfType(parent, PsiClass.class, PsiMethod.class) != method;
}
for (PsiExpression occurrence : occurences) {
if (PsiTreeUtil.getParentOfType(occurrence, PsiClass.class, PsiMethod.class) != method) {
mustBeFinal = true;
@@ -705,6 +709,11 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
return new MyAbstractExtractDialog();
}
@Override
protected boolean isNeedToChangeCallContext() {
return false;
}
public void setMethodName(String methodName) {
myMethodName = methodName;
}
@@ -0,0 +1,10 @@
class Test {
void foo(final Runnable anObject) {
Runnable c = new Runnable() {
@Override
public void run() {
anObject.run();
}
};
}
}
@@ -0,0 +1,10 @@
class Test {
void foo() {
Runnable c = new Runnable() {
@Override
public void run() {
<selection>System.out.println();</selection>
}
};
}
}
@@ -57,6 +57,10 @@ public class IntroduceFunctionalParameterTest extends LightRefactoringTestCase
doTest();
}
public void testInsideAnonymous() throws Exception {
doTest();
}
@NotNull
@Override
protected String getTestDataPath() {