extract method: break&continue statements inside selected cycle should not treated as exit statements from method

This commit is contained in:
anna
2009-10-19 21:31:55 +04:00
parent 21c828e805
commit 29ad3e617c
4 changed files with 47 additions and 4 deletions
@@ -264,7 +264,7 @@ public class ControlFlowUtil {
}
@Override public void visitBranchingInstruction(BranchingInstruction instruction, int offset, int nextOffset) {
processGoto(flow, start, end, exitPoints, exitStatements, instruction.offset, classesFilter, findStatement(flow, offset));
processGoto(flow, start, end, exitPoints, exitStatements, instruction, classesFilter, findStatement(flow, offset));
}
// call/return do not incur exit points
@@ -297,8 +297,9 @@ public class ControlFlowUtil {
private static void processGoto(ControlFlow flow, int start, int end,
IntArrayList exitPoints,
Collection<PsiStatement> exitStatements, int gotoOffset, Class[] classesFilter, final PsiStatement statement) {
Collection<PsiStatement> exitStatements, BranchingInstruction instruction, Class[] classesFilter, final PsiStatement statement) {
if (statement == null) return;
int gotoOffset = instruction.offset;
if (start > gotoOffset || gotoOffset >= end || isElementOfClass(statement, classesFilter)) {
// process chain of goto's
gotoOffset = promoteThroughGotoChain(flow, gotoOffset);
@@ -306,7 +307,16 @@ public class ControlFlowUtil {
if (!exitPoints.contains(gotoOffset) && (gotoOffset >= end || gotoOffset < start)) {
exitPoints.add(gotoOffset);
}
processGotoStatement(classesFilter, exitStatements, statement);
if (gotoOffset >= end || gotoOffset < start) {
processGotoStatement(classesFilter, exitStatements, statement);
} else {
boolean isReturn = instruction instanceof GoToInstruction && ((GoToInstruction)instruction).isReturn;
final Instruction gotoInstruction = flow.getInstructions().get(gotoOffset);
isReturn |= gotoInstruction instanceof GoToInstruction && ((GoToInstruction)gotoInstruction).isReturn;
if (isReturn) {
processGotoStatement(classesFilter, exitStatements, statement);
}
}
}
}
@@ -0,0 +1,12 @@
class Test {
String foo(String[] args) {
<selection>
for(String arg : args) {
if (arg == null) continue;
System.out.println(arg);
}
if (args.length == 0) return null;
</selection>
return null;
}
}
@@ -0,0 +1,17 @@
class Test {
String foo(String[] args) {
if (newMethod(args)) return null;
return null;
}
private boolean newMethod(String[] args) {
for(String arg : args) {
if (arg == null) continue;
System.out.println(arg);
}
if (args.length == 0) return true;
return false;
}
}
@@ -74,6 +74,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doExitPointsTest(false);
}
public void testContinueInside() throws Exception {
doTest();
}
public void testBooleanExpression() throws Exception {
doTest();
}
@@ -496,4 +500,4 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
return true;
}
}
}