Skip to content

Commit cefe750

Browse files
author
emmanue1
committed
Fix #249 : Bug with switch case multi-labels
1 parent f7c6684 commit cefe750

File tree

8 files changed

+65
-8
lines changed

8 files changed

+65
-8
lines changed

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/SwitchStatementMaker.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,24 @@ public static void makeSwitchString(LocalVariableMaker localVariableMaker, State
125125

126126
// Replace synthetic index by string
127127
for (SwitchStatement.Block block : switchStatement.getBlocks()) {
128-
SwitchStatement.LabelBlock lb = (SwitchStatement.LabelBlock) block;
128+
if (block.getClass() == SwitchStatement.LabelBlock.class) {
129+
SwitchStatement.LabelBlock lb = (SwitchStatement.LabelBlock) block;
129130

130-
if (lb.getLabel() != SwitchStatement.DEFAULT_LABEL) {
131-
SwitchStatement.ExpressionLabel el = (SwitchStatement.ExpressionLabel) lb.getLabel();
132-
IntegerConstantExpression nce = (IntegerConstantExpression) el.getExpression();
133-
el.setExpression(new StringConstantExpression(nce.getLineNumber(), map.get(nce.getValue())));
131+
if (lb.getLabel() != SwitchStatement.DEFAULT_LABEL) {
132+
SwitchStatement.ExpressionLabel el = (SwitchStatement.ExpressionLabel) lb.getLabel();
133+
IntegerConstantExpression nce = (IntegerConstantExpression) el.getExpression();
134+
el.setExpression(new StringConstantExpression(nce.getLineNumber(), map.get(nce.getValue())));
135+
}
136+
} else if (block.getClass() == SwitchStatement.MultiLabelsBlock.class) {
137+
SwitchStatement.MultiLabelsBlock lmb = (SwitchStatement.MultiLabelsBlock) block;
138+
139+
for (SwitchStatement.Label label : lmb.getLabels()) {
140+
if (label != SwitchStatement.DEFAULT_LABEL) {
141+
SwitchStatement.ExpressionLabel el = (SwitchStatement.ExpressionLabel) label;
142+
IntegerConstantExpression nce = (IntegerConstantExpression) el.getExpression();
143+
el.setExpression(new StringConstantExpression(nce.getLineNumber(), map.get(nce.getValue())));
144+
}
145+
}
134146
}
135147
}
136148

src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,17 @@ public void testJdk170AdvancedSwitch() throws Exception {
11441144
assertTrue(source.matches(PatternMaker.make("[ 22: 0]", "case B:")));
11451145
assertTrue(source.matches(PatternMaker.make("[ 25: 0]", "case C:")));
11461146

1147-
assertTrue(source.matches(PatternMaker.make("[ 38: 38]", "switch (str)")));
1148-
assertTrue(source.matches(PatternMaker.make("[ 39: 0]", "case \"One\":")));
1149-
assertTrue(source.matches(PatternMaker.make("[ 40: 40]", "System.out.println(1);")));
1147+
assertTrue(source.matches(PatternMaker.make("[ 39: 0]", "case A:")));
1148+
assertTrue(source.matches(PatternMaker.make("[ 40: 0]", "case B:")));
1149+
assertTrue(source.matches(PatternMaker.make("[ 41: 41]", "System.out.println(\"A or B\");")));
1150+
1151+
assertTrue(source.matches(PatternMaker.make("[ 56: 56]", "switch (str)")));
1152+
assertTrue(source.matches(PatternMaker.make("[ 57: 0]", "case \"One\":")));
1153+
assertTrue(source.matches(PatternMaker.make("[ 58: 58]", "System.out.println(1);")));
1154+
1155+
assertTrue(source.matches(PatternMaker.make("[ 78: 0]", "case \"One\":")));
1156+
assertTrue(source.matches(PatternMaker.make("[ 79: 0]", "case \"POe\":")));
1157+
assertTrue(source.matches(PatternMaker.make("[ 80: 80]", "System.out.println(\"'One' or 'POe'\");")));
11501158

11511159
assertTrue(source.indexOf("// Byte code:") == -1);
11521160
assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1);

src/test/resources/java/org/jd/core/test/AdvancedSwitch.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ public void switchEnum(TestEnum te) {
3232
System.out.println("end");
3333
}
3434

35+
public void switchEnumBis(TestEnum te) {
36+
System.out.println("start");
37+
38+
switch (te) {
39+
case A:
40+
case B:
41+
System.out.println("A or B");
42+
break;
43+
case C:
44+
System.out.println("C");
45+
break;
46+
default:
47+
System.out.println("default");
48+
}
49+
50+
System.out.println("end");
51+
}
52+
3553
public void switchString(String str) {
3654
System.out.println("start");
3755

@@ -52,4 +70,23 @@ public void switchString(String str) {
5270

5371
System.out.println("end");
5472
}
73+
74+
public void switchStringBis(String str) {
75+
System.out.println("start");
76+
77+
switch (str) {
78+
case "One":
79+
case "POe":
80+
System.out.println("'One' or 'POe'");
81+
break;
82+
case "Two":
83+
System.out.println(2);
84+
break;
85+
default:
86+
System.out.println("?");
87+
break;
88+
}
89+
90+
System.out.println("end");
91+
}
5592
}
2.22 KB
Binary file not shown.
151 Bytes
Binary file not shown.
163 Bytes
Binary file not shown.
2.61 KB
Binary file not shown.
2.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)