Top Description Inners Fields Constructors Methods
javassist.bytecode

public Class CodeIterator

extends Object
implements Opcode
Class Inheritance
All Implemented Interfaces
javassist.bytecode.Opcode
Imports
java.util.ArrayList, .List

An iterator for editing a code attribute.

To directly read or edit a bytecode sequence, call byteAt(int), s16bitAt(int), writeByte(int, int), 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 next() on a CodeIterator. It returns the index of the first byte of the next instruction.

If there are multiple CodeIterators 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 Also
CodeAttribute#iterator()

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv abstract static class
pack-priv abstract static class
public static class
CodeIterator.Gap

An inserted gap.

pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv abstract static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
protected byte[]
protected CodeAttribute
protected int
protected int
protected int
protected int
private static final int[]

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
private static void
public int

Returns:

the position of the first byte of the appended bytecode.
append
(byte[]
the bytecode appended.
code
)

Appends the given bytecode sequence at the end.

public void
append(ExceptionTable et, int
the value added to the code positions included in the entries.
offset
)

Copies and appends the entries in the given exception table at the end of the exception table in the code attribute edited by this object.

public void
appendGap(int
gap length
gapLength
)

Appends a gap at the end of the bytecode sequence.

public void
begin()

Moves to the first instruction.

public int
byteAt(int index)

Returns the unsigned 8bit value at the given index.

pack-priv static byte[]
private static int
copyGapBytes(byte[] newcode, int j, byte[] code, int i, int iEnd)

public CodeAttribute
get()

Returns a Code attribute read with this iterator.

public int
getCodeLength()

Returns code_length of Code_attribute.

public int

Returns:

the index of the position.
getMark
()

Gets the index of the position of the mark set by setMark.

public int

Returns:

the index of the position.
getMark2
()

Gets the index of the position of the mark set by setMark2.

public boolean
hasNext()

Returns true if there is more instructions.

public int

Returns:

the index indicating the first byte of the inserted byte sequence.
insert
(byte[]
inserted bytecode sequence.
code
)

Inserts the given bytecode sequence before the next instruction that would be returned by next() (not before the instruction returned by the last call to next()).

public void
insert(int
the index at which a byte sequence is inserted.
pos
,
byte[]
inserted bytecode sequence.
code
)

Inserts the given bytecode sequence before the instruction at the given index pos.

public void
insert(ExceptionTable et, int
the value added to the code positions included in the entries.
offset
)

Copies and inserts the entries in the given exception table at the beginning of the exception table in the code attribute edited by this object.

private int

Returns:

the index indicating the first byte of the inserted byte sequence.
insert0
(int pos, byte[] code, boolean exclusive)

public int

Returns:

the index indicating the first byte of the inserted byte sequence, which might be different from pos.
insertAt
(int
the index at which a byte sequence is inserted.
pos
,
byte[]
inserted bytecode sequence.
code
)

Inserts the given bytecode sequence before the instruction at the given index pos.

public int

Returns:

the index indicating the first byte of the inserted byte sequence.
insertEx
(byte[]
inserted bytecode sequence.
code
)

Inserts the given bytecode sequence exclusively before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()).

public void
insertEx(int
the index at which a byte sequence is inserted.
pos
,
byte[]
inserted bytecode sequence.
code
)

Inserts the given bytecode sequence exclusively before the instruction at the given index pos.

public int

Returns:

the index indicating the first byte of the inserted byte sequence, which might be different from pos.
insertExAt
(int
the index at which a byte sequence is inserted.
pos
,
byte[]
inserted bytecode sequence.
code
)

Inserts the given bytecode sequence exclusively before the instruction at the given index pos.

public int

Returns:

the index indicating the first byte of the inserted gap.
insertExGap
(int
gap length
length
)

Inserts an exclusive gap before the next instruction that would be returned by next() (not before the instruction returned by the last call to next()).

public int

Returns:

