From 30fbf4bcfa7ae931101c5f848aefb94246d2c39f Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Wed, 24 Apr 2019 18:33:05 +0200 Subject: [PATCH] refactor: better place for removing parenthesis (PR #627) --- .../java/jadx/core/dex/instructions/ArithOp.java | 7 ------- .../jadx/core/dex/instructions/args/InsnArg.java | 8 -------- .../core/dex/visitors/PrepareForCodeGen.java | 16 +++++++++------- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/jadx-core/src/main/java/jadx/core/dex/instructions/ArithOp.java b/jadx-core/src/main/java/jadx/core/dex/instructions/ArithOp.java index e7ec1f135..7e4ce9648 100644 --- a/jadx-core/src/main/java/jadx/core/dex/instructions/ArithOp.java +++ b/jadx-core/src/main/java/jadx/core/dex/instructions/ArithOp.java @@ -24,11 +24,4 @@ public enum ArithOp { public String getSymbol() { return this.symbol; } - - public boolean noWrapWith(ArithOp other) { - return (this == ADD && other == ADD) - || (this == MUL && other == MUL) - || (this == AND && other == AND) - || (this == OR && other == OR); - } } diff --git a/jadx-core/src/main/java/jadx/core/dex/instructions/args/InsnArg.java b/jadx-core/src/main/java/jadx/core/dex/instructions/args/InsnArg.java index 59090d28b..7daa6926e 100644 --- a/jadx-core/src/main/java/jadx/core/dex/instructions/args/InsnArg.java +++ b/jadx-core/src/main/java/jadx/core/dex/instructions/args/InsnArg.java @@ -9,8 +9,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jadx.core.dex.attributes.AFlag; -import jadx.core.dex.instructions.ArithNode; -import jadx.core.dex.instructions.InsnType; import jadx.core.dex.nodes.InsnNode; import jadx.core.utils.InsnUtils; @@ -113,12 +111,6 @@ public abstract class InsnArg extends Typed { insn.add(AFlag.WRAPPED); InsnArg arg = wrapArg(insn); parent.setArg(i, arg); - - if (insn.getType() == InsnType.ARITH && parent.getType() == InsnType.ARITH - && ((ArithNode) insn).getOp().noWrapWith(((ArithNode) parent).getOp())) { - insn.add(AFlag.DONT_WRAP); - } - return arg; } diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java b/jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java index 9d1cc4ea9..02b7b9d9f 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java @@ -42,7 +42,7 @@ public class PrepareForCodeGen extends AbstractVisitor { } removeInstructions(block); checkInline(block); -// removeParenthesis(block); + removeParenthesis(block); modifyArith(block); } } @@ -99,7 +99,7 @@ public class PrepareForCodeGen extends AbstractVisitor { private static void removeParenthesis(BlockNode block) { for (InsnNode insn : block.getInstructions()) { - checkInsn(insn); + removeParenthesis(insn); } } @@ -107,17 +107,19 @@ public class PrepareForCodeGen extends AbstractVisitor { * Remove parenthesis for wrapped insn in arith '+' or '-' * ('(a + b) +c' => 'a + b + c') */ - private static void checkInsn(InsnNode insn) { + private static void removeParenthesis(InsnNode insn) { if (insn.getType() == InsnType.ARITH) { ArithNode arith = (ArithNode) insn; ArithOp op = arith.getOp(); - if (op == ArithOp.ADD || op == ArithOp.SUB) { + if (op == ArithOp.ADD || op == ArithOp.MUL || op == ArithOp.AND || op == ArithOp.OR) { for (int i = 0; i < 2; i++) { InsnArg arg = arith.getArg(i); if (arg.isInsnWrap()) { InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn(); - wrapInsn.add(AFlag.DONT_WRAP); - checkInsn(wrapInsn); + if (wrapInsn.getType() == InsnType.ARITH && ((ArithNode) wrapInsn).getOp() == op) { + wrapInsn.add(AFlag.DONT_WRAP); + } + removeParenthesis(wrapInsn); } } } @@ -125,7 +127,7 @@ public class PrepareForCodeGen extends AbstractVisitor { for (InsnArg arg : insn.getArguments()) { if (arg.isInsnWrap()) { InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn(); - checkInsn(wrapInsn); + removeParenthesis(wrapInsn); } } }