Commit 95e9da36 authored by Skylot's avatar Skylot

core: simplify conditions, omit redundant parenthesis

parent 9bf7270b
...@@ -309,7 +309,7 @@ public class InsnGen { ...@@ -309,7 +309,7 @@ public class InsnGen {
break; break;
case APUT: case APUT:
code.add(arg(insn, 0)).add('[').add(arg(insn, 1)).add("] = ").add(arg(insn, 2)); code.add(arg(insn, 0)).add('[').add(arg(insn.getArg(1), false)).add("] = ").add(arg(insn.getArg(2), false));
break; break;
case IGET: { case IGET: {
......
...@@ -167,7 +167,7 @@ public class RegionGen extends InsnGen { ...@@ -167,7 +167,7 @@ public class RegionGen extends InsnGen {
return "!" + makeCondition(condition.getArgs().get(0)); return "!" + makeCondition(condition.getArgs().get(0));
case AND: case AND:
case OR: case OR:
String mode = condition.getMode() == IfCondition.MODE.AND ? " && " : " || "; String mode = condition.getMode() == IfCondition.Mode.AND ? " && " : " || ";
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (IfCondition arg : condition.getArgs()) { for (IfCondition arg : condition.getArgs()) {
if (sb.length() != 0) { if (sb.length() != 0) {
......
...@@ -5,6 +5,7 @@ import jadx.core.dex.instructions.IfOp; ...@@ -5,6 +5,7 @@ import jadx.core.dex.instructions.IfOp;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -21,16 +22,18 @@ public final class IfCondition { ...@@ -21,16 +22,18 @@ public final class IfCondition {
return new IfCondition(new Compare(insn)); return new IfCondition(new Compare(insn));
} }
public static IfCondition not(IfCondition a) { public static IfCondition merge(Mode mode, IfCondition a, IfCondition b) {
return new IfCondition(MODE.NOT, Arrays.asList(a)); if (a.getMode() == mode) {
IfCondition n = new IfCondition(a);
n.addArg(b);
return n;
} else if (b.getMode() == mode) {
IfCondition n = new IfCondition(b);
n.addArg(a);
return n;
} else {
return new IfCondition(mode, Arrays.asList(a, b));
} }
public static IfCondition and(IfCondition a, IfCondition b) {
return new IfCondition(MODE.AND, Arrays.asList(a, b));
}
public static IfCondition or(IfCondition a, IfCondition b) {
return new IfCondition(MODE.OR, Arrays.asList(a, b));
} }
public static final class Compare { public static final class Compare {
...@@ -61,30 +64,36 @@ public final class IfCondition { ...@@ -61,30 +64,36 @@ public final class IfCondition {
} }
} }
public static enum MODE { public static enum Mode {
COMPARE, COMPARE,
NOT, NOT,
AND, AND,
OR OR
} }
private final MODE mode; private final Mode mode;
private final List<IfCondition> args; private final List<IfCondition> args;
private final Compare compare; private final Compare compare;
private IfCondition(Compare compare) { private IfCondition(Compare compare) {
this.mode = MODE.COMPARE; this.mode = Mode.COMPARE;
this.compare = compare; this.compare = compare;
this.args = null; this.args = null;
} }
private IfCondition(MODE mode, List<IfCondition> args) { private IfCondition(Mode mode, List<IfCondition> args) {
this.mode = mode; this.mode = mode;
this.args = args; this.args = args;
this.compare = null; this.compare = null;
} }
public MODE getMode() { private IfCondition(IfCondition c) {
this.mode = c.mode;
this.compare = c.compare;
this.args = new ArrayList<IfCondition>(c.args);
}
public Mode getMode() {
return mode; return mode;
} }
...@@ -92,8 +101,12 @@ public final class IfCondition { ...@@ -92,8 +101,12 @@ public final class IfCondition {
return args; return args;
} }
public void addArg(IfCondition c) {
args.add(c);
}
public boolean isCompare() { public boolean isCompare() {
return mode == MODE.COMPARE; return mode == Mode.COMPARE;
} }
public Compare getCompare() { public Compare getCompare() {
......
...@@ -40,6 +40,7 @@ import java.util.Set; ...@@ -40,6 +40,7 @@ import java.util.Set;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static jadx.core.dex.regions.IfCondition.Mode;
import static jadx.core.utils.BlockUtils.getBlockByOffset; import static jadx.core.utils.BlockUtils.getBlockByOffset;
import static jadx.core.utils.BlockUtils.isPathExists; import static jadx.core.utils.BlockUtils.isPathExists;
import static jadx.core.utils.BlockUtils.selectOther; import static jadx.core.utils.BlockUtils.selectOther;
...@@ -400,6 +401,7 @@ public class RegionMaker { ...@@ -400,6 +401,7 @@ public class RegionMaker {
IfCondition condition; IfCondition condition;
boolean inverted = false; boolean inverted = false;
IfCondition nestedCondition = IfCondition.fromIfNode(nestedIfInsn);
if (isPathExists(bElse, nestedIfBlock)) { if (isPathExists(bElse, nestedIfBlock)) {
// else branch // else branch
if (bThen != nbThen) { if (bThen != nbThen) {
...@@ -409,7 +411,7 @@ public class RegionMaker { ...@@ -409,7 +411,7 @@ public class RegionMaker {
nestedIfInsn.invertOp(nbElse.getStartOffset()); nestedIfInsn.invertOp(nbElse.getStartOffset());
inverted = true; inverted = true;
} }
condition = IfCondition.or(ifRegion.getCondition(), IfCondition.fromIfNode(nestedIfInsn)); condition = IfCondition.merge(Mode.OR, ifRegion.getCondition(), nestedCondition);
} else { } else {
// then branch // then branch
if (bElse != nbElse) { if (bElse != nbElse) {
...@@ -419,7 +421,7 @@ public class RegionMaker { ...@@ -419,7 +421,7 @@ public class RegionMaker {
nestedIfInsn.invertOp(nbElse.getStartOffset()); nestedIfInsn.invertOp(nbElse.getStartOffset());
inverted = true; inverted = true;
} }
condition = IfCondition.and(ifRegion.getCondition(), IfCondition.fromIfNode(nestedIfInsn)); condition = IfCondition.merge(Mode.AND, ifRegion.getCondition(), nestedCondition);
} }
ifRegion.setCondition(condition); ifRegion.setCondition(condition);
nestedIfBlock.getAttributes().add(AttributeFlag.SKIP); nestedIfBlock.getAttributes().add(AttributeFlag.SKIP);
......
...@@ -31,6 +31,17 @@ public class TestRedundantBrackets extends InternalJadxTest { ...@@ -31,6 +31,17 @@ public class TestRedundantBrackets extends InternalJadxTest {
} }
return b; return b;
} }
public void method4(int num) {
if (num == 4 || num == 6 || num == 8 || num == 10) {
method2(null);
}
}
public void method5(int a[], int n) {
a[1] = n * 2;
a[n - 1] = 1;
}
} }
@Test @Test
...@@ -42,5 +53,9 @@ public class TestRedundantBrackets extends InternalJadxTest { ...@@ -42,5 +53,9 @@ public class TestRedundantBrackets extends InternalJadxTest {
assertThat(code, containsString("if (obj instanceof String)")); assertThat(code, containsString("if (obj instanceof String)"));
assertThat(code, containsString("if (a + b < 10)")); assertThat(code, containsString("if (a + b < 10)"));
assertThat(code, containsString("if ((a & b) != 0)")); assertThat(code, containsString("if ((a & b) != 0)"));
assertThat(code, containsString("if (num == 4 || num == 6 || num == 8 || num == 10)"));
assertThat(code, containsString("a[1] = n * 2;"));
assertThat(code, containsString("a[n - 1] = 1;"));
} }
} }
...@@ -50,6 +50,16 @@ public class TestConditions extends AbstractTest { ...@@ -50,6 +50,16 @@ public class TestConditions extends AbstractTest {
test1(0); test1(0);
} }
public void test4(int num) {
if (num == 4 || num == 6 || num == 8 || num == 10) {
accept("a");
}
}
public boolean test5(int num) {
return num > 5 && (num < 10 || num == 7);
}
public boolean accept(String name) { public boolean accept(String name) {
return name.startsWith("Test") && name.endsWith(".class") && !name.contains("$"); return name.startsWith("Test") && name.endsWith(".class") && !name.contains("$");
} }
...@@ -71,6 +81,12 @@ public class TestConditions extends AbstractTest { ...@@ -71,6 +81,12 @@ public class TestConditions extends AbstractTest {
assertTrue(accept("Test.class")); assertTrue(accept("Test.class"));
test3(false, false); test3(false, false);
assertFalse(test5(4));
assertFalse(test5(11));
assertTrue(test5(6));
assertTrue(test5(7));
assertTrue(test5(8));
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment