core: improve out block detection in switch (issue #38)

This commit is contained in:
Skylot
2014-12-27 23:16:30 +03:00
parent 90fb95e785
commit 1d5368f5a2
6 changed files with 221 additions and 32 deletions
@@ -0,0 +1,51 @@
package jadx.tests.integration.switches;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import org.junit.Test;
import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class TestSwitchBreak extends IntegrationTest {
public static class TestCls {
public String test(int a) {
String s = "";
loop:
while (a > 0) {
switch (a % 4) {
case 1:
s += "1";
break;
case 3:
case 4:
s += "4";
break;
case 5:
s += "+";
break loop;
}
s += "-";
a--;
}
return s;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("switch (a % 4) {"));
assertEquals(4, count(code, "case "));
assertEquals(3, count(code, "break;"));
// TODO finish break with label from switch
assertThat(code, containsOne("return s + \"+\";"));
}
}
@@ -0,0 +1,50 @@
package jadx.tests.integration.switches;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import org.junit.Test;
import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class TestSwitchContinue extends IntegrationTest {
public static class TestCls {
public String test(int a) {
String s = "";
while (a > 0) {
switch (a % 4) {
case 1:
s += "1";
break;
case 3:
case 4:
s += "4";
break;
case 5:
a -= 2;
continue;
}
s += "-";
a--;
}
return s;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("switch (a % 4) {"));
assertEquals(4, count(code, "case "));
assertEquals(2, count(code, "break;"));
assertThat(code, containsOne("a -= 2;"));
assertThat(code, containsOne("continue;"));
}
}
@@ -0,0 +1,53 @@
package jadx.tests.integration.switches;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import org.junit.Test;
import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class TestSwitchReturnFromCase extends IntegrationTest {
public static class TestCls {
public void test(int a) {
String s = null;
if (a > 1000) {
return;
}
switch (a % 4) {
case 1:
s = "1";
break;
case 2:
s = "2";
break;
case 3:
case 4:
s = "4";
break;
case 5:
return;
}
s = "5";
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("switch (a % 4) {"));
assertEquals(5, count(code, "case "));
assertEquals(3, count(code, "break;"));
assertThat(code, containsOne("s = \"1\";"));
assertThat(code, containsOne("s = \"2\";"));
assertThat(code, containsOne("s = \"4\";"));
assertThat(code, containsOne("s = \"5\";"));
}
}