extract method: don't skip vars from the exit statements if nullable checks would be added (IDEA-141562)

This commit is contained in:
Anna Kozlova committed 2015-06-17 20:12:16 +03:00
1 parent 483df7b174
commit f2cddf7201
6 files changed
+51 -7

No files matched your search

@@ -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) {