Top Description Fields Constructors Methods
javassist.bytecode

public Class Bytecode

Additional top-level class in compilation unit: ByteVector.

extends ByteVector
implements Cloneable, Opcode
Class Inheritance
All Implemented Interfaces
javassist.bytecode.Opcode, java.lang.Cloneable
Imports
javassist.CtClass, .CtPrimitiveType

A utility class for producing a bytecode sequence.

A Bytecode object is an unbounded array containing bytecode. For example,

ConstPool cp = ...;    // constant pool table
Bytecode b = new Bytecode(cp, 1, 0);
b.addIconst(3);
b.addReturn(CtClass.intType);
CodeAttribute ca = b.toCodeAttribute();

This program produces a Code attribute including a bytecode sequence:

iconst_3
ireturn
See Also
ConstPool, CodeAttribute

Field Summary

Modifier and TypeField and Description
pack-priv ConstPool
pack-priv int
pack-priv int
private int
public static final CtClass
THIS

Represents the CtClass file using the constant pool table given to this Bytecode object.

pack-priv ExceptionTable

Constructor Summary

AccessConstructor and Description
public
Bytecode(ConstPool
constant pool table.
cp
,
int
max_stack.
stacksize
,
int
max_locals.
localvars
)

Constructs a Bytecode object with an empty bytecode sequence.

public
Bytecode(ConstPool
constant pool table.
cp
)

Constructs a Bytecode object with an empty bytecode sequence.

Method Summary

Modifier and TypeMethod and Description
public void
add(int code)

Overrides javassist.bytecode.ByteVector.add.

Appends an 8bit value to the end of the bytecode sequence.

public void
add32bit(int value)

Appends a 32bit value to the end of the bytecode sequence.

public void
addAload(int
an index into the local variable array.
n
)

Appends ALOAD or (WIDE) ALOAD_<n>

public void
addAnewarray(String
the qualified class name of the element type.
classname
)

Appends ANEWARRAY.

public void
addAnewarray(CtClass
the elememnt type.
clazz
,
int
the array length.
length
)

Appends ICONST and ANEWARRAY.

public void
addAstore(int
an index into the local variable array.
n
)

Appends ASTORE or (WIDE) ASTORE_<n>

public void
addCheckcast(CtClass
the type.
c
)

Appends CHECKCAST.

public void
addCheckcast(String
a fully-qualified class name.
classname
)

Appends CHECKCAST.

public void
addConstZero(CtClass
the type of the zero value (or null).
type
)

Appends an instruction for pushing zero or null on the stack.

public void
addDconst(double
the pushed double constant.
d
)

Appends DCONST or DCONST_<n>

public void
addDload(int
an index into the local variable array.
n
)

Appends DLOAD or (WIDE) DLOAD_<n>

public void
addDstore(int
an index into the local variable array.
n
)

Appends DSTORE or (WIDE) DSTORE_<n>

public void
addExceptionHandler(int start, int end, int handler, CtClass type)

Adds a new entry of exception_table.

public void
addExceptionHandler(int start, int end, int handler, String
the fully-qualified name of a throwable class.
type
)

Adds a new entry of exception_table.

public void
addExceptionHandler(int start, int end, int handler, int type)

Adds a new entry of exception_table.

public void
addFconst(float
the pushed float constant.
f
)

Appends FCONST or FCONST_<n>

public void
addFload(int
an index into the local variable array.
n
)

Appends FLOAD or (WIDE) FLOAD_<n>

public void
addFstore(int
an index into the local variable array.
n
)

Appends FSTORE or FSTORE_<n>

public void
addGap(int
the gap length in byte.
length
)

Overrides javassist.bytecode.ByteVector.addGap.

Appends the length-byte gap to the end of the bytecode sequence.

public void
addGetfield(CtClass
the class.
c
,
String
the field name.
name
,
String
the descriptor of the field type.
type
)

Appends GETFIELD.

public void
addGetfield(String
the fully-qualified class name.
c
,
String
the field name.
name
,
String
the descriptor of the field type.
type
)

Appends GETFIELD.

public void
addGetstatic(CtClass
the class
c
,
String
the field name
name
,
String
the descriptor of the field type.
type
)

Appends GETSTATIC.

public void
addGetstatic(String
the fully-qualified class name
c
,
String
the field name
name
,
String
the descriptor of the field type.
type
)

Appends GETSTATIC.

public void
addIconst(int
the pushed integer constant.
n
)

Appends ICONST or ICONST_<n>

public void
addIload(int
an index into the local variable array.
n
)

Appends ILOAD or (WIDE) ILOAD_<n>

public void
addIndex(int index)

Appends a 16bit value to the end of the bytecode sequence.

public void
addInstanceof(String
the class name.
classname
)

Appends INSTANCEOF.

public void
addInvokedynamic(int
an index into the bootstrap_methods array of the bootstrap method table.
bootstrap
,
String
the method name.
name
,
String
the method descriptor.
desc
)

Appends INVOKEDYNAMIC.

public void
addInvokeinterface(CtClass
the target class.
clazz
,
String
the method name
name
,
CtClass
the return type.
returnType
,
CtClass[]
the parameter types.
paramTypes
,
int
the count operand of the instruction.
count
)

Appends INVOKEINTERFACE.

public void
addInvokeinterface(CtClass
the target class.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
,
int
the count operand of the instruction.
count
)

Appends INVOKEINTERFACE.

public void
addInvokeinterface(String
the fully-qualified class name.
classname
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
,
int
the count operand of the instruction.
count
)

Appends INVOKEINTERFACE.

public void
addInvokeinterface(int
the index of CONSTANT_Class_info structure.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
,
int
the count operand of the instruction.
count
)

Appends INVOKEINTERFACE.

public void
addInvokespecial(CtClass
the target class.
clazz
,
String
the method name.
name
,
CtClass
the return type.
returnType
,
CtClass[]
the parameter types.
paramTypes
)

Appends INVOKESPECIAL.

public void
addInvokespecial(CtClass
the target class.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESPECIAL.

public void
addInvokespecial(String
the fully-qualified class name.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESPECIAL.

public void
addInvokespecial(int
the index of CONSTANT_Class_info structure.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESPECIAL.

public void
addInvokespecial(boolean
true if the invoked method is a default method declared in an interface.
isInterface
,
int
the index of CONSTANT_Class_info structure.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESPECIAL.

public void
addInvokespecial(int
the index of CONSTANT_Methodref_info or CONSTANT_InterfaceMethodref_info
index
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESPECIAL.

public void
addInvokestatic(CtClass
the target class.
clazz
,
String
the method name
name
,
CtClass
the return type.
returnType
,
CtClass[]
the parameter types.
paramTypes
)

Appends INVOKESTATIC.

public void
addInvokestatic(CtClass
the target class.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESTATIC.

public void
addInvokestatic(String
the fully-qualified class name. It must not be an interface-type name.
classname
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESTATIC.

public void
addInvokestatic(int
the index of CONSTANT_Class_info structure. It must not be an interface type.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKESTATIC.

private void
addInvokestatic(int clazz, String name, String desc, boolean isInterface)

public void
addInvokevirtual(CtClass
the target class.
clazz
,
String
the method name
name
,
CtClass
the return type.
returnType
,
CtClass[]
the parameter types.
paramTypes
)

Appends INVOKEVIRTUAL.

public void
addInvokevirtual(CtClass
the target class.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKEVIRTUAL.

public void
addInvokevirtual(String
the fully-qualified class name.
classname
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKEVIRTUAL.

public void
addInvokevirtual(int
the index of CONSTANT_Class_info structure.
clazz
,
String
the method name
name
,
String
the descriptor of the method signature.
desc
)

Appends INVOKEVIRTUAL.

public void
addIstore(int
an index into the local variable array.
n
)

Appends ISTORE or (WIDE) ISTORE_<n>

public void
addLconst(long
the pushed long integer constant.
n
)

Appends LCONST or LCONST_<n>

public void
addLdc(String
the character string pushed by LDC or LDC_W.
s
)

Appends LDC or LDC_W.

public void
addLdc(int
index into the constant pool.
i
)

Appends LDC or LDC_W.

public void
addLdc2w(long l)

Appends LDC2_W.

public void
addLdc2w(double d)

Appends LDC2_W.

public void
addLload(int
an index into the local variable array.
n
)

Appends LLOAD or (WIDE) LLOAD_<n>

public int

Returns:

the size of the value (1 or 2 word).
addLoad
(int
the index.
n
,
CtClass
the type of the loaded value.
type
)

Appends an instruction for loading a value from the local variable at the index n.

public int
addLoadParameters(CtClass[] params, int
the index of the first parameter. It is 0 if the method is static. Otherwise, it is 1.
offset
)

Appends instructions for loading all the parameters onto the operand stack.

public void
addLstore(int
an index into the local variable array.
n
)

Appends LSTORE or LSTORE_<n>

public int

Returns:

the length of dimensions.
addMultiNewarray
(CtClass
the array type.
clazz
,
int[]
the sizes of all dimensions.
dimensions
)

Appends MULTINEWARRAY.

public int

Returns:

the value of dim.
addMultiNewarray
(CtClass
the array type.
clazz
,
int
the number of the dimensions.
dim
)

Appends MULTINEWARRAY.

public int

Returns:

the value of dim.
addMultiNewarray
(String
the type descriptor of the created array.
desc
,
int
dimensions.
dim
)

Appends MULTINEWARRAY.

public void
addNew(CtClass
the class of the created instance.
clazz
)

Appends NEW.

public void
addNew(String
the fully-qualified class name.
classname
)

Appends NEW.

public void
addNewarray(int
T_BOOLEAN, T_CHAR, ...
atype
,
int length)

Appends NEWARRAY for primitive types.

public void
addOpcode(int code)

Appends an 8bit opcode to the end of the bytecode sequence.

public void
addPrintln(String
printed message.
message
)

Appends instructions for executing java.lang.System.println(message).

public void
addPutfield(CtClass
the target class.
c
,
String
the field name.
name
,
String
the descriptor of the field type.
desc
)

Appends PUTFIELD.

public void
addPutfield(String
the fully-qualified name of the target class.
classname
,
String
the field name.
name
,
String
the descriptor of the field type.
desc
)

Appends PUTFIELD.

private void
addPutfield0(CtClass target, String classname, String name, String desc)

public void
addPutstatic(CtClass
the target class.
c
,
String
the field name.
name
,
String
the descriptor of the field type.
desc
)

Appends PUTSTATIC.

public void
addPutstatic(String
the fully-qualified name of the target class.
classname
,
String
the field name.
fieldName
,
String
the descriptor of the field type.
desc
)

Appends PUTSTATIC.

private void
addPutstatic0(CtClass target, String classname, String fieldName, String desc)

public void
addRet(int
local variable
var
)

Appends RET.

public void
addReturn(CtClass
the return type.
type
)

Appends ARETURN, IRETURN, .., or RETURN.

public int

Returns:

2 if the type is long or double. Otherwise 1.
addStore
(int
the index.
n
,
CtClass
the type of the stored value.
type
)

Appends an instruction for storing a value into the local variable at the index n.

public Object
clone()

Overrides javassist.bytecode.ByteVector.clone.

Creates and returns a copy of this object.

public int
currentPc()

Returns the length of bytecode sequence that have been added so far.

public byte[]
get()

Returns the produced bytecode sequence.

public ConstPool
getConstPool()

Gets a constant pool table.

public ExceptionTable
getExceptionTable()

Returns exception_table.

public int
getMaxLocals()

Gets max_locals.

public int
getMaxStack()

Gets max_stack.

public int
getStackDepth()

Returns the current stack depth.

public void
growStack(int
the number added to the current stack depth.
diff
)

Increases the current stack depth.

public void
incMaxLocals(int diff)

Increments max_locals.

public int
length()

Returns the length of the bytecode sequence.

public int
read(int offset)

Overrides javassist.bytecode.ByteVector.read.

Reads a signed 8bit value at the offset from the beginning of the bytecode sequence.

public int
read16bit(int offset)

Reads a signed 16bit value at the offset from the beginning of the bytecode sequence.

public int
read32bit(int offset)

Reads a signed 32bit value at the offset from the beginning of the bytecode sequence.

public void
setMaxLocals(int size)

Sets max_locals.

public void
setMaxLocals(boolean
true if params must be interpreted as parameters to a static method.
isStatic
,
CtClass[]
parameter types.
params
,
int
the number of local variables excluding ones used to pass parameters.
locals
)

Sets max_locals.

public void
setMaxStack(int size)

Sets max_stack.

public void
setStackDepth(int
new value.
depth
)

Sets the current stack depth.

public CodeAttribute
toCodeAttribute()

Converts to a CodeAttribute.

public void
write(int offset, int value)

Overrides javassist.bytecode.ByteVector.write.

Writes an 8bit value at the offset from the beginning of the bytecode sequence.

public void
write16bit(int offset, int value)

Writes an 16bit value at the offset from the beginning of the bytecode sequence.

public void
write32bit(int offset, int value)

Writes an 32bit value at the offset from the beginning of the bytecode sequence.

Inherited from javassist.bytecode.ByteVector:
addaddcopygetSize