Skip to content

Commit

Permalink
Fix java-decompiler#249 : Bug with switch case multi-labels
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Jul 1, 2019
1 parent f7c6684 commit cefe750
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,24 @@ public static void makeSwitchString(LocalVariableMaker localVariableMaker, State

// Replace synthetic index by string
for (SwitchStatement.Block block : switchStatement.getBlocks()) {
SwitchStatement.LabelBlock lb = (SwitchStatement.LabelBlock) block;
if (block.getClass() == SwitchStatement.LabelBlock.class) {
SwitchStatement.LabelBlock lb = (SwitchStatement.LabelBlock) block;

if (lb.getLabel() != SwitchStatement.DEFAULT_LABEL) {
SwitchStatement.ExpressionLabel el = (SwitchStatement.ExpressionLabel) lb.getLabel();
IntegerConstantExpression nce = (IntegerConstantExpression) el.getExpression();
el.setExpression(new StringConstantExpression(nce.getLineNumber(), map.get(nce.getValue())));
if (lb.getLabel() != SwitchStatement.DEFAULT_LABEL) {
SwitchStatement.ExpressionLabel el = (SwitchStatement.ExpressionLabel) lb.getLabel();
IntegerConstantExpression nce = (IntegerConstantExpression) el.getExpression();
el.setExpression(new StringConstantExpression(nce.getLineNumber(), map.get(nce.getValue())));
}
} else if (block.getClass() == SwitchStatement.MultiLabelsBlock.class) {
SwitchStatement.MultiLabelsBlock lmb = (SwitchStatement.MultiLabelsBlock) block;

for (SwitchStatement.Label label : lmb.getLabels()) {
if (label != SwitchStatement.DEFAULT_LABEL) {
SwitchStatement.ExpressionLabel el = (SwitchStatement.ExpressionLabel) label;
IntegerConstantExpression nce = (IntegerConstantExpression) el.getExpression();
el.setExpression(new StringConstantExpression(nce.getLineNumber(), map.get(nce.getValue())));
}
}
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1144,9 +1144,17 @@ public void testJdk170AdvancedSwitch() throws Exception {
assertTrue(source.matches(PatternMaker.make("[ 22: 0]", "case B:")));
assertTrue(source.matches(PatternMaker.make("[ 25: 0]", "case C:")));

assertTrue(source.matches(PatternMaker.make("[ 38: 38]", "switch (str)")));
assertTrue(source.matches(PatternMaker.make("[ 39: 0]", "case \"One\":")));
assertTrue(source.matches(PatternMaker.make("[ 40: 40]", "System.out.println(1);")));
assertTrue(source.matches(PatternMaker.make("[ 39: 0]", "case A:")));
assertTrue(source.matches(PatternMaker.make("[ 40: 0]", "case B:")));
assertTrue(source.matches(PatternMaker.make("[ 41: 41]", "System.out.println(\"A or B\");")));

assertTrue(source.matches(PatternMaker.make("[ 56: 56]", "switch (str)")));
assertTrue(source.matches(PatternMaker.make("[ 57: 0]", "case \"One\":")));
assertTrue(source.matches(PatternMaker.make("[ 58: 58]", "System.out.println(1);")));

assertTrue(source.matches(PatternMaker.make("[ 78: 0]", "case \"One\":")));
assertTrue(source.matches(PatternMaker.make("[ 79: 0]", "case \"POe\":")));
assertTrue(source.matches(PatternMaker.make("[ 80: 80]", "System.out.println(\"'One' or 'POe'\");")));

assertTrue(source.indexOf("// Byte code:") == -1);
assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1);
Expand Down
37 changes: 37 additions & 0 deletions src/test/resources/java/org/jd/core/test/AdvancedSwitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ public void switchEnum(TestEnum te) {
System.out.println("end");
}

public void switchEnumBis(TestEnum te) {
System.out.println("start");

switch (te) {
case A:
case B:
System.out.println("A or B");
break;
case C:
System.out.println("C");
break;
default:
System.out.println("default");
}

System.out.println("end");
}

public void switchString(String str) {
System.out.println("start");

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

System.out.println("end");
}

public void switchStringBis(String str) {
System.out.println("start");

switch (str) {
case "One":
case "POe":
System.out.println("'One' or 'POe'");
break;
case "Two":
System.out.println(2);
break;
default:
System.out.println("?");
break;
}

System.out.println("end");
}
}
Binary file modified src/test/resources/zip/data-java-jdk-1.7.0-no-debug-info.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.7.0.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.8.0.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-10.0.2.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-9.0.1.zip
Binary file not shown.

0 comments on commit cefe750

Please sign in to comment.