java – 使用ASM选择和修改`if`语句
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用ASM选择和修改`if`语句,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4306字,纯文字阅读大概需要7分钟。
内容图文
![java – 使用ASM选择和修改`if`语句](/upload/InfoBanner/zyjiaocheng/810/d148e5001ae349c7899a59feecb5e3a8.jpg)
我想在特定行上更新现有类中的if语句而不更改整个方法.
这是目标代码(类,方法和一些代码的名称已更改,因为它们不相关):
public class Target extends Something {
public Target(){
super();
//some code...
}
public Result targetMethod(Data firstPar, Data secondPar){
if(someStatement()) {
return Result.FAIL;
} else {
if(firstPar.check()){ //here is the line I want to change
firstPar.doSomething()
}
secondPar.doSomething();
return Result.SUCCESS;
}
}
}
在这段代码中,我想将if(firstPar.check())改为:
if(firstPar.check() && !Utilities.someOtherCheck(firstPar, secondPar))
这是我试图解决这个问题的方法:
ClassNode node = new ClassNode();
ClassReader reader = new ClassReader(bytes); //bytes - original target class
reader.accept(node, 0);
ClassWriter cw = new ClassWriter(0);
node.accept(cw);
MethodVisitor visitor = cw.visitMethod(ACC_PUBLIC, "targetMethod", "<it's signature>", null, null);
visitor.visitCode();
//No idea what to do next
我不明白的事情:
>如何在MethodVisitor中正确选择一行?
>如何仅更改if语句的一半?
>如何获取新类的字节码(将生成该字节码)
我们改变目标类后)?
如果您能提供某种示例代码,那将是很好的.
谢谢!
解决方法:
问题1可能是最困难的.您需要通过识别某些模式来找出插入指令的位置.如果您假设firstPar.check()只被调用一次,那么您可以查找if(firstPar.check())的以下字节码指令:
ALOAD 1
INVOKEVIRTUAL Data.check ()Z
IFEQ L3
其中L3是跳转标签,如果检查返回false.
对于问题2,请注意if(firstPar.check()&&!Utilities.someOtherCheck(firstPar,secondPar))的字节码指令是:
ALOAD 1
INVOKEVIRTUAL Data.check ()Z
IFEQ L3
ALOAD 1
ALOAD 2
INVOKESTATIC Utilities.someOtherCheck (LData;LData;)Z
IFNE L3
因此,您需要在IFEQ L3之后插入4条新指令.
您可以使用Tree API执行此操作,您可以通过继承ClassVisitor和MethodNode为targetMethod创建适配器:
private static class ClassAdapter extends ClassVisitor {
public ClassAdapter(ClassVisitor cv) {
super(Opcodes.ASM5, cv);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
if (name.equals("targetMethod"))
return new MethodAdapter(access, name, desc, signature, exceptions, mv);
else
return mv;
}
}
private static class MethodAdapter extends MethodNode {
public MethodAdapter(int access, String name, String desc,
String signature, String[] exceptions, MethodVisitor mv) {
super(Opcodes.ASM5, access, name, desc, signature, exceptions);
this.mv = mv;
}
// More to come ...
}
在MethodAdapter中,您可以覆盖visitEnd以迭代方法内的所有指令,并尝试检测上述3条指令并在其后插入4条新指令:
@Override
public void visitEnd() {
// Iterates all instructions in the method
ListIterator<AbstractInsnNode> itr = instructions.iterator();
while (itr.hasNext()) {
// Checks whether the instruction is ALOAD 1
AbstractInsnNode node = itr.next();
if (node.getOpcode() != Opcodes.ALOAD
|| ((VarInsnNode) node).var != 1)
continue;
// Checks whether the next instruction is INVOKEVIRTUAL
if (node.getNext() == null
|| node.getNext().getOpcode() != Opcodes.INVOKEVIRTUAL)
continue;
// Checks the invoked method name and signature
MethodInsnNode next = (MethodInsnNode) node.getNext();
if (!next.owner.equals("Data")
|| !next.name.equals("check")
|| !next.desc.equals("()Z"))
continue;
// Checks whether the next of the next instruction is IFEQ
AbstractInsnNode next2 = next.getNext();
if (next2 == null
|| next2.getOpcode() != Opcodes.IFEQ)
continue;
// Creates a list instructions to be inserted
InsnList list = new InsnList();
list.add(new VarInsnNode(Opcodes.ALOAD, 1));
list.add(new VarInsnNode(Opcodes.ALOAD, 2));
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
"Utilities", "someOtherCheck",
"(LData;LData;)Z", false));
list.add(new JumpInsnNode(Opcodes.IFNE, ((JumpInsnNode) next2).label));
// Inserts the list, updates maxStack to at least 2, and we are done
instructions.insert(next2, list);
maxStack = Math.max(2, maxStack);
break;
}
accept(mv);
}
要使用适配器,可以使用ClassReader和ClassWriter对其进行链接.下面,我还链接一个TraceClassVisitor来打印tmp目录中的日志文件:
ClassReader reader = new ClassReader("Target");
ClassWriter writer = new ClassWriter(reader, 0);
TraceClassVisitor printer = new TraceClassVisitor(writer,
new PrintWriter(System.getProperty("java.io.tmpdir") + File.separator + "Target.log"));
ClassAdapter adapter = new ClassAdapter(printer);
reader.accept(adapter, 0);
byte[] b = writer.toByteArray(); // The modified bytecode
内容总结
以上是互联网集市为您收集整理的java – 使用ASM选择和修改`if`语句全部内容,希望文章能够帮你解决java – 使用ASM选择和修改`if`语句所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。