IDEA-209743 Extracted variable from case label expression should be 'final'

This commit is contained in:
Tagir Valeev
2019-04-04 19:21:51 +07:00
parent 7586b9608f
commit bdcdc4379e
4 changed files with 34 additions and 1 deletions
@@ -1050,7 +1050,8 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
final boolean replaceAll = replaceChoice.isMultiple();
final SuggestedNameInfo suggestedName = getSuggestedName(typeSelectorManager.getDefaultType(), expr, anchor);
final String variableName = suggestedName.names.length > 0 ? suggestedName.names[0] : "";
final boolean declareFinal = replaceAll && declareFinalIfAll || !anyAssignmentLHS && createFinals(anchor.getContainingFile());
final boolean declareFinal = replaceAll && declareFinalIfAll || !anyAssignmentLHS && createFinals(anchor.getContainingFile()) ||
anchor instanceof PsiSwitchLabelStatementBase;
final boolean declareVarType = canBeExtractedWithoutExplicitType(expr) && createVarType();
final boolean replaceWrite = anyAssignmentLHS && replaceChoice.isAll();
return new IntroduceVariableSettings() {
@@ -0,0 +1,14 @@
class A {
private void A() {
final int i = 2;
switch (1) {
case 1:
System.out.println(1);
break;
case i:
System.out.println(3);
break;
}
}
}
@@ -0,0 +1,13 @@
class A {
private void A() {
switch (1) {
case 1:
System.out.println(1);
break;
case <selection>2</selection>:
System.out.println(3);
break;
}
}
}
@@ -12,6 +12,7 @@ import com.intellij.psi.PsiType;
import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.introduceVariable.InputValidator;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.refactoring.introduceVariable.IntroduceVariableHandler;
import com.intellij.refactoring.introduceVariable.IntroduceVariableSettings;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.testFramework.LightCodeInsightTestCase;
@@ -221,6 +222,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("temp", true, false, false, "int"));
}
public void testCaseLabelSingle() {
doTest(new IntroduceVariableHandler());
}
public void testCaseLabelEnum() {
try {
doTest(new MockIntroduceVariableHandler("temp", true, false, false, ""));