Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: Java Enum auto-completion of switch case

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...

like image 733
Maroloccio Avatar asked Apr 11 '10 21:04

Maroloccio


People also ask

How do I turn on autocomplete in eclipse?

Step 1: Open your Eclipse or Spring Tool Suite, then go to the Window > Preferences as shown in the below image. Step 2: In the next screen go to the Java > Editor > Content Assist > Auto activation triggers for Java as shown in the below image.

Can we use Enum in switch case Java?

We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive.

Is there autocomplete in eclipse?

In Eclipse click Windows Drop-Down > Click Preferences > Navigate to Java > Editor > Content Assist In order to Make the Auto suggestion/completion to work you have to set trigger that invoke the Auto Completion.


2 Answers

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

like image 125
BalusC Avatar answered Sep 19 '22 07:09

BalusC


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 } 
like image 41
Tyler Avatar answered Sep 20 '22 07:09

Tyler