InsnList nodes are removed after "accept"
I noticed this issue recently when trying out ASM 7.0 (snapshot) and I believe I have been able to track it down to the following recent change:
Here is a simple test I've set up that reproduces the issue that I'm seeing:
@Test
public void testAcceptWithLabelInfo() {
MethodNode method = new MethodNode();
method.instructions = new InsnList();
LabelNode labelNode = new LabelNode(new Label());
labelNode.getLabel().info = labelNode;
method.instructions.add(labelNode);
method.instructions.add(new InsnNode(11));
method.instructions.add(new InsnNode(22));
method.instructions.add(new InsnNode(33));
MethodNode otherMethod = new MethodNode();
final InsnList insnList = new InsnList();
method.accept(new MethodVisitor(Opcodes.ASM7, otherMethod) {
@Override
public void visitInsn(int opcode) {
insnList.add(new InsnNode(opcode));
super.visitInsn(opcode);
}
});
assertEquals(3, insnList.size()); // This will fail
assertEquals(11, insnList.get(0).opcode);
assertEquals(22, insnList.get(1).opcode);
assertEquals(33, insnList.get(2).opcode);
}
It appears that the change to null out the nextInsn
field:
can cause the iteration of this loop to stop before it gets through all of the instruction nodes:
It's entirely possible that I may be doing something wrong here so please let me know if that's the case.
In my usage, I tried not using the info
field on the Label
and that allowed the instructions to be iterated through but caused a failure when attempting to compute frames later on:
java.lang.NullPointerException
at org.objectweb.asm.MethodWriter.computeAllFrames(MethodWriter.java:1581)
at org.objectweb.asm.MethodWriter.visitMaxs(MethodWriter.java:1555)
at org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:806)
Any help would be greatly appreciated. Thank you!