Skip to content
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

Switch with string constant as a label: Exception: [source error] bad case label #72

Open
SergiusSidorov opened this issue Mar 20, 2016 · 10 comments

Comments

@SergiusSidorov
Copy link

Hi,

I'm using javassist 3.12.1.GA with java 8.
When I try to generate method with the switch statement, which uses string constant as a label, javassist presents the following exception: "Exception: [source error] bad case label"
However, generated source is correct and works (I checked it in a real java class).

For example, generated method body:

{
switch($1.getClass().getName()) {
case "example.EventA":
handler0.onEvent((example.EventA)$1);
break;
case "example.EventB":
handler1.onEvent((example.EventB)$1);
break;
}
}

Does javassist support switch statement with string label?

@chibash
Copy link
Member

chibash commented Mar 20, 2016

The specification is that Javassist's compiler only supports Java 1.4 (yes, old!) syntax.
But, I'll check the code and, if it's easy to implement, I'll support string constants as a label.

@SergiusSidorov
Copy link
Author

Thanks!

@QIvan
Copy link

QIvan commented Mar 30, 2016

@chibash
Hi! I need this feature too. Did you check code? Is it hard to implement?

@smilanko
Copy link

smilanko commented Dec 7, 2016

any progress on this? While I do not mind using the if cases, a switch would really be nice ;)

@lygoing
Copy link

lygoing commented Feb 5, 2018

@chibash
Hi!
I also encountered the same situation! May I ask how to solve this problem now? thank you very much

@smilanko
Copy link

smilanko commented Feb 5, 2018

@lygoing I doubt that a solution has been made, in the version we are referencing. I ended up just using if statements to solve the issue. More code, but oh well, it works. Does doing the same not solve the problem for you?

@lygoing
Copy link

lygoing commented Feb 6, 2018

@smilanko
Hi!
The case is more efficient than if, and I am now solving this with the string hashcode.
like this:
switch (fieldName.hashCode()) {
case -861253476:
return context.getField13();
case -861253471:
return context.getField18();
}

@andreysubbotin
Copy link

Hi,
Do you have any plans to fix the problem?

@chibash
Copy link
Member

chibash commented Dec 9, 2018

I'm working... Check the test/switch-case branch.

@chibash
Copy link
Member

chibash commented Dec 11, 2018

I've done. See Test/switch case #231.

odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 10, 2019
3.25 fixes:
- jboss-javassist/javassist#72
- jboss-javassist/javassist#241
- jboss-javassist/javassist#242
- jboss-javassist/javassist#246
- jboss-javassist/javassist#252
3.26 fixes:
- jboss-javassist/javassist#265
- jboss-javassist/javassist#270
- jboss-javassist/javassist#271
- jboss-javassist/javassist#275

Of these #270 is most important, as it fixes an issue we've seen
with powermock downstream.

Change-Id: Ib4d75d6411e71438436249a8eb9313ccf4411ca2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 17, 2019
3.25 fixes:
- jboss-javassist/javassist#72
- jboss-javassist/javassist#241
- jboss-javassist/javassist#242
- jboss-javassist/javassist#246
- jboss-javassist/javassist#252
3.26 fixes:
- jboss-javassist/javassist#265
- jboss-javassist/javassist#270
- jboss-javassist/javassist#271
- jboss-javassist/javassist#275

Of these #270 is most important, as it fixes an issue we've seen
with powermock downstream.

Change-Id: Ib4d75d6411e71438436249a8eb9313ccf4411ca2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6a404f1)
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 17, 2019
3.25 fixes:
- jboss-javassist/javassist#72
- jboss-javassist/javassist#241
- jboss-javassist/javassist#242
- jboss-javassist/javassist#246
- jboss-javassist/javassist#252
3.26 fixes:
- jboss-javassist/javassist#265
- jboss-javassist/javassist#270
- jboss-javassist/javassist#271
- jboss-javassist/javassist#275

Of these #270 is most important, as it fixes an issue we've seen
with powermock downstream.

Change-Id: Ib4d75d6411e71438436249a8eb9313ccf4411ca2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6a404f1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants