Community
Participate
Working Groups
BETA_JAVA7 enum Day { Mon, Tue, Wed, Thurs, Fri, Sat, Sun, } public class X { public static void main(final String [] args) { switch (Day.Wed) { case Wed: System.out.println("Correct"); break; default: System.out.println("Wrong"); } } } With the cursor on switch, press ctrl-1. You see two quick assists: (a) Add missing case statements (b) Convert switch to if-else-if Opt for the first one. Now with the cursor on switch press ctrl-1, you get a "No suggestions available" message.
I also noticed that if a case block falls through to the subsequent one as in: public class X { public static void main(final String [] args) { switch (args[0]) { case "": System.out.println(""); // break; // uncommenting out this line shows the assist case "a": System.out.println("a"); break; } } } then the new quick assist is not proposed. This is intentional I presume.
> then the new quick assist is not proposed. This is intentional I presume. The quick assist is not new. Already an issue in older releases, e.g. 3.5.
(In reply to comment #1) > I also noticed that if a case block falls through to the subsequent > one as in: > then the new quick assist is not proposed. This is intentional I presume. Quick assist is not proposed in cases in which the conversion requires code duplication. e.g. the quick assist is offered in the following case ------------------------------------------------------ void foo(int a) { switch (a) { case 1: case 2: System.out.println(2); break; } } ------------------------------------------------------ the result is => ------------------------------------------------------ void foo(int a) { if (a == 1 || a == 2) { System.out.println(2); } } ------------------------------------------------------ but it is not offered in the following 2 cases - code to be executed for case 1 and case 2 is different hence upon conversion 2 if statements will be required with code for case 2 duplicated in both. (This is similar to snippet from comment 1) ------------------------------------------------------ void foo1(int a) { switch (a) { case 1: System.out.println(); case 2: System.out.println(2); break; } } ------------------------------------------------------ - a 'case' statement cannot be combined with 'default' in the if-else code. To think of it, a fall through from a 'case' to 'default' is not too intelligent code, as the case statement can simply be eliminated.(This is similar to snippet from comment 0) ------------------------------------------------------ void foo2(int a) { switch (a) { case 1: default: System.out.println(2); break; } } ------------------------------------------------------ Someone can argue that the conversion should happen in all cases, even if code duplication is required. However, I do not feel strongly enough about this to change anything here. Closing as WONTFIX.
Fixing this is not so hard, even if we avoid to copy the code. But I agree that it is low priority.
(In reply to comment #3) > Quick assist is not proposed in cases in which the conversion requires code > duplication. [..] > but it is not offered in the following 2 cases > - code to be executed for case 1 and case 2 is different hence upon conversion > 2 if statements will be required with code for case 2 duplicated in both. (This > is similar to snippet from comment 1) > ------------------------------------------------------ > void foo1(int a) { > switch (a) { > case 1: > System.out.println(); > case 2: > System.out.println(2); > break; > } > } > ------------------------------------------------------ Code duplication is not necessary and should indeed not be implemented. Just the conditions and the ifs need to be put a bit differently: if (a == 1 || a == 2) { if (a == 1) { System.out.println(); } System.out.println(2); } > - a 'case' statement cannot be combined with 'default' in the if-else code. To > think of it, a fall through from a 'case' to 'default' is not too intelligent > code, as the case statement can simply be eliminated.(This is similar to > snippet from comment 0) > ------------------------------------------------------ > void foo2(int a) { > switch (a) { > case 1: > default: > System.out.println(2); > break; > } > } > ------------------------------------------------------ The "default:" can just be translated to condition "true".
(In reply to comment #5) Makes sense. I will take another look.
The "default:" case is easy (and is the subject of comment 0). Comment 1 (cases without "break;") is a lot more complicated. Comment 5 only shows one example, but in practice, this can become more difficult (e.g. when the "break;" is inside another condition). That problem should go into a separate bug that is really P5 (i.e. not for now).
I've fixed the default case (and a few unused exception warnings) with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=a78dd099ed0210a89cf20f09bcb7907282e0fbc2 Keeping this bug for comment 1 (case statements with fall-through).