Commit d0f120c3 authored by Skylot's avatar Skylot

core: fix string concatenation

parent 54f4c6d2
...@@ -303,14 +303,19 @@ public class InsnGen { ...@@ -303,14 +303,19 @@ public class InsnGen {
break; break;
case STR_CONCAT: case STR_CONCAT:
// TODO: wrap in braces only if necessary StringBuilder sb = new StringBuilder();
code.add('(');
for (Iterator<InsnArg> it = insn.getArguments().iterator(); it.hasNext(); ) { for (Iterator<InsnArg> it = insn.getArguments().iterator(); it.hasNext(); ) {
code.add(arg(it.next())); sb.append(arg(it.next()));
if (it.hasNext()) if (it.hasNext()) {
code.add(" + "); sb.append(" + ");
}
}
// TODO: wrap in braces only if necessary
if (state.contains(InsnGenState.BODY_ONLY)) {
code.add('(').add(sb.toString()).add(')');
} else {
code.add(sb.toString());
} }
code.add(')');
break; break;
case MONITOR_ENTER: case MONITOR_ENTER:
......
package jadx.core.dex.instructions.args; package jadx.core.dex.instructions.args;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.utils.InsnUtils; import jadx.core.utils.InsnUtils;
...@@ -57,12 +58,17 @@ public abstract class InsnArg extends Typed { ...@@ -57,12 +58,17 @@ public abstract class InsnArg extends Typed {
this.parentInsn = parentInsn; this.parentInsn = parentInsn;
} }
public InsnWrapArg wrapInstruction(InsnNode insn) { public InsnArg wrapInstruction(InsnNode insn) {
assert parentInsn != insn : "Can't wrap instruction info itself"; assert parentInsn != insn : "Can't wrap instruction info itself";
int count = parentInsn.getArgsCount(); int count = parentInsn.getArgsCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (parentInsn.getArg(i) == this) { if (parentInsn.getArg(i) == this) {
InsnWrapArg arg = wrap(insn); InsnArg arg;
if (insn.getType() == InsnType.MOVE) {
arg = insn.getArg(0);
} else {
arg = wrap(insn);
}
parentInsn.setArg(i, arg); parentInsn.setArg(i, arg);
return arg; return arg;
} }
......
...@@ -36,7 +36,7 @@ public class CodeShrinker extends AbstractVisitor { ...@@ -36,7 +36,7 @@ public class CodeShrinker extends AbstractVisitor {
return; return;
shrink(mth); shrink(mth);
pretify(mth); prettify(mth);
} }
private static void shrink(MethodNode mth) { private static void shrink(MethodNode mth) {
...@@ -72,7 +72,7 @@ public class CodeShrinker extends AbstractVisitor { ...@@ -72,7 +72,7 @@ public class CodeShrinker extends AbstractVisitor {
BlockNode useBlock = BlockUtils.getBlockByInsn(mth, useInsn); BlockNode useBlock = BlockUtils.getBlockByInsn(mth, useInsn);
if (useBlock != null if (useBlock != null
&& (useBlock.getPredecessors().contains(block) && (useBlock.getPredecessors().contains(block)
|| insn.getType() == InsnType.MOVE_EXCEPTION)) { || insn.getType() == InsnType.MOVE_EXCEPTION)) {
wrap = true; wrap = true;
} }
} }
...@@ -81,8 +81,8 @@ public class CodeShrinker extends AbstractVisitor { ...@@ -81,8 +81,8 @@ public class CodeShrinker extends AbstractVisitor {
// // TODO // // TODO
// // remover.add(useInsn); // // remover.add(useInsn);
// } else { // } else {
useInsnArg.wrapInstruction(insn); useInsnArg.wrapInstruction(insn);
remover.add(insn); remover.add(insn);
// } // }
} }
} }
...@@ -93,14 +93,14 @@ public class CodeShrinker extends AbstractVisitor { ...@@ -93,14 +93,14 @@ public class CodeShrinker extends AbstractVisitor {
} }
} }
private static void pretify(MethodNode mth) { private static void prettify(MethodNode mth) {
for (BlockNode block : mth.getBasicBlocks()) { for (BlockNode block : mth.getBasicBlocks()) {
for (int i = 0; i < block.getInstructions().size(); i++) { List<InsnNode> list = block.getInstructions();
InsnNode insn = block.getInstructions().get(i); for (int i = 0; i < list.size(); i++) {
InsnNode modInsn = pretifyInsn(mth, list.get(i));
InsnNode ni = pretifyInsn(mth, insn); if (modInsn != null) {
if (ni != null) list.set(i, modInsn);
block.getInstructions().set(i, ni); }
} }
} }
} }
...@@ -166,19 +166,23 @@ public class CodeShrinker extends AbstractVisitor { ...@@ -166,19 +166,23 @@ public class CodeShrinker extends AbstractVisitor {
if (callMth.getDeclClass().getFullName().equals(Consts.CLASS_STRING_BUILDER) if (callMth.getDeclClass().getFullName().equals(Consts.CLASS_STRING_BUILDER)
&& callMth.getShortId().equals("toString()") && callMth.getShortId().equals("toString()")
&& insn.getArg(0).isInsnWrap()) { && insn.getArg(0).isInsnWrap()) {
List<InsnNode> chain = flattenInsnChain(insn); try {
if (chain.size() > 1 && chain.get(0).getType() == InsnType.CONSTRUCTOR) { List<InsnNode> chain = flattenInsnChain(insn);
ConstructorInsn constr = (ConstructorInsn) chain.get(0); if (chain.size() > 1 && chain.get(0).getType() == InsnType.CONSTRUCTOR) {
if (constr.getClassType().getFullName().equals(Consts.CLASS_STRING_BUILDER) ConstructorInsn constr = (ConstructorInsn) chain.get(0);
&& constr.getArgsCount() == 0) { if (constr.getClassType().getFullName().equals(Consts.CLASS_STRING_BUILDER)
int len = chain.size(); && constr.getArgsCount() == 0) {
InsnNode concatInsn = new InsnNode(InsnType.STR_CONCAT, len - 1); int len = chain.size();
for (int i = 1; i < len; i++) { InsnNode concatInsn = new InsnNode(InsnType.STR_CONCAT, len - 1);
concatInsn.addArg(chain.get(i).getArg(1)); for (int i = 1; i < len; i++) {
concatInsn.addArg(chain.get(i).getArg(1));
}
concatInsn.setResult(insn.getResult());
return concatInsn;
} }
concatInsn.setResult(insn.getResult());
return concatInsn;
} }
} catch (Throwable e) {
LOG.debug("Can't convert string concatenation: {} insn: {}", mth, insn, e);
} }
} }
break; break;
......
...@@ -18,6 +18,10 @@ sourceSets { ...@@ -18,6 +18,10 @@ sourceSets {
} }
} }
compileJava {
options.compilerArgs << '-g:none'
}
task samplesRun(type: JavaExec, dependsOn: compileJava) { task samplesRun(type: JavaExec, dependsOn: compileJava) {
classpath = sourceSets.main.output classpath = sourceSets.main.output
main = mainSamplesClass main = mainSamplesClass
......
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