提交 69494c92 编写于 作者: S Skylot

core: add method for copy instruction nodes

上级 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());
}
}
......@@ -16,6 +16,11 @@ public final class ConstClassNode extends InsnNode {
return clsType;
}
@Override
public InsnNode copy() {
return copyCommonParams(new ConstClassNode(clsType));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
......
......@@ -15,6 +15,11 @@ public final class ConstStringNode extends InsnNode {
return str;
}
@Override
public InsnNode copy() {
return copyCommonParams(new ConstStringNode(str));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
......
......@@ -16,6 +16,11 @@ public class IndexInsnNode extends InsnNode {
return index;
}
@Override
public IndexInsnNode copy() {
return copyCommonParams(new IndexInsnNode(insnType, index, getArgsCount()));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
......
......@@ -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;
}
......@@ -44,6 +50,11 @@ public class InvokeNode extends InsnNode {
return mth;
}
@Override
public InsnNode copy() {
return copyCommonParams(new InvokeNode(mth, type, getArgsCount()));
}
@Override
public boolean isSame(InsnNode obj) {
if (this == obj) {
......
......@@ -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% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册