In the wiki2wikiproject I am currently working on is a java-Enum called Wikitype:
public enum WikiType {
twiki,jspwiki
}
We all now the nice an sweet construct named switch..case. One would suggest it was quite easy to use this enum in a switch statement:
switch (mode) {
case WikiType.jspwiki:
return new JSPWikiSyntax();
case WikiType.twiki:
return new TWikiSyntax();
}
Seems right, but Java has kind of an own opinion about that:
The enum constant WikiType.twiki reference cannot be qualified in a case label
Odd. So we try to use the switch with a string. This tells us switch can only be used on integers and enums. Odd enough. A bit it clicking in eclipse leads us to the following code:
switch (mode) {
case (WikiType) WikiType.valueOf("jspwiki"):
return new JSPWikiSyntax();
case (WikiType) WikiType.valueOf("twiki"):
return new TWikiSyntax();
}
Still not working:
Type mismatch: cannot convert from WikiType to WikiType
The solution to this problem is actually quite simple, though a bit irrational. The case statements inherit the context of the switch argument object. So there is no need to fully qualify the cases.
switch (mode) {
case jspwiki:
return new JSPWikiSyntax();
case twiki:
return new TWikiSyntax();
}