core: fix loop detection

This commit is contained in:
Skylot
2014-02-25 23:53:30 +04:00
parent 2b300341a0
commit 2cf28eb2e7
5 changed files with 117 additions and 4 deletions
@@ -1,4 +1,4 @@
package jadx.tests.internal;
package jadx.tests.internal.loops;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
@@ -0,0 +1,37 @@
package jadx.tests.internal.loops;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class TestLoopDetection extends InternalJadxTest {
public static class TestCls {
private void test(int[] a, int b) {
int i = 0;
while (i < a.length && i < b) {
a[i]++;
i++;
}
while (i < a.length) {
a[i]--;
i++;
}
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("while (i < a.length && i < b) {"));
assertThat(code, containsString("while (i < a.length) {"));
}
}
@@ -0,0 +1,51 @@
package jadx.tests.internal.loops;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class TestNestedLoops extends InternalJadxTest {
public static class TestCls {
private void test(List<String> l1, List<String> l2) {
Iterator<String> it1 = l1.iterator();
while (it1.hasNext()) {
String s1 = it1.next();
Iterator<String> it2 = l2.iterator();
while (it2.hasNext()) {
String s2 = it2.next();
if (s1.equals(s2)) {
if (s1.length() == 5) {
l2.add(s1);
} else {
l1.remove(s2);
}
}
}
}
if (l2.size() > 0) {
l1.clear();
}
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("while (it1.hasNext()) {"));
assertThat(code, containsString("while (it2.hasNext()) {"));
assertThat(code, containsString("if (s1.equals(s2)) {"));
assertThat(code, containsString("l2.add(s1);"));
}
}