mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-07 22:09:38 +07:00
extract method: don't skip vars from the exit statements if nullable checks would be added (IDEA-141562)
This commit is contained in:
@@ -242,10 +242,10 @@ public class ControlFlowWrapper {
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<PsiVariable> getInputVariables(final PsiElement codeFragment, PsiElement[] elements) {
|
||||
public List<PsiVariable> getInputVariables(final PsiElement codeFragment, PsiElement[] elements, PsiVariable[] outputVariables) {
|
||||
final List<PsiVariable> inputVariables = ControlFlowUtil.getInputVariables(myControlFlow, myFlowStart, myFlowEnd);
|
||||
List<PsiVariable> myInputVariables;
|
||||
if (myGenerateConditionalExit) {
|
||||
if (skipVariablesFromExitStatements(outputVariables)) {
|
||||
List<PsiVariable> inputVariableList = new ArrayList<PsiVariable>(inputVariables);
|
||||
removeParametersUsedInExitsOnly(codeFragment, inputVariableList);
|
||||
myInputVariables = inputVariableList;
|
||||
@@ -341,8 +341,16 @@ public class ControlFlowWrapper {
|
||||
return getUsedVariables(myFlowEnd);
|
||||
}
|
||||
|
||||
public List<PsiVariable> getUsedVariablesInBody() {
|
||||
return getUsedVariables(myFlowStart, myFlowEnd);
|
||||
public List<PsiVariable> getUsedVariablesInBody(PsiElement codeFragment, PsiVariable[] outputVariables) {
|
||||
final List<PsiVariable> variables = getUsedVariables(myFlowStart, myFlowEnd);
|
||||
if (skipVariablesFromExitStatements(outputVariables)) {
|
||||
removeParametersUsedInExitsOnly(codeFragment, variables);
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
private boolean skipVariablesFromExitStatements(PsiVariable[] outputVariables) {
|
||||
return myGenerateConditionalExit && outputVariables.length == 0;
|
||||
}
|
||||
|
||||
public Collection<ControlFlowUtil.VariableInfo> getInitializedTwice() {
|
||||
|
||||
@@ -1438,7 +1438,7 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
}
|
||||
|
||||
private boolean chooseTargetClass(PsiElement codeFragment, final Pass<ExtractMethodProcessor> extractPass) throws PrepareFailedException {
|
||||
final List<PsiVariable> inputVariables = myControlFlowWrapper.getInputVariables(codeFragment, myElements);
|
||||
final List<PsiVariable> inputVariables = myControlFlowWrapper.getInputVariables(codeFragment, myElements, myOutputVariables);
|
||||
|
||||
myNeedChangeContext = false;
|
||||
myTargetClass = myCodeFragmentMember instanceof PsiMember
|
||||
@@ -1513,7 +1513,7 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
}
|
||||
|
||||
private void declareNecessaryVariablesInsideBody(PsiCodeBlock body) throws IncorrectOperationException {
|
||||
List<PsiVariable> usedVariables = myControlFlowWrapper.getUsedVariablesInBody();
|
||||
List<PsiVariable> usedVariables = myControlFlowWrapper.getUsedVariablesInBody(ControlFlowUtil.findCodeFragment(myElements[0]), myOutputVariables);
|
||||
for (PsiVariable variable : usedVariables) {
|
||||
boolean toDeclare = !isDeclaredInside(variable) && myInputVariables.toDeclareInsideBody(variable);
|
||||
if (toDeclare) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
class X {
|
||||
static String guessTestDataName(String method, String testName, String[] methods) {
|
||||
for (String psiMethod : methods) {
|
||||
<selection>String strings = method;
|
||||
if (strings != null && !strings.isEmpty()) {
|
||||
return strings.substring(0) + testName;
|
||||
}
|
||||
</selection>
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class X {
|
||||
static String guessTestDataName(String method, String testName, String[] methods) {
|
||||
for (String psiMethod : methods) {
|
||||
String strings = newMethod(method, testName);
|
||||
if (strings != null) return strings;
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String newMethod(String method, String testName) {
|
||||
String strings = method;
|
||||
if (strings != null && !strings.isEmpty()) {
|
||||
return strings.substring(0) + testName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ public class Test {
|
||||
}
|
||||
|
||||
private boolean newMethod() {
|
||||
Object result;
|
||||
if (test1()) return true;
|
||||
if (test2()) return true;
|
||||
return false;
|
||||
|
||||
@@ -749,6 +749,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testConditionalExitCombinedWithNullabilityShouldPreserveVarsUsedInExitStatements() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTestDisabledParam() throws PrepareFailedException {
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.ELSE_ON_NEW_LINE = true;
|
||||
|
||||
Reference in New Issue
Block a user