the length of the inserted gap. It might be bigger than length.
insertExGap
(int
the index at which a gap is inserted.
pos
,
int
gap length.
length
)

Inserts an exclusive gap in front of the instruction at the given index pos.

public int

Returns:

the index indicating the first byte of the inserted gap.
insertGap
(int
gap length
length
)

Inserts a gap before the next instruction that would be returned by next() (not before the instruction returned by the last call to next()).

public int

Returns:

the length of the inserted gap. It might be bigger than length.
insertGap
(int
the index at which a gap is inserted.
pos
,
int
gap length.
length
)

Inserts a gap in front of the instruction at the given index pos.

private static void
insertGap2(byte[] code, int where, int gapLength, int endPos, byte[] newcode, boolean exclusive)

private static byte[]
insertGap2w(byte[] code, int where, int gapLength, boolean exclusive, List<CodeIterator.Branch> jumps, CodeIterator.Pointers ptrs)

public CodeIterator.Gap

Returns:

the position and the length of the inserted gap.
insertGapAt
(int
the index at which a gap is inserted.
pos
,
int
gap length.
length
,
boolean
true if exclusive, otherwise false.
exclusive
)

Inserts an inclusive or exclusive gap in front of the instruction at the given index pos.

pack-priv static byte[]
insertGapCore0(byte[] code, int
It must indicate the first byte of an opcode.
where
,
int gapLength, boolean exclusive, ExceptionTable etable, CodeAttribute ca)

insertGapCore0() inserts a gap (some NOPs).

private byte[]
insertGapCore0w(byte[] code, int where, int gapLength, boolean exclusive, ExceptionTable etable, CodeAttribute ca, CodeIterator.Gap newWhere)

private static byte[]
insertGapCore1(byte[] code, int where, int gapLength, boolean exclusive, ExceptionTable etable, CodeAttribute ca)

public int
lookAhead()

Obtains the value that the next call to next() will return.

private static byte[]
makeExapndedCode(byte[] code, List<CodeIterator.Branch> jumps, int where, int gapLength)

private static List<CodeIterator.Branch>
makeJumpList(byte[] code, int endPos, CodeIterator.Pointers ptrs)

public void
move(int index)

Moves to the given index.

private static int
newOffset(int i, int offset, int where, int gapLength, boolean exclusive)

public int
next()

Returns the index of the next instruction (not the operand following the current opcode).

pack-priv static int
nextOpcode(byte[] code, int index)

Calculates the index of the next opcode.

public int
s16bitAt(int index)

Returns the signed 16bit value at the given index.

public int
s32bitAt(int index)

Returns the signed 32bit value at the given index.

public void
setMark(int index)

Sets a mark to the bytecode at the given index.

public void
setMark2(int index)

Sets a mark to the bytecode at the given index.

public int
signedByteAt(int index)

Returns the signed 8bit value at the given index.

public int

Returns:

the index of the INVOKESPECIAL instruction, or -1 if a constructor invocation is not found.
skipConstructor
()

Moves to the instruction for either super() or this().

public int

Returns:

the index of the INVOKESPECIAL instruction, or -1 if a super constructor invocation is not found but this() is found.
skipSuperConstructor
()

Moves to the instruction for super().

private int
skipSuperConstructor0(int skipThis)

public int

Returns:

the index of the INVOKESPECIAL instruction, or -1 if a explicit constructor invocation is not found but super() is found.
skipThisConstructor
()

Moves to the instruction for this().

public int
u16bitAt(int index)

Returns the unsigned 16bit value at the given index.

protected void
updateCursors(int
the position where a gap is inserted.
pos
,
int
the length of the gap.
length
)

Is called when a gap is inserted.

public void
write(byte[]
may be a zero-length array.
code
,
int index)

Writes a byte array at the index.

public void
write16bit(int value, int index)

Writes a 16 bit integer at the index.

public void
write32bit(int value, int index)

Writes a 32bit integer at the index.

public void
writeByte(int value, int index)

Writes an 8bit value at the given index.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait