ClassFile
object
9. Boxing/Unboxing
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.
If you want to just produce a simple class file,
A Otherwise, you can construct a
This code snippet creats a
A You can create a new class file from scratch. For example,
this code generates a class file
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
this code makes the default constructor and adds it to the class specified
by Annotations are stored in a class file
as runtime invisible (or visible) annotations attribute.
These attributes can be obtained from Javassist also let you access annotations by the higher-level
API.
If you want to access annotations through The lower-level API of Javassist fully supports generics
introduced by Java 5. On the other hand, the higher-level
API such as The generics of Java is implemented by the erasure technique.
After compilation, all type parameters are dropped off. For
example, suppose that your source code declares a parameterized
type The compiled bytecode is equivalent to the following code:
So when you write a bytecode transformer, you can just drop
off all type parameters. Because the compiler embedded in Javassist
does not support generics,
you must insert an explicit type cast at the
caller site if the source code is compiled by Javassist, for example,
through For example, if you have a class:
and want to add an interface then the interface you really have to add is Note that no type parameters are necessary.
Since The type cast is not needed if the source code is compiled by a normal Java
compiler because it will automatically insert a type cast.
If you need to make type parameters accessible through reflection
during runtime, you have to add generic signatures to the class file.
For more details, see the API documentation (javadoc) of the
Currently, Javassist does not directly support varargs. So to make a method with varargs,
you must explicitly set a method modifier. But this is easy.
Suppose that now you want to make the following method:
The following code using Javassist will make the method shown above:
The parameter type To call this method in the source code compiled by the compiler embedded in Javassist,
you must write:
instead of this method call using the varargs mechanism:
If you modify a class file for the J2ME execution environment,
you must perform You can also manually
produce a stack map for a modified method.
For a given method represented by a Here, Boxing and unboxing in Java are syntactic sugar. There is no bytecode for
boxing or unboxing. So the compiler of Javassist does not support them.
For example, the following statement is valid in Java:
since boxing is implicitly performed. For Javassist, however, you must explicitly
convert a value type from Set For example,
All modified class files are saved in
5. Bytecode level API
javassist.bytecode.ClassFileWriter
might provide
the best API for you. It is much faster than
javassist.bytecode.ClassFile
although its API
is minimum.
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
.
ClassFile cf = new ClassFile(false, "test.Foo", null);
cf.setInterfaces(new String[] { "java.lang.Cloneable" });
FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I");
f.setAccessFlags(AccessFlag.PUBLIC);
cf.addField(f);
cf.write(new DataOutputStream(new FileOutputStream("Foo.class")));
Foo.class
that contains
the implementation of the following class:
package test;
class Foo implements Cloneable {
public int width;
}
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
.
Bytecode
can be used to construct a method.
For example,
ClassFile cf = ...
Bytecode code = new Bytecode(cf.getConstPool());
code.addAload(0);
code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
code.addReturn(null);
code.setMaxLocals(1);
MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);
cf
. The Bytecode
object is first converted into
a CodeAttribute
object and then added to the method specified
by minfo
. The method is finally added to a class file cf
.
5.5 Annotations (Meta tags)
ClassFile
,
MethodInfo
, or FieldInfo
objects.
Call getAttribute(AnnotationsAttribute.invisibleTag)
on those objects. For more details, see the javadoc manual
of javassist.bytecode.AnnotationsAttribute
class
and the javassist.bytecode.annotation
package.
CtClass
,
call getAnnotations()
in CtClass
or
CtBehavior
.
6. Generics
CtClass
does not directly support
generics. However, this is not a serious problem for bytecode
transformation.
Vector<String>
:
Vector<String> v = new Vector<String>();
:
String s = v.get(0);
Vector v = new Vector();
:
String s = (String)v.get(0);
CtMethod.make()
. No type cast
is necessary if the source code is compiled by a normal Java compiler
such as javac
.
public class Wrapper<T> {
T value;
public Wrapper(T t) { value = t; }
}
Getter<T>
to the
class Wrapper<T>
:
public interface Getter<T> {
T get();
}
Getter
(the type parameters <T>
drops off)
and the method you also have to add to the Wrapper
class is this simple one:
public Object get() { return value; }
get
returns an Object
, an explicit type cast
is needed at the caller site if the source code is compiled by Javassist.
For example, if the type parameter T
is String
, then (String)
must be inserted as follows:
Wrapper w = ...
String s = (String)w.get();
setGenericSignature
method in the CtClass
.
7. Varargs
public int length(int... args) { return args.length; }
CtClass cc = /* target class */;
CtMethod m = CtMethod.make("public int length(int[] args) { return args.length; }", cc);
m.setModifiers(m.getModifiers() | Modifier.VARARGS);
cc.addMethod(m);
int...
is changed into int[]
and Modifier.VARARGS
is added to the method modifiers.
length(new int[] { 1, 2, 3 });
length(1, 2, 3);
8. J2ME
javassist.bytecode.MethodInfo.doPreverify
is true.
CtMethod
object m
,
you can produce a stack map by calling the following methods:
m.getMethodInfo().rebuildStackMapForME(cpool);
cpool
is a ClassPool
object, which is
available by calling getClassPool()
on a CtClass
object. A ClassPool
object is responsible for finding
class files from given class pathes. To obtain all the CtMethod
objects, call the getDeclaredMethods
method on a CtClass
object.
9. Boxing/Unboxing
Integer i = 3;
int
to Integer
:
Integer i = new Integer(3);
10. Debug
CtClass.debugDump
to a directory name.
Then all class files modified and generated by Javassist are saved in that
directory. To stop this, set CtClass.debugDump
to null.
The default value is null.
CtClass.debugDump = "./dump";
./dump
.
Java(TM) is a trademark of Sun Microsystems, Inc.
Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved.