我不知道是否可以作为模板来执行此操作,因为模板必须知道您使用的是哪种枚举类型。 但是您可以编写一个小脚本来为您打印该语句,然后将其输出复制到您的源文件中。
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}");
}
}
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
}