-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enum values in switch() #41
Comments
Being on vacation. Fixing this afterwards. Should be easy. |
Please verify if it works for you as well. I remove emitting the type in the |
…num code generation bug introduced in phax/jcodemodel#41.
Unfortunately this change breaks code generation in my project because it results in emitting an unscoped enum name in a case where the fully-qualified name is required. The generated code looks like this: public class Simple4 {
private State_Simple4 __state;
private void __conserveFields(State_Simple4 nextState) {
if (getState() == S1) { // need to be State_Simple4.S1
…
}
}
public enum State_Simple4 {
S1,
S2;
}
} |
Agreed - had the same issue myself in other scenarios. |
Do you need it in the 2.8.x series, or can you use Java 8 with upcoming V3? |
V3 is probably fine. For now I'm using a locally-modified V3 and it seems to work okay otherwise. Thanks! |
So you basically reverted the changes mentioned in the above referenced commit - right? |
Yes, though I realize that's not the right fix. Presumably we need to check to see what scope the enum was defined in and emit the right thing? |
Yep :) |
@mcoblenz So the current solution should suffice both requirements - the difference is done in |
3.0.0 apparently is back to fully qualified enumeration constants in switch. |
I added a quick test and it prints: -----------------------------------issue41.Issue41Test.java-----------------------------------
package issue41;
public class Issue41Test {
void dummy(Issue41Test.MyEnum enumParam) {
switch (enumParam) {
case A:
{
break;
}
case B:
{
break;
}
default:
{
break;
}
}
}
public enum MyEnum {
A,
B,
C;
}
} ==> so to me it looks good... |
ok, I probably need to minimize it, but Im switching version 2.8.6 and 3.0.0 for existing project and I have case A: w 2.8.6 and case Enum.A: for 3.0.0 |
Are you using it like this: ...
final JEnumConstant ca = jEnumClass.enumConstant ("A");
...
final JSwitch s = method.body ()._switch (p);
...
s._case (ca).body ()._break ();
... or do you use the |
JEnumConstant enumEntry(JDefinedClass enumClass, String valueName, int value, String description) {
ie as u say, but with return instead break. also it calls enumConstant() twice, plus its having argument. |
Okay, so you have a method where the case is based on an int and the return value is an enum constant. Working on a test case |
correct, sorry for being unclear in the begining. |
Test code: final JCodeModel cm = new JCodeModel ();
final JDefinedClass c2 = cm._package ("issue41")._class ("Issue41Test2");
final JDefinedClass jEnumClass = c2._enum ("MyEnum");
final JEnumConstant ca = jEnumClass.enumConstant ("A");
final JEnumConstant cb = jEnumClass.enumConstant ("B");
jEnumClass.enumConstant ("C");
final JMethod m = c2.method (0, jEnumClass, "dummy");
final JVar p = m.param (cm.INT, "val");
final JSwitch s = m.body ()._switch (p);
s._case (JExpr.lit (1)).body ()._return (ca);
s._case (JExpr.lit (2)).body ()._return (cb);
s._default ().body ()._return (JExpr._null ()); output: package issue41;
public class Issue41Test2 {
Issue41Test2 .MyEnum dummy(int val) {
switch (val) {
case 1 :
{
return Issue41Test2 .MyEnum.A;
}
case 2 :
{
return Issue41Test2 .MyEnum.B;
}
default:
{
return null;
}
}
}
public enum MyEnum {
A,
B,
C;
}
} |
When generating a switch on an enum, the case labels should not include the enum class name, but only the enum label.
It's an old issue with the original codemodel.
Unfortunately, I have no idea how to fix this with the
generate()
method architecture. I solved it on my local codemodel copy with an uglyinstanceof
inJCase
.The text was updated successfully, but these errors were encountered: