ListIterator交换当前和下一个

时间:2016-04-12 03:03:24

标签: java swap listiterator

我需要在ListIterator“nodeListIterator”中交换node和node.getNext()并且我已经尝试了几个小时

 if (node instanceof LdcInsnNode && (node.getNext() instanceof FieldInsnNode && node.getNext().getOpcode() == Opcodes.GETSTATIC)
        && node.getNext().getNext().getOpcode() == Opcodes.IMUL) {
        // code
 }

谢谢你。

1 个答案:

答案 0 :(得分:0)

我想出来了,它的hacky但是它有效

final ListIterator<AbstractInsnNode> nodeListIterator = methodNode.instructions.iterator();
while (nodeListIterator.hasNext()) {
    AbstractInsnNode node = nodeListIterator.next();
    if (node instanceof LdcInsnNode && (node.getNext() instanceof FieldInsnNode && node.getNext().getOpcode() == Opcodes.GETSTATIC)
            && node.getNext().getNext().getOpcode() == Opcodes.IMUL) {
        AbstractInsnNode temp1 = node;
        nodeListIterator.remove();
        node = nodeListIterator.next();
        AbstractInsnNode temp2 = node;
        nodeListIterator.remove();
        nodeListIterator.next();
        nodeListIterator.previous();
        nodeListIterator.add(temp1);
        nodeListIterator.previous();
        nodeListIterator.add(temp2);
        nodeListIterator.previous();
    }
}