【发布时间】:2019-08-16 20:41:13
【问题描述】:
我正在尝试:
1) 迭代指令并找到所有相关节点
2) 在找到的节点前插入自定义代码
我使用流和迭代器来制作和插入,这仅适用于第一个节点
InsnList instructions = methodNode.instructions;
InsnList addition = ...
//It work: found n nodes for n return instructions
Stream<AbstractInsnNode> returnNodes =
Stream.iterate(instructions.getFirst(), AbstractInsnNode::getNext).limit(instructions.size())
.filter(n -> returnOpcodes.contains(n.getOpcode()));
//It not work: inserted only before first node
returnNodes.forEach(n -> instructions.insertBefore(n, addition));
我也试过迭代器,它也不起作用
ListIterator<AbstractInsnNode> iterator = instructions.iterator();
while (iterator.hasNext()) {
AbstractInsnNode node = iterator.next();
if (returnOpcodes.contains(node.getOpcode()))
instructions.insertBefore(node, addition);
}
我原以为addition 会插入到所有返回节点之前,但它插入到第一个之前。
InsnList 是一个链表,这样的插入必须有效。 我哪里错了?
【问题讨论】:
-
我做了一些实验。结果:插入单个节点(无列表)正常工作,但插入列表(有任意数量的节点)不起作用
-
哈,insertBefore 插入后从参数中清除列表。为了解决这个问题需要复制添加列表
标签: java java-bytecode-asm jvm-bytecode