core: redone 'if' structure checking
This commit is contained in:
@@ -50,26 +50,16 @@ public class IfMakerHelper {
|
||||
boolean badThen = !allPathsFromIf(thenBlock, info);
|
||||
boolean badElse = !allPathsFromIf(elseBlock, info);
|
||||
if (badThen && badElse) {
|
||||
LOG.debug("Stop processing blocks after 'if': {}, method: {}", info, mth);
|
||||
return null;
|
||||
}
|
||||
if (badThen || badElse) {
|
||||
if (badElse && isPathExists(thenBlock, elseBlock)) {
|
||||
info = new IfInfo(info.getCondition(), thenBlock, null);
|
||||
info.setOutBlock(elseBlock);
|
||||
} else if (badThen && isPathExists(elseBlock, thenBlock)) {
|
||||
info = IfInfo.invert(info);
|
||||
info = new IfInfo(info.getCondition(), info.getThenBlock(), null);
|
||||
info.setOutBlock(thenBlock);
|
||||
} else if (badElse) {
|
||||
info = new IfInfo(info.getCondition(), thenBlock, null);
|
||||
info.setOutBlock(null);
|
||||
LOG.debug("Stop processing blocks after bad 'else' in 'if': {}, method: {}", info, mth);
|
||||
} else {
|
||||
info = IfInfo.invert(info);
|
||||
info = new IfInfo(info.getCondition(), info.getThenBlock(), null);
|
||||
info.setOutBlock(null);
|
||||
LOG.debug("Stop processing blocks after bad 'then' in 'if': {}, method: {}", info, mth);
|
||||
}
|
||||
if (badElse) {
|
||||
info = new IfInfo(info.getCondition(), thenBlock, null);
|
||||
info.setOutBlock(elseBlock);
|
||||
} else if (badThen) {
|
||||
info = IfInfo.invert(info);
|
||||
info = new IfInfo(info.getCondition(), elseBlock, null);
|
||||
info.setOutBlock(thenBlock);
|
||||
} else {
|
||||
List<BlockNode> thenSC = thenBlock.getCleanSuccessors();
|
||||
List<BlockNode> elseSC = elseBlock.getCleanSuccessors();
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package jadx.tests.internal.others;
|
||||
|
||||
import jadx.api.InternalJadxTest;
|
||||
import jadx.core.dex.nodes.ClassNode;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static jadx.tests.utils.JadxMatchers.containsOne;
|
||||
import static jadx.tests.utils.JadxMatchers.countString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class TestIfInTry extends InternalJadxTest {
|
||||
|
||||
public static class TestCls {
|
||||
private File dir;
|
||||
|
||||
public int test() {
|
||||
try {
|
||||
int a = f();
|
||||
if (a != 0) {
|
||||
return a;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// skip
|
||||
}
|
||||
try {
|
||||
f();
|
||||
return 1;
|
||||
} catch (IOException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private int f() throws IOException {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
setOutputCFG();
|
||||
ClassNode cls = getClassNode(TestCls.class);
|
||||
String code = cls.getCode().toString();
|
||||
System.out.println(code);
|
||||
|
||||
assertThat(code, containsOne("if (a != 0) {"));
|
||||
assertThat(code, containsOne("} catch (Exception e) {"));
|
||||
assertThat(code, countString(2, "try {"));
|
||||
assertThat(code, countString(3, "f()"));
|
||||
assertThat(code, containsOne("return 1;"));
|
||||
assertThat(code, containsOne("} catch (IOException e"));
|
||||
assertThat(code, containsOne("return -1;"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user