Commit 69494c92 authored by Skylot's avatar Skylot

core: add method for copy instruction nodes

parent b2f0f025
......@@ -9,6 +9,7 @@ dependencies {
compile 'commons-io:commons-io:2.4'
compile 'org.ow2.asm:asm:5.0.3'
compile 'com.intellij:annotations:12.0'
compile 'uk.com.robust-it:cloning:1.9.2'
testCompile 'org.smali:smali:2.0.3'
}
......
......@@ -23,4 +23,9 @@ public abstract class LineAttrNode extends AttrNode {
public void setDecompiledLine(int decompiledLine) {
this.decompiledLine = decompiledLine;
}
public void copyLines(LineAttrNode lineAttrNode) {
setSourceLine(lineAttrNode.getSourceLine());
setDecompiledLine(lineAttrNode.getDecompiledLine());
}
}
......@@ -17,6 +17,11 @@ public final class ConstClassNode extends InsnNode {
}
@Override
public InsnNode copy() {
return copyCommonParams(new ConstClassNode(clsType));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
return true;
......
......@@ -16,6 +16,11 @@ public final class ConstStringNode extends InsnNode {
}
@Override
public InsnNode copy() {
return copyCommonParams(new ConstStringNode(str));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
return true;
......
......@@ -17,6 +17,11 @@ public class IndexInsnNode extends InsnNode {
}
@Override
public IndexInsnNode copy() {
return copyCommonParams(new IndexInsnNode(insnType, index, getArgsCount()));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
return true;
......
......@@ -36,6 +36,12 @@ public class InvokeNode extends InsnNode {
}
}
private InvokeNode(MethodInfo mth, InvokeType invokeType, int argsCount) {
super(InsnType.INVOKE, argsCount);
this.mth = mth;
this.type = invokeType;
}
public InvokeType getInvokeType() {
return type;
}
......@@ -45,6 +51,11 @@ public class InvokeNode extends InsnNode {
}
@Override
public InsnNode copy() {
return copyCommonParams(new InvokeNode(mth, type, getArgsCount()));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
return true;
......
......@@ -5,7 +5,10 @@ import jadx.core.dex.instructions.InsnType;
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.NamedArg;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.instructions.args.SSAVar;
import jadx.core.utils.InsnUtils;
import jadx.core.utils.Utils;
......@@ -14,9 +17,17 @@ import java.util.Collections;
import java.util.List;
import com.android.dx.io.instructions.DecodedInstruction;
import com.rits.cloning.Cloner;
public class InsnNode extends LineAttrNode {
private static final Cloner INSN_CLONER = new Cloner();
static {
INSN_CLONER.dontClone(ArgType.class, SSAVar.class, LiteralArg.class, NamedArg.class);
INSN_CLONER.dontCloneInstanceOf(RegisterArg.class);
}
protected final InsnType insnType;
private RegisterArg result;
......@@ -228,4 +239,31 @@ public class InsnNode extends LineAttrNode {
&& arguments.size() == other.arguments.size();
}
protected <T extends InsnNode> T copyCommonParams(T copy) {
copy.setResult(result);
if (copy.getArgsCount() == 0) {
for (InsnArg arg : this.getArguments()) {
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
copy.addArg(InsnArg.wrapArg(wrapInsn.copy()));
} else {
copy.addArg(arg);
}
}
}
copy.copyAttributesFrom(this);
copy.copyLines(this);
copy.setOffset(this.getOffset());
return copy;
}
/**
* Make copy of InsnNode object.
*/
public InsnNode copy() {
if (this.getClass() == InsnNode.class) {
return copyCommonParams(new InsnNode(insnType, getArgsCount()));
}
return INSN_CLONER.deepClone(this);
}
}
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