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.

Bug 268499 - Refactoring to switch misplaces label for break
Summary: Refactoring to switch misplaces label for break
Status: RESOLVED INVALID
Alias: None
Product: java
Classification: Unclassified
Component: Refactoring (show other bugs)
Version: 8.1
Hardware: PC Linux
: P4 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-13 08:34 UTC by ivo.bloechliger
Modified: 2016-10-13 12:50 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ivo.bloechliger 2016-10-13 08:34:08 UTC
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
Comment 1 Svata Dedic 2016-10-13 12:50:46 UTC
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.