extract method: don't skip variables used in left side of assignments (IDEA-165386)

This commit is contained in:
Anna.Kozlova
2016-12-13 17:46:28 +01:00
parent fd8e9c3b03
commit d3134b026b
4 changed files with 56 additions and 11 deletions
@@ -18,7 +18,6 @@ package com.intellij.refactoring.extractMethod;
import com.intellij.codeInsight.PsiEquivalenceUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.controlFlow.*;
import com.intellij.psi.search.LocalSearchScope;
@@ -209,19 +208,12 @@ public class ControlFlowWrapper {
for (PsiReference ref : ReferencesSearch.search(variable, scope)) {
PsiElement element = ref.getElement();
int elementOffset = myControlFlow.getStartOffset(element);
if (elementOffset == -1) {
continue Variables;
}
if (elementOffset >= myFlowStart && elementOffset <= myFlowEnd) {
if (!isInExitStatements(element, myExitStatements)) continue Variables;
}
if (elementOffset == -1) { //references in local/anonymous classes should not be skipped
final PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (psiClass != null) {
final TextRange textRange = psiClass.getTextRange();
if (myControlFlow.getElement(myFlowStart).getTextOffset() <= textRange.getStartOffset() &&
textRange.getEndOffset() <= myControlFlow.getElement(myFlowEnd).getTextRange().getEndOffset()) {
continue Variables;
}
}
}
}
iterator.remove();
}
@@ -0,0 +1,22 @@
import java.util.List;
class Test {
boolean m(Object[] parsed, String[] parameters, List<String> args) {
int i = 1;
<selection>String parameter = parameters[i];
try {
parsed[i] = parseValue(args.get(i), parameter.getClass());
} catch (Exception e) {
return false;
}
</selection>
return false;
}
private static Object parseValue(String stringValue, Class type) {
return "1";
}
}
@@ -0,0 +1,27 @@
import java.util.List;
class Test {
boolean m(Object[] parsed, String[] parameters, List<String> args) {
int i = 1;
if (newMethod(parsed, parameters[i], args.get(i), i)) return false;
return false;
}
private boolean newMethod(Object[] parsed, String parameter1, String stringValue, int i) {
String parameter = parameter1;
try {
parsed[i] = parseValue(stringValue, parameter.getClass());
} catch (Exception e) {
return true;
}
return false;
}
private static Object parseValue(String stringValue, Class type) {
return "1";
}
}
@@ -426,6 +426,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testDontSkipVariablesUsedInLeftSideOfAssignments() throws Exception {
doTest();
}
public void testIDEADEV11748() throws Exception {
doTest();
}