javassist.bytecode

Class CodeIterator

public class CodeIterator extends Object implements Opcode

An iterator for editing a code attribute.

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: iterator

Method Summary
intappend(byte[] code)
Appends the given bytecode sequence at the end.
voidappend(ExceptionTable et, int 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.
voidappendGap(int gapLength)
Appends a gap at the end of the bytecode sequence.
voidbegin()
Moves to the first instruction.
intbyteAt(int index)
Returns the unsigned 8bit value at the given index.
CodeAttributeget()
Returns a Code attribute read with this iterator.
intgetCodeLength()
Returns code_length of Code_attribute.
booleanhasNext()
Returns true if there is more instructions.
intinsert(byte[] code)
Inserts the given bytecode sequence before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()).
voidinsert(int pos, byte[] code)
Inserts the given bytecode sequence before the instruction at the given index pos.
voidinsert(ExceptionTable et, int 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.
intinsertEx(byte[] 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()).
voidinsertEx(int pos, byte[] code)
Inserts the given bytecode sequence exclusively before the instruction at the given index pos.
intinsertExGap(int length)
Inserts an exclusive gap before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()).
intinsertExGap(int pos, int length)
Inserts an exclusive gap in front of the instruction at the given index pos.
intinsertGap(int length)
Inserts a gap before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()).
intinsertGap(int pos, int length)
Inserts a gap in front of the instruction at the given index pos.
intlookAhead()
Obtains the value that the next call to next() will return.
voidmove(int index)
Moves to the given index.
intnext()
Returns the index of the next instruction (not the operand following the current opcode).
ints16bitAt(int index)
Returns the signed 16bit value at the given index.
ints32bitAt(int index)
Returns the signed 32bit value at the given index.
intskipConstructor()
Moves to the instruction for either super() or this().
intskipSuperConstructor()
Moves to the instruction for super().
intskipThisConstructor()
Moves to the instruction for this().
intu16bitAt(int index)
Returns the unsigned 16bit value at the given index.
voidwrite(byte[] code, int index)
Writes a byte array at the index.
voidwrite16bit(int value, int index)
Writes a 16 bit integer at the index.
voidwrite32bit(int value, int index)
Writes a 32bit integer at the index.
voidwriteByte(int value, int index)
Writes an 8bit value at the given index.

Method Detail

append

public int append(byte[] code)
Appends the given bytecode sequence at the end.

Parameters: code the bytecode appended.

Returns: the position of the first byte of the appended bytecode.

append

public void append(ExceptionTable et, int 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.

Parameters: offset the value added to the code positions included in the entries.

appendGap

public void appendGap(int gapLength)
Appends a gap at the end of the bytecode sequence.

Parameters: gapLength gap length

begin

public void begin()
Moves to the first instruction.

byteAt

public int byteAt(int index)
Returns the unsigned 8bit value at the given index.

get

public CodeAttribute get()
Returns a Code attribute read with this iterator.

getCodeLength

public int getCodeLength()
Returns code_length of Code_attribute.

hasNext

public boolean hasNext()
Returns true if there is more instructions.

insert

public int insert(byte[] code)
Inserts the given bytecode sequence before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()). Branch offsets and the exception table are also updated.

If the next instruction is at the beginning of a block statement, then the bytecode is inserted within that block.

An extra gap may be inserted at the end of the inserted bytecode sequence for adjusting alignment if the code attribute includes LOOKUPSWITCH or TABLESWITCH.

Parameters: code inserted bytecode sequence.

Returns: the index indicating the first byte of the inserted byte sequence.

insert

public void insert(int pos, byte[] code)
Inserts the given bytecode sequence before the instruction at the given index pos. Branch offsets and the exception table are also updated.

If the instruction at the given index is at the beginning of a block statement, then the bytecode is inserted within that block.

An extra gap may be inserted at the end of the inserted bytecode sequence for adjusting alignment if the code attribute includes LOOKUPSWITCH or TABLESWITCH.

Parameters: pos the index at which a byte sequence is inserted. code inserted bytecode sequence.

insert

public void insert(ExceptionTable et, int 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.

Parameters: offset the value added to the code positions included in the entries.

insertEx

public int insertEx(byte[] 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()). Branch offsets and the exception table are also updated.

If the next instruction is at the beginning of a block statement, then the bytecode is excluded from that block.

An extra gap may be inserted at the end of the inserted bytecode sequence for adjusting alignment if the code attribute includes LOOKUPSWITCH or TABLESWITCH.

Parameters: code inserted bytecode sequence.

Returns: the index indicating the first byte of the inserted byte sequence.

insertEx

public void insertEx(int pos, byte[] code)
Inserts the given bytecode sequence exclusively before the instruction at the given index pos. Branch offsets and the exception table are also updated.

If the instruction at the given index is at the beginning of a block statement, then the bytecode is excluded from that block.

An extra gap may be inserted at the end of the inserted bytecode sequence for adjusting alignment if the code attribute includes LOOKUPSWITCH or TABLESWITCH.

Parameters: pos the index at which a byte sequence is inserted. code inserted bytecode sequence.

insertExGap

public int insertExGap(int length)
Inserts an exclusive gap before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()). Branch offsets and the exception table are also updated. The inserted gap is filled with NOP. The gap length may be extended to a multiple of 4.

If the next instruction is at the beginning of a block statement, then the gap is excluded from that block.

Parameters: length gap length

Returns: the index indicating the first byte of the inserted gap.

insertExGap

public int insertExGap(int pos, int length)
Inserts an exclusive gap in front of the instruction at the given index pos. Branch offsets and the exception table are also updated. The inserted gap is filled with NOP. The gap length may be extended to a multiple of 4.

If the instruction at the given index is at the beginning of a block statement, then the gap is excluded from that block.

Parameters: pos the index at which a gap is inserted. length gap length.

Returns: the length of the inserted gap. It might be bigger than length.

insertGap

public int insertGap(int length)
Inserts a gap before the next instruction that would be returned by next() (not before the instruction returned by tha last call to next()). Branch offsets and the exception table are also updated. The inserted gap is filled with NOP. The gap length may be extended to a multiple of 4.

If the next instruction is at the beginning of a block statement, then the gap is inserted within that block.

Parameters: length gap length

Returns: the index indicating the first byte of the inserted gap.

insertGap

public int insertGap(int pos, int length)
Inserts a gap in front of the instruction at the given index pos. Branch offsets and the exception table are also updated. The inserted gap is filled with NOP. The gap length may be extended to a multiple of 4.

If the instruction at the given index is at the beginning of a block statement, then the gap is inserted within that block.

Parameters: pos the index at which a gap is inserted. length gap length.

Returns: the length of the inserted gap. It might be bigger than length.

lookAhead

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

This method is side-effects free. Successive calls to lookAhead() return the same value until next() is called.

move

public void move(int index)
Moves to the given index.

The index of the next instruction is set to the given index. The successive call to next() returns the index that has been given to move().

Note that the index is into the byte array returned by get().getCode().

See Also: getCode

next

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

Note that the index is into the byte array returned by get().getCode().

See Also: getCode CodeIterator

s16bitAt

public int s16bitAt(int index)
Returns the signed 16bit value at the given index.

s32bitAt

public int s32bitAt(int index)
Returns the signed 32bit value at the given index.

skipConstructor

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

This method skips all the instructions for computing arguments to super() or this(), which should be placed at the beginning of a constructor body.

This method returns the index of INVOKESPECIAL instruction executing super() or this(). A successive call to next() returns the index of the next instruction following that INVOKESPECIAL.

This method works only for a constructor.

Returns: the index of the INVOKESPECIAL instruction, or -1 if a constructor invocation is not found.

skipSuperConstructor

public int skipSuperConstructor()
Moves to the instruction for super().

This method skips all the instructions for computing arguments to super(), which should be placed at the beginning of a constructor body.

This method returns the index of INVOKESPECIAL instruction executing super(). A successive call to next() returns the index of the next instruction following that INVOKESPECIAL.

This method works only for a constructor.

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

skipThisConstructor

public int skipThisConstructor()
Moves to the instruction for this().

This method skips all the instructions for computing arguments to this(), which should be placed at the beginning of a constructor body.

This method returns the index of INVOKESPECIAL instruction executing this(). A successive call to next() returns the index of the next instruction following that INVOKESPECIAL.

This method works only for a constructor.

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

u16bitAt

public int u16bitAt(int index)
Returns the unsigned 16bit value at the given index.

write

public void write(byte[] code, int index)
Writes a byte array at the index.

Parameters: code may be a zero-length array.

write16bit

public void write16bit(int value, int index)
Writes a 16 bit integer at the index.

write32bit

public void write32bit(int value, int index)
Writes a 32bit integer at the index.

writeByte

public void writeByte(int value, int index)
Writes an 8bit value at the given index.
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.