Community
Participate
Working Groups
I have a block of code like this: for (...) { some code /*[*/ if (condition) continue; more code here /*]*/ } and I do an extract method on the bracket surrounded code (I hope I remembered the notation correctly). It fails with an error message that essentially says it can't do this because of the continue. But, beceause the selection runs to the end of the for block, it should be able to handle this by translating the continue to a return (provided the new method returns void).
*** Bug 54947 has been marked as a duplicate of this bug. ***
This would really be useful when refactoring spaghetti-code.
Agree. Will see if we can do something for 3.1
Created attachment 142588 [details] patch+testcases Ok, finally I think I got all edge cases covered ;-) Markus, do you still see any case I missed? Any thoughts on the patch?
Created attachment 143249 [details] Patch 2 Thanks, I've released the patch with a few bug fixes and a few stylistic nitpicks: - ExtractMethodAnalyzer#canHandleBranches(): - early returns are easier to understand - "if (body != null) { if (body instanceof Block)...": null check is unnecessary - updated the error messages Example with remaining problems I fixed: public static void main(String[] args) { outer: for (int i= 0; i < 3; i++) { /*[*/ inner: for (int j= 0; j < 3; j++) { // while (i == j) {} //made both continues become returns if (i == 1) continue outer; if (j == 2) continue inner; System.out.println(i + ", " + j); // break outer; //must block! // break inner; //must block! break; } /*]*/ } } - Patch did not allow me to extract the whole block (including curly braces) of the outer loop: { /*[*/ ... /*]*/ } - If you switched the order of the two continue statements, the refactoring still showed the error message, although extracting works fine. - Did not handle break statements (we don't support breaking out of outer loops) - I did not verify breaks inside switch statements. Can you please check whether we have a test for that? - The ASTVisitor in ExtractMethodRefactoring#replaceBranches(CompilationUnitChange) failed when - continue statement was after a nested loop - labelled loop was not a for loop, but enhaced for, do, or while If you could attach another patch with test cases for these, that would be great!
Benny, any plans to provide tests?
Ping! Benny, any plans to work on this?