ClassFile
object
Javassist also provides lower-level API for directly editing
a class file. To use this level of API, you need detailed
knowledge of the Java bytecode and the class file format
while this level of API allows you any kind of modification
of class files.
A Otherwise, you can construct a
This code snippet creats a
A
Note that
To remove a field or a method from a
To examine every bytecode instruction in a method body,
A The following code snippet displays all the instructions included
in a method body:
A
This produces the code attribute representing the following sequence:
You can also obtain a byte array containing this sequence by
calling
While
5. Bytecode level API
5.1 Obtaining a
ClassFile
objectjavassist.bytecode.ClassFile
object represents
a class file. To obtian this object, getClassFile()
in CtClass
should be called.
javassist.bytecode.ClassFile
directly from a class file.
For example,
BufferedInputStream fin
= new BufferedInputStream(new FileInputStream("Point.class"));
ClassFile cf = new ClassFile(new DataInputStream(fin));
ClassFile
object from
Point.class
.
ClassFile
object can be written back to a
class file. write()
in ClassFile
writes the contents of the class file to a given
DataOutputStream
.
5.2 Adding and removing a member
ClassFile
provides addField()
and
addMethod()
for adding a field or a method (note that
a constructor is regarded as a method at the bytecode level).
It also provides addAttribute()
for adding an attribute
to the class file.
FieldInfo
, MethodInfo
, and
AttributeInfo
objects include a link to a
ConstPool
(constant pool table) object. The ConstPool
object must be common to the ClassFile
object and
a FieldInfo
(or MethodInfo
etc.) object
that is added to that ClassFile
object.
In other words, a FieldInfo
(or MethodInfo
etc.) object
must not be shared among different ClassFile
objects.
ClassFile
object,
you must first obtain a java.util.List
object containing all the fields of the class. getFields()
and getMethods()
return the lists. A field or a method can
be removed by calling remove()
on the List
object.
An attribute can be removed in a similar way.
Call getAttributes()
in FieldInfo
or
MethodInfo
to obtain the list of attributes,
and remove one from the list.
5.3 Traversing a method body
CodeIterator
is useful. To otbain this object,
do as follows:
ClassFile cf = ... ;
MethodInfo minfo = cf.getMethod("move"); // we assume move is not overloaded.
CodeAttribute ca = minfo.getCodeAttribute();
CodeIterator i = ca.iterator();
CodeIterator
object allows you to visit every
bytecode instruction one by one from the beginning to the end.
The following methods are part of the methods declared in
CodeIterator
:
void begin()
Move to the first instruction.
void move(int index)
Move to the instruction specified by the given index.
boolean hasNext()
Returns true if there is more instructions.
int next()
Returns the index of the next instruction.
Note that it does not return the opcode of the next
instruction.
int byteAt(int index)
Returns the unsigned 8bit value at the index.
int u16bitAt(int index)
Returns the unsigned 16bit value at the index.
int write(byte[] code, int index)
Writes a byte array at the index.
void insert(int index, byte[] code)
Inserts a byte array at the index.
Branch offsets etc. are automatically adjusted.
CodeIterator ci = ... ;
while (ci.hasNext()) {
int index = ci.next();
int op = ci.byteAt(index);
System.out.println(Mnemonic.OPCODE[op]);
}
5.4 Producing a bytecode sequence
Bytecode
object represents a sequence of bytecode
instructions. It is a growable array of bytecode.
Here is a sample code snippet:
ConstPool cp = ...; // constant pool table
Bytecode b = new Bytecode(cp, 1, 0);
b.addIconst(3);
b.addReturn(CtClass.intType);
CodeAttribute ca = b.toCodeAttribute();
iconst_3
ireturn
get()
in Bytecode
. The
obtained array can be inserted in another code attribute.
Bytecode
provides a number of methods for adding a
specific instruction to the sequence, it provides
addOpcode()
for adding an 8bit opcode and
addIndex()
for adding an index.
The 8bit value of each opcode is defined in the Opcode
interface.
addOpcode()
and other methods for adding a specific
instruction are automatically maintain the maximum stack depth
unless the control flow does not include a branch.
This value can be obtained by calling getMaxStack()
on the Bytecode
object.
It is also reflected on the CodeAttribute
object
constructed from the Bytecode
object.
To recompute the maximum stack depth of a method body,
call computeMaxStack()
in CodeAttribute
.