提交 1358a05a 编写于 作者: S Skylot

core: omit redundant brackets in conditions

上级 1b0a8990
......@@ -214,7 +214,7 @@ public class InsnGen {
break;
case CHECK_CAST:
case CAST:
case CAST: {
boolean wrap = state.contains(IGState.BODY_ONLY);
if (wrap)
code.add("(");
......@@ -225,7 +225,7 @@ public class InsnGen {
if (wrap)
code.add(")");
break;
}
case ARITH:
makeArith((ArithNode) insn, code, state);
break;
......@@ -263,11 +263,18 @@ public class InsnGen {
code.add(String.format("(%1$s > %2$s ? 1 : (%1$s == %2$s ? 0 : -1))", arg(insn, 0), arg(insn, 1)));
break;
case INSTANCE_OF:
code.add('(').add(arg(insn, 0)).add(" instanceof ")
.add(useType((ArgType) ((IndexInsnNode) insn).getIndex())).add(')');
case INSTANCE_OF: {
boolean wrap = state.contains(IGState.BODY_ONLY);
if (wrap)
code.add("(");
code.add(arg(insn, 0));
code.add(" instanceof ");
code.add(useType((ArgType) ((IndexInsnNode) insn).getIndex()));
if (wrap) {
code.add(")");
}
break;
}
case CONSTRUCTOR:
makeConstructor((ConstructorInsn) insn, code, state);
break;
......
......@@ -5,10 +5,13 @@ import jadx.core.dex.attributes.AttributeType;
import jadx.core.dex.attributes.DeclareVariableAttr;
import jadx.core.dex.attributes.ForceReturnAttr;
import jadx.core.dex.attributes.IAttribute;
import jadx.core.dex.instructions.ArithNode;
import jadx.core.dex.instructions.IfOp;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.SwitchNode;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.nodes.IBlock;
......@@ -186,17 +189,38 @@ public class RegionGen extends InsnGen {
&& secondArg.isLiteral()
&& secondArg.getType().equals(ArgType.BOOLEAN)) {
LiteralArg lit = (LiteralArg) secondArg;
if (lit.getLiteral() == 0)
if (lit.getLiteral() == 0) {
op = op.invert();
}
if (op == IfOp.EQ) {
return arg(firstArg); // == true
return arg(firstArg, false); // == true
} else if (op == IfOp.NE) {
return "!" + arg(firstArg); // != true
}
LOG.warn(ErrorsCounter.formatErrorMsg(mth, "Unsupported boolean condition " + op.getSymbol()));
}
return arg(firstArg) + " " + op.getSymbol() + " " + arg(secondArg);
return arg(firstArg, isWrapNeeded(firstArg))
+ " " + op.getSymbol() + " "
+ arg(secondArg, isWrapNeeded(secondArg));
}
private boolean isWrapNeeded(InsnArg arg) {
if (!arg.isInsnWrap()) {
return false;
}
InsnNode insn = ((InsnWrapArg) arg).getWrapInsn();
if(insn.getType() == InsnType.ARITH) {
ArithNode arith = ((ArithNode) insn);
switch (arith.getOp()) {
case ADD:
case SUB:
case MUL:
case DIV:
case REM:
return false;
}
}
return true;
}
private CodeWriter makeSwitch(SwitchRegion sw, CodeWriter code) throws CodegenException {
......
package jadx.tests.internal;
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 TestRedundantBrackets extends InternalJadxTest {
public static class TestCls {
public boolean method(String str) {
return str.indexOf('a') != -1;
}
public int method2(Object obj) {
if (obj instanceof String) {
return ((String) obj).length();
}
return 0;
}
public int method3(int a, int b) {
if (a + b < 10) {
return a;
}
if ((a & b) != 0) {
return a * b;
}
return b;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
// assertThat(code, not(containsString("(-1)")));
assertThat(code, containsString("if (obj instanceof String)"));
assertThat(code, containsString("if (a + b < 10)"));
assertThat(code, containsString("if ((a & b) != 0)"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册