From f3098741c3fb1f8eda352a4b62ed0d76151b919a Mon Sep 17 00:00:00 2001 From: Skylot Date: Wed, 8 Jan 2020 14:25:27 +0000 Subject: [PATCH] test: switch with fallthrough cases (#826) --- .../core/dex/instructions/SwitchNode.java | 15 +++--- .../switches/TestSwitchFallThrough.java | 54 +++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/integration/switches/TestSwitchFallThrough.java diff --git a/jadx-core/src/main/java/jadx/core/dex/instructions/SwitchNode.java b/jadx-core/src/main/java/jadx/core/dex/instructions/SwitchNode.java index fc6249d2f..641b45ac0 100644 --- a/jadx-core/src/main/java/jadx/core/dex/instructions/SwitchNode.java +++ b/jadx-core/src/main/java/jadx/core/dex/instructions/SwitchNode.java @@ -3,6 +3,7 @@ package jadx.core.dex.instructions; import java.util.Arrays; import java.util.List; +import jadx.core.codegen.CodeWriter; import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.InsnNode; @@ -110,15 +111,13 @@ public class SwitchNode extends TargetInsnNode { @Override public String toString() { - StringBuilder targ = new StringBuilder(); - targ.append('['); + StringBuilder sb = new StringBuilder(); + sb.append(super.toString()); for (int i = 0; i < targets.length; i++) { - targ.append(InsnUtils.formatOffset(targets[i])); - if (i < targets.length - 1) { - targ.append(", "); - } + sb.append(" case ").append(keys[i]) + .append(": goto ").append(InsnUtils.formatOffset(targets[i])); + sb.append(CodeWriter.NL); } - targ.append(']'); - return super.toString() + " k:" + Arrays.toString(keys) + " t:" + targ; + return sb.toString(); } } diff --git a/jadx-core/src/test/java/jadx/tests/integration/switches/TestSwitchFallThrough.java b/jadx-core/src/test/java/jadx/tests/integration/switches/TestSwitchFallThrough.java new file mode 100644 index 000000000..7e2a0fe9d --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/integration/switches/TestSwitchFallThrough.java @@ -0,0 +1,54 @@ +package jadx.tests.integration.switches; + +import org.junit.jupiter.api.Test; + +import jadx.NotYetImplemented; +import jadx.tests.api.IntegrationTest; + +import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; + +public class TestSwitchFallThrough extends IntegrationTest { + + public static class TestCls { + public int r; + + public void test(int a) { + int i = 10; + switch (a) { + case 1: + i = 1000; + // fallthrough + case 2: + r = i; + break; + + default: + r = -1; + break; + } + r *= 2; + System.out.println("in: " + a + ", out: " + r); + } + + public int testWrap(int a) { + r = 0; + test(a); + return r; + } + + public void check() { + assertThat(testWrap(1)).isEqualTo(2000); + assertThat(testWrap(2)).isEqualTo(20); + assertThat(testWrap(0)).isEqualTo(-2); + } + } + + @NotYetImplemented("switch fallthrough") + @Test + public void test() { + assertThat(getClassNode(TestCls.class)) + .code() + .containsOnlyOnce("switch"); + // code correctness checks done in 'check' method + } +}