Eclipse:Java 枚举自动完成 switch case
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2618797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse: Java Enum auto-completion of switch case
提问by Maroloccio
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...
在 Eclipse 中,是否有类似 CTRL+space 的“自动构造”切换案例的方式围绕给定的 Java 枚举?我想要一个包含所有 Enum 案例的存根......
回答by BalusC
It has been in Eclipse for ages. It's admittedly only a bit hard to find. First start with
它已在 Eclipse 中使用多年。诚然,它只是有点难找。首先从
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+1and choose Add missing case statements. This way it will insert any possible case
.
此时,您的光标通常位于语句块内{}
。您需要将光标放回带有switch
关键字的行,然后按Ctrl+1并选择Add missing case statements。这样它将插入任何可能的case
.
You'd intuitively expect this option to be available insidethe 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 finallyavailable via Ctrl+1inside the statement block as well.
更新:由于 Eclipse Kepler(或者可能已经是 Juno,但它非常不稳定,我从未真正使用过它),这个选项最终也可以通过语句块内的Ctrl+1获得。
回答by MatrixFrog
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)
:
的输出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
}
回答by richj
You can add your own code templates using: Windows->Preferences->Java->Editor->Templates.
您可以使用以下方法添加自己的代码模板: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.
添加代码模板后,键入模板名称的足够字符以使其唯一;输入CTRL+空格;并且您定义的代码将替换模板名称字符。
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.
switch 的模板是在 Eclipse Galileo 中预定义的。sw+CTRL+Space 应该给你一个 switch 语句。您可能需要调整现有模板才能为您提供 switch-enum 组合。