An iterator for editing a code attribute.
To directly read or edit a bytecode sequence, call {@link #byteAt(int)}, {@link #s16bitAt(int)}, {@link #writeByte(int,int)}, {@link #write16bit(int,int)}, and other methods. For example, if method
refers to a CtMethod
object, the following code substitutes the NOP
instruction for the first instruction of the method:
CodeAttribute ca = method.getMethodInfo().getCodeAttribute(); CodeIterator ci = ca.iterator(); ci.writeByte(Opcode.NOP, 0);
To visit every instruction, call {@link #next()} on a CodeIterator
.It returns the index of the first byte of the next instruction.
If there are multiple CodeIterator
s referring to the same Code_attribute
, then inserting a gap by one CodeIterator
will break the other CodeIterator
.
This iterator does not provide remove()
. If a piece of code in a Code_attribute
is unnecessary, it should be overwritten with NOP
.
@see CodeAttribute#iterator()