|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectclassUtils.javassist.bytecode.Bytecode
public class Bytecode
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
ConstPool
,
CodeAttribute
Field Summary | |
---|---|
static CompileTimeClass |
THIS
Represents the CtClass file using the
constant pool table given to this Bytecode object. |
Constructor Summary | |
---|---|
Bytecode(ConstPool cp,
int stacksize,
int localvars)
Constructs a Bytecode object with an empty bytecode
sequence. |
Method Summary | |
---|---|
void |
add(int code)
Appends an 8bit value to the end of the bytecode sequence. |
void |
addAload(int n)
Appends ALOAD or (WIDE) ALOAD_<n> |
void |
addAnewarray(CompileTimeClass clazz,
int length)
Appends ICONST and ANEWARRAY. |
void |
addAnewarray(java.lang.String classname)
Appends ANEWARRAY. |
void |
addAstore(int n)
Appends ASTORE or (WIDE) ASTORE_<n> |
void |
addCheckcast(CompileTimeClass c)
Appends CHECKCAST. |
void |
addCheckcast(java.lang.String classname)
Appends CHECKCAST. |
void |
addDconst(double d)
Appends DCONST or DCONST_<n> |
void |
addDload(int n)
Appends DLOAD or (WIDE) DLOAD_<n> |
void |
addDstore(int n)
Appends DSTORE or (WIDE) DSTORE_<n> |
void |
addExceptionHandler(int start,
int end,
int handler,
CompileTimeClass type)
Adds a new entry of exception_table . |
void |
addExceptionHandler(int start,
int end,
int handler,
int type)
Adds a new entry of exception_table . |
void |
addFconst(float f)
Appends FCONST or FCONST_<n> |
void |
addFload(int n)
Appends FLOAD or (WIDE) FLOAD_<n> |
void |
addFstore(int n)
Appends FSTORE or FSTORE_<n> |
void |
addGetfield(CompileTimeClass c,
java.lang.String name,
java.lang.String type)
Appends GETFIELD. |
void |
addGetstatic(CompileTimeClass c,
java.lang.String name,
java.lang.String type)
Appends GETSTATIC. |
void |
addGetstatic(java.lang.String c,
java.lang.String name,
java.lang.String type)
Appends GETSTATIC. |
void |
addIconst(int n)
Appends ICONST or ICONST_<n> |
void |
addIload(int n)
Appends ILOAD or (WIDE) ILOAD_<n> |
void |
addIndex(int index)
Appends a 16bit value to the end of the bytecode sequence. |
void |
addInstanceof(java.lang.String classname)
Appends INSTANCEOF. |
void |
addInvokeinterface(CompileTimeClass clazz,
java.lang.String name,
CompileTimeClass returnType,
CompileTimeClass[] paramTypes,
int count)
Appends INVOKEINTERFACE. |
void |
addInvokeinterface(CompileTimeClass clazz,
java.lang.String name,
java.lang.String desc,
int count)
Appends INVOKEINTERFACE. |
void |
addInvokeinterface(int clazz,
java.lang.String name,
java.lang.String desc,
int count)
Appends INVOKEINTERFACE. |
void |
addInvokeinterface(java.lang.String classname,
java.lang.String name,
java.lang.String desc,
int count)
Appends INVOKEINTERFACE. |
void |
addInvokespecial(CompileTimeClass clazz,
java.lang.String name,
CompileTimeClass returnType,
CompileTimeClass[] paramTypes)
Appends INVOKESPECIAL. |
void |
addInvokespecial(CompileTimeClass clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKESPECIAL. |
void |
addInvokespecial(int clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKESPECIAL. |
void |
addInvokespecial(java.lang.String clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKESPECIAL. |
void |
addInvokestatic(CompileTimeClass clazz,
java.lang.String name,
CompileTimeClass returnType,
CompileTimeClass[] paramTypes)
Appends INVOKESTATIC. |
void |
addInvokestatic(CompileTimeClass clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKESTATIC. |
void |
addInvokestatic(int clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKESTATIC. |
void |
addInvokestatic(java.lang.String classname,
java.lang.String name,
java.lang.String desc)
Appends INVOKESTATIC. |
void |
addInvokevirtual(CompileTimeClass clazz,
java.lang.String name,
CompileTimeClass returnType,
CompileTimeClass[] paramTypes)
Appends INVOKEVIRTUAL. |
void |
addInvokevirtual(CompileTimeClass clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKEVIRTUAL. |
void |
addInvokevirtual(int clazz,
java.lang.String name,
java.lang.String desc)
Appends INVOKEVIRTUAL. |
void |
addInvokevirtual(java.lang.String classname,
java.lang.String name,
java.lang.String desc)
Appends INVOKEVIRTUAL. |
void |
addIstore(int n)
Appends ISTORE or (WIDE) ISTORE_<n> |
void |
addLconst(long n)
Appends LCONST or LCONST_<n> |
void |
addLdc(int i)
Appends LDC or LDC_W. |
void |
addLdc(java.lang.String s)
Appends LDC or LDC_W. |
void |
addLdc2w(double d)
Appends LDC2_W. |
void |
addLdc2w(long l)
Appends LDC2_W. |
void |
addLload(int n)
Appends LLOAD or (WIDE) LLOAD_<n> |
int |
addLoad(int n,
CompileTimeClass type)
Appends an instruction for loading a value from the local variable affineTransform the index n . |
int |
addLoadParameters(CompileTimeClass[] params,
int offset)
Appends instructions for loading all the parameters onto the operand stack. |
void |
addLstore(int n)
Appends LSTORE or LSTORE_<n> |
int |
addMultiNewarray(CompileTimeClass clazz,
int dim)
Appends MULTINEWARRAY. |
int |
addMultiNewarray(CompileTimeClass clazz,
int[] dimensions)
Appends MULTINEWARRAY. |
int |
addMultiNewarray(java.lang.String desc,
int dim)
Appends MULTINEWARRAY. |
void |
addNew(CompileTimeClass clazz)
Appends NEW. |
void |
addNew(java.lang.String classname)
Appends NEW. |
void |
addNewarray(int atype,
int length)
Appends NEWARRAY for primitive types. |
void |
addOpcode(int code)
Appends an 8bit opcode to the end of the bytecode sequence. |
void |
addPrintln(java.lang.String message)
Appends instructions for executing java.lang.System.println(message) . |
void |
addPutfield(CompileTimeClass c,
java.lang.String name,
java.lang.String desc)
Appends PUTFIELD. |
void |
addPutstatic(CompileTimeClass c,
java.lang.String name,
java.lang.String desc)
Appends PUTSTATIC. |
void |
addRet(int var)
Appends RET. |
void |
addReturn(CompileTimeClass type)
Appends ARETURN, IRETURN, .., or RETURN. |
int |
addStore(int n,
CompileTimeClass type)
Appends an instruction for storing a value into the local variable affineTransform the index n . |
int |
currentPc()
Returns the length of bytecode sequence that have been added so far. |
byte[] |
get()
Returns the produced bytecode sequence. |
ConstPool |
getConstPool()
Gets a constant pool table. |
ExceptionTable |
getExceptionTable()
Returns exception_table . |
int |
getMaxLocals()
Gets max_locals . |
int |
getMaxStack()
Gets max_stack . |
int |
getStackDepth()
Returns the current stack depth. |
void |
growStack(int diff)
Increases the current stack depth. |
void |
incMaxLocals(int diff)
Increments max_locals . |
int |
length()
Returns the length of the bytecode sequence. |
int |
read(int offset)
Reads a signed 8bit value affineTransform the offset from the beginning of the bytecode sequence. |
int |
read16bit(int offset)
Reads a signed 16bit value affineTransform the offset from the beginning of the bytecode sequence. |
void |
setMaxLocals(boolean isStatic,
CompileTimeClass[] params,
int locals)
Sets max_locals . |
void |
setMaxLocals(int size)
Sets max_locals . |
void |
setMaxStack(int size)
Sets max_stack . |
void |
setStackDepth(int depth)
Sets the current stack depth. |
CodeAttribute |
toCodeAttribute()
Converts to a CodeAttribute . |
void |
write(int offset,
int value)
Writes an 8bit value affineTransform the offset from the beginning of the bytecode sequence. |
void |
write16bit(int offset,
int value)
Writes an 16bit value affineTransform the offset from the beginning of the bytecode sequence. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final CompileTimeClass THIS
CtClass
file using the
constant pool table given to this Bytecode
object.
Constructor Detail |
---|
public Bytecode(ConstPool cp, int stacksize, int localvars)
Bytecode
object with an empty bytecode
sequence.
The parameters stacksize
and localvars
specify initial values
of max_stack
and max_locals
.
They can be changed later.
cp
- constant pool table.stacksize
- max_stack
.localvars
- max_locals
.Method Detail |
---|
public ConstPool getConstPool()
public ExceptionTable getExceptionTable()
exception_table
.
public CodeAttribute toCodeAttribute()
CodeAttribute
.
public int length()
public byte[] get()
public int getMaxStack()
max_stack
.
public void setMaxStack(int size)
max_stack
.
This value may be automatically updated when an instruction
is appended. A Bytecode
object maintains the current
stack depth whenever an instruction is added
by addOpcode()
. For example, if DUP is appended,
the current stack depth is increased by one. If the new stack
depth is more than max_stack
, then it is assigned
to max_stack
. However, if branch instructions are
appended, the current stack depth may not be correctly maintained.
addOpcode(int)
public int getMaxLocals()
max_locals
.
public void setMaxLocals(int size)
max_locals
.
public void setMaxLocals(boolean isStatic, CompileTimeClass[] params, int locals)
max_locals
.
This computes the number of local variables
used to pass method parameters and sets max_locals
to that number plus locals
.
isStatic
- true if params
must be
interpreted as parameters to a static method.params
- parameter types.locals
- the number of local variables excluding
ones used to pass parameters.public void incMaxLocals(int diff)
max_locals
.
public void addExceptionHandler(int start, int end, int handler, CompileTimeClass type)
exception_table
.
public void addExceptionHandler(int start, int end, int handler, int type)
exception_table
.
public int currentPc()
public int read(int offset)
java.lang.ArrayIndexOutOfBoundsException
- if offset is invalid.public int read16bit(int offset)
public void write(int offset, int value)
java.lang.ArrayIndexOutOfBoundsException
- if offset is invalid.public void write16bit(int offset, int value)
public void add(int code)
public void addOpcode(int code)
max_stack
is updated if the current stack depth
is the deepest so far.
Note: some instructions such as INVOKEVIRTUAL does not
update the current stack depth since the increment depends
on the method signature.
growStack()
must be explicitly called.
public void growStack(int diff)
max_stack
if the current stack depth
is the deepest so far.
diff
- the number added to the current stack depth.public int getStackDepth()
public void setStackDepth(int depth)
max_stack
if the current stack depth
is the deepest so far.
depth
- new value.public void addIndex(int index)
public void addAload(int n)
n
- an index into the local variable array.public void addAstore(int n)
n
- an index into the local variable array.public void addIconst(int n)
n
- the pushed integer constant.public void addIload(int n)
n
- an index into the local variable array.public void addIstore(int n)
n
- an index into the local variable array.public void addLconst(long n)
n
- the pushed long integer constant.public void addLload(int n)
n
- an index into the local variable array.public void addLstore(int n)
n
- an index into the local variable array.public void addDconst(double d)
d
- the pushed double constant.public void addDload(int n)
n
- an index into the local variable array.public void addDstore(int n)
n
- an index into the local variable array.public void addFconst(float f)
f
- the pushed float constant.public void addFload(int n)
n
- an index into the local variable array.public void addFstore(int n)
n
- an index into the local variable array.public int addLoad(int n, CompileTimeClass type)
n
.
n
- the index.type
- the type of the loaded value.
public int addStore(int n, CompileTimeClass type)
n
.
n
- the index.type
- the type of the stored value.
public int addLoadParameters(CompileTimeClass[] params, int offset)
offset
- the index of the first parameter. It is 0
if the method is static. Otherwise, it is 1.public void addCheckcast(CompileTimeClass c)
c
- the type.public void addCheckcast(java.lang.String classname)
classname
- a fully-qualified class name.public void addInstanceof(java.lang.String classname)
classname
- the class name.public void addGetfield(CompileTimeClass c, java.lang.String name, java.lang.String type)
c
- the classname
- the field nametype
- the descriptor of the field type.Descriptor.of(CompileTimeClass)
public void addGetstatic(CompileTimeClass c, java.lang.String name, java.lang.String type)
c
- the classname
- the field nametype
- the descriptor of the field type.Descriptor.of(CompileTimeClass)
public void addGetstatic(java.lang.String c, java.lang.String name, java.lang.String type)
c
- the fully-qualified class namename
- the field nametype
- the descriptor of the field type.Descriptor.of(CompileTimeClass)
public void addInvokespecial(CompileTimeClass clazz, java.lang.String name, CompileTimeClass returnType, CompileTimeClass[] paramTypes)
clazz
- the target class.name
- the method name.returnType
- the return type.paramTypes
- the parameter types.public void addInvokespecial(CompileTimeClass clazz, java.lang.String name, java.lang.String desc)
clazz
- the target class.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
,
Descriptor.ofConstructor(CompileTimeClass[])
public void addInvokespecial(java.lang.String clazz, java.lang.String name, java.lang.String desc)
clazz
- the fully-qualified class name.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
,
Descriptor.ofConstructor(CompileTimeClass[])
public void addInvokespecial(int clazz, java.lang.String name, java.lang.String desc)
clazz
- the index of CONSTANT_Class_info
structure.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
,
Descriptor.ofConstructor(CompileTimeClass[])
public void addInvokestatic(CompileTimeClass clazz, java.lang.String name, CompileTimeClass returnType, CompileTimeClass[] paramTypes)
clazz
- the target class.name
- the method namereturnType
- the return type.paramTypes
- the parameter types.public void addInvokestatic(CompileTimeClass clazz, java.lang.String name, java.lang.String desc)
clazz
- the target class.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokestatic(java.lang.String classname, java.lang.String name, java.lang.String desc)
classname
- the fully-qualified class name.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokestatic(int clazz, java.lang.String name, java.lang.String desc)
clazz
- the index of CONSTANT_Class_info
structure.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokevirtual(CompileTimeClass clazz, java.lang.String name, CompileTimeClass returnType, CompileTimeClass[] paramTypes)
The specified method must not be an inherited method.
It must be directly declared in the class specified
in clazz
.
clazz
- the target class.name
- the method namereturnType
- the return type.paramTypes
- the parameter types.public void addInvokevirtual(CompileTimeClass clazz, java.lang.String name, java.lang.String desc)
The specified method must not be an inherited method.
It must be directly declared in the class specified
in clazz
.
clazz
- the target class.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokevirtual(java.lang.String classname, java.lang.String name, java.lang.String desc)
The specified method must not be an inherited method.
It must be directly declared in the class specified
in classname
.
classname
- the fully-qualified class name.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokevirtual(int clazz, java.lang.String name, java.lang.String desc)
The specified method must not be an inherited method.
It must be directly declared in the class specified
by clazz
.
clazz
- the index of CONSTANT_Class_info
structure.name
- the method namedesc
- the descriptor of the method signature.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokeinterface(CompileTimeClass clazz, java.lang.String name, CompileTimeClass returnType, CompileTimeClass[] paramTypes, int count)
clazz
- the target class.name
- the method namereturnType
- the return type.paramTypes
- the parameter types.count
- the count operand of the instruction.public void addInvokeinterface(CompileTimeClass clazz, java.lang.String name, java.lang.String desc, int count)
clazz
- the target class.name
- the method namedesc
- the descriptor of the method signature.count
- the count operand of the instruction.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokeinterface(java.lang.String classname, java.lang.String name, java.lang.String desc, int count)
classname
- the fully-qualified class name.name
- the method namedesc
- the descriptor of the method signature.count
- the count operand of the instruction.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addInvokeinterface(int clazz, java.lang.String name, java.lang.String desc, int count)
clazz
- the index of CONSTANT_Class_info
structure.name
- the method namedesc
- the descriptor of the method signature.count
- the count operand of the instruction.Descriptor.ofMethod(CompileTimeClass,CompileTimeClass[])
public void addLdc(java.lang.String s)
String
object.
s
- the character string pushed by LDC or LDC_W.public void addLdc(int i)
i
- index into the constant pool.public void addLdc2w(long l)
public void addLdc2w(double d)
public void addNew(CompileTimeClass clazz)
clazz
- the class of the created instance.public void addNew(java.lang.String classname)
classname
- the fully-qualified class name.public void addAnewarray(java.lang.String classname)
classname
- the qualified class name of the element type.public void addAnewarray(CompileTimeClass clazz, int length)
clazz
- the elememnt type.length
- the array length.public void addNewarray(int atype, int length)
atype
- T_BOOLEAN
, T_CHAR
, ...Opcode
public int addMultiNewarray(CompileTimeClass clazz, int[] dimensions)
clazz
- the array type.dimensions
- the sizes of all dimensions.
dimensions
.public int addMultiNewarray(CompileTimeClass clazz, int dim)
clazz
- the array type.dim
- the number of the dimensions.
dim
.public int addMultiNewarray(java.lang.String desc, int dim)
desc
- the type descriptor of the created array.dim
- dimensions.
dim
.public void addPutfield(CompileTimeClass c, java.lang.String name, java.lang.String desc)
c
- the target class.name
- the field name.desc
- the descriptor of the field type.public void addPutstatic(CompileTimeClass c, java.lang.String name, java.lang.String desc)
c
- the target class.name
- the field name.desc
- the descriptor of the field type.public void addReturn(CompileTimeClass type)
type
- the return type.public void addRet(int var)
var
- local variablepublic void addPrintln(java.lang.String message)
java.lang.System.println(message)
.
message
- printed message.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |