This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
When refactoring if-else if-else statements containing a break statement into a switch construct, the break label gets misplaced: Code before refactoring: public class SwitchRefactoringError { static void test(int t) { int r=0; while(true) { if (t==1) { r=10; } else if (t==2) { r=9; } else { r=8; break; // Terminates the while loop } } } } Code after applying the proposed refactoring into a switch statement: public class SwitchRefactoringError { static void test(int t) { int r=0; OUTER: while (true) { switch (t) { case 1: r=10; break; case 2: r=9; break; default: r=8; break OUTER; // Terminates the while loop. NOPE it restarts } } } } Ihmo, the label should be placed *after* the while loop, not before. Warm regards Ivo
The OUTER label identifies the while loop statement. According to break statement definition (JLS ยง14.15) the break OUTER transfers control to the while statement, which then immediately completes normally, that is exits the loop. The break label cannot be placed after the while loop: the statement following the `while' (which would be identified by such label) is not an enclosing staetment of the `break' and compile-time error would be reported. When running the sample with t = 3, the final value of r is 8, which is the same result as the original code produces.