57

Is there a CTRL+space -like way of "auto-constructing" a switch case around a given Java Enum in Eclipse? I'd like a stub with all Enum cases...

Maroloccio
  • 1,143
  • 2
  • 12
  • 22
  • Just in case you don't already know, you *can* get Eclipse to fill in each `case` statement for you (just type `case` and then ctrl-space) which already saves a fair amount of time. It will automatically exclude values you've already used but it doesn't absolutely guarantee you won't forget one, like the template you're suggesting. – Tyler Apr 11 '10 at 22:40

3 Answers3

128

It has been in Eclipse for ages. It's admittedly only a bit hard to find. First start with

switch (myEnum) {

}

At that point, your cursor would usually be inside the statement block {}. You need to put your cusror back to the line with the switch keyword and press Ctrl+1 and choose Add missing case statements. This way it will insert any possible case.

alt text

You'd intuitively expect this option to be available inside the statement block {} as well, but no.


Update: since Eclipse Kepler (or perhaps already Juno, but it's so instable that I never really used it), this option is finally available via Ctrl+1 inside the statement block as well.

enter image description here

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    Eclipse has so many features that are awesome but hard to find. Are there any good quick references? There was a recent SO thread that had a lot of good tips. – Jonathon Faust Apr 11 '10 at 23:27
  • 3
    Thanks! This answer helped me discover a similar technique for NetBeans. After typing `switch (myEnum)`, put your cursor inside the `(` and press Alt+Enter for an option to "Add missing case clauses." – Brandon Jan 04 '14 at 09:44
  • @BrandonMintern wow thanks. Emphasis on "put your cursor inside the parenthesis (`(`)" - not the bracket (`{`). – ryvantage Jun 06 '14 at 17:37
4

I don't know if it's possible to do this as a template, because the template would have to know which enum type you were using. But you could write a little script to print out the statement for you, and then just copy its output into your source file.

public class SwitchWriter {
  public static void printSwitchStatement(String varName, Class<?> E) {
    System.out.format("switch(%s) {\n", varName);
    for (Object o : E.getEnumConstants()) {
      System.out.format("case %s:\n  // TODO: Auto-generated switch statement stub\n  break;\n", o);
    }
    System.out.println("default:\n  // TODO: Auto-generated switch statement stub\n}");
  }
}

Output of SwitchWriter.printSwitchStatement("action", java.awt.Desktop.Action.class):

switch(action) {
case OPEN:
  // TODO: Auto-generated switch statement stub
  break;
case EDIT:
  // TODO: Auto-generated switch statement stub
  break;
case PRINT:
  // TODO: Auto-generated switch statement stub
  break;
case MAIL:
  // TODO: Auto-generated switch statement stub
  break;
case BROWSE:
  // TODO: Auto-generated switch statement stub
  break;
default:
  // TODO: Auto-generated switch statement stub
}
Tyler
  • 21,762
  • 11
  • 61
  • 90
0

You can add your own code templates using: Windows->Preferences->Java->Editor->Templates.

Once you have added a code template, type enough characters of the template name to make it unique; type CTRL+Space; and your defined code will replace the template name characters.

The template for switch is predefined in Eclipse Galileo. sw+CTRL+Space should give you a switch statement. You might have to adapt an existing template to give you the switch-enum combination.

richj
  • 7,499
  • 3
  • 32
  • 50