Example:
ClassFileWriter cfw = new ClassFileWriter(ClassFile.JAVA_4, 0);
ConstPoolWriter cpw = cfw.getConstPool();
FieldWriter fw = cfw.getFieldWriter();
fw.add(AccessFlag.PUBLIC, "value", "I", null);
fw.add(AccessFlag.PUBLIC, "value2", "J", null);
int thisClass = cpw.addClassInfo("sample/Test");
int superClass = cpw.addClassInfo("java/lang/Object");
MethodWriter mw = cfw.getMethodWriter();
mw.begin(AccessFlag.PUBLIC, MethodInfo.nameInit, "()V", null, null);
mw.add(Opcode.ALOAD_0);
mw.add(Opcode.INVOKESPECIAL);
int signature = cpw.addNameAndTypeInfo(MethodInfo.nameInit, "()V");
mw.add16(cpw.addMethodrefInfo(superClass, signature));
mw.add(Opcode.RETURN);
mw.codeEnd(1, 1);
mw.end(null, null);
mw.begin(AccessFlag.PUBLIC, "one", "()I", null, null);
mw.add(Opcode.ICONST_1);
mw.add(Opcode.IRETURN);
mw.codeEnd(1, 1);
mw.end(null, null);
byte[] classfile = cfw.end(AccessFlag.PUBLIC, thisClass, superClass,
null, null);
The code above generates the following class:
package sample;
public class Test {
public int value;
public long value2;
public Test() { super(); }
public one() { return 1; }
}
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ClassFileWriter.
This writes attributes. |
| public static class | ClassFileWriter.
Constant Pool. |
| public static class | |
| public static class | ClassFileWriter.
Method. |
| Modifier and Type | Field and Description |
|---|---|
| private ClassFileWriter. | |
| private ClassFileWriter. | |
| private ClassFileWriter. | |
| private ByteStream | |
| pack-priv int | |
| pack-priv int |
| Access | Constructor and Description |
|---|---|
| public | ClassFileWriter(int
the major version ( major, int ClassFile#JAVA_4, ClassFile#JAVA_5, ...).the minor version (0 for JDK 1.3 and later). minor)Constructs a class file writer. |
| Modifier and Type | Method and Description |
|---|---|
| public byte[] | end(int
access flags. accessFlags, int this class. an index indicating its thisClass, int CONSTANT_Class_info.super class. an index indicating its superClass, int[] CONSTANT_Class_info.implemented interfaces.
index numbers indicating their interfaces, ClassFileWriter.ClassInfo.
It may be null.attributes of the class file. May be null. awEnds writing and returns the contents of the class file. |
| public void | end(DataOutputStream out, int
access flags. accessFlags, int this class. an index indicating its thisClass, int CONSTANT_Class_info.super class. an index indicating its superClass, int[] CONSTANT_Class_info.implemented interfaces.
index numbers indicating their interfaces, ClassFileWriter.CONSTATNT_Class_info.
It may be null.attributes of the class file. May be null. awEnds writing and writes the contents of the class file into the given output stream. |
| public ClassFileWriter. | |
| public ClassFileWriter. | |
| public ClassFileWriter. | |
| pack-priv static void |
| constPool | back to summary |
|---|---|
| private ClassFileWriter. | |
| fields | back to summary |
|---|---|
| private ClassFileWriter. | |
| methods | back to summary |
|---|---|
| private ClassFileWriter. | |
| output | back to summary |
|---|---|
| private ByteStream output | |
| superClass | back to summary |
|---|---|
| pack-priv int superClass | |
| thisClass | back to summary |
|---|---|
| pack-priv int thisClass | |
| ClassFileWriter | back to summary |
|---|---|
| public ClassFileWriter(int major, int minor) Constructs a class file writer.
| |
| end | back to summary |
|---|---|
| public byte[] end(int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter. Ends writing and returns the contents of the class file.
| |
| end | back to summary |
|---|---|
| public void end(DataOutputStream out, int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter. Ends writing and writes the contents of the class file into the given output stream.
| |
| getConstPool | back to summary |
|---|---|
| public ClassFileWriter. Returns a constant pool. | |
| getFieldWriter | back to summary |
|---|---|
| public ClassFileWriter. Returns a filed writer. | |
| getMethodWriter | back to summary |
|---|---|
| public ClassFileWriter. Returns a method writer. | |
| writeAttribute | back to summary |
|---|---|
| pack-priv static void writeAttribute(ByteStream bs, ClassFileWriter. | |
For example, the following object writes a synthetic attribute:
ConstPoolWriter cpw = ...;
final int tag = cpw.addUtf8Info("Synthetic");
AttributeWriter aw = new AttributeWriter() {
public int size() {
return 1;
}
public void write(DataOutputStream out) throws java.io.IOException {
out.writeShort(tag);
out.writeInt(0);
}
};
| Modifier and Type | Method and Description |
|---|---|
| public int | |
| public void |
| size | back to summary |
|---|---|
| public int size() Returns the number of attributes that this writer will write. | |
| write | back to summary |
|---|---|
| public void write(DataOutputStream out) throws IOException Writes all the contents of the attributes. The binary representation
of the contents is an array of | |
| Modifier and Type | Field and Description |
|---|---|
| protected int | |
| pack-priv ByteStream | |
| protected int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int[] | Returns: an array of indexes indicatingCONSTANT_Class_infos.Makes |
| public int | Returns: the index of the added entry.the JVM-internal representation of a class name.
e.g. jvmname)java/lang/Object.Adds a new |
| public int | Returns: the index of the added entry.name_indexAdds a new |
| public int | Returns: the index of the added entry.Adds a new |
| public int | Returns: the index of the added entry.bootstrap_method_attr_index.name_and_type_index.Adds a new |
| public int | Returns: the index of the added entry.class_indexname_and_type_index.Adds a new |
| public int | Returns: the index of the added entry.Adds a new |
| public int | Returns: the index of the added entry.Adds a new |
| public int | Returns: the index of the added entry.class_indexname_and_type_index.Adds a new |
| public int | Returns: the index of the added entry.bootstrap_method_attr_index.name_and_type_index.Adds a new |
| public int | |
| public int | Returns: the index of the added entry.reference_kind
such as .reference_index.Adds a new |
| public int | Returns: the index of the added entry.class_indexname_and_type_index.Adds a new |
| public int | Returns: the index of the added entry.descriptor_index.Adds a new |
| public int | Returns: the index of the added entry.name_indexdescriptor_indexAdds a new |
| public int | Returns: the index of the added entry.name_indexdescriptor_indexAdds a new |
| public int | Returns: the index of the added entry.Adds a new |
| public int | Returns: the index of the added entry.Adds a new |
| pack-priv void |
| num | back to summary |
|---|---|
| protected int num | |
| output | back to summary |
|---|---|
| pack-priv ByteStream output | |
| startPos | back to summary |
|---|---|
| protected int startPos | |
| ConstPoolWriter | back to summary |
|---|---|
| pack-priv ConstPoolWriter(ByteStream out) | |
| addClassInfo | back to summary |
|---|---|
| public int[] addClassInfo(String[] classNames) Makes
| |
| addClassInfo | back to summary |
|---|---|
| public int addClassInfo(String jvmname) Adds a new This also adds a
| |
| addClassInfo | back to summary |
|---|---|
| public int addClassInfo(int name) Adds a new
| |
| addDoubleInfo | back to summary |
|---|---|
| public int addDoubleInfo(double d) Adds a new
| |
| addDynamicInfo | back to summary |
|---|---|
| public int addDynamicInfo(int bootstrap, int nameAndTypeInfo) Adds a new
| |
| addFieldrefInfo | back to summary |
|---|---|
| public int addFieldrefInfo(int classInfo, int nameAndTypeInfo) Adds a new
| |
| addFloatInfo | back to summary |
|---|---|
| public int addFloatInfo(float f) Adds a new
| |
| addIntegerInfo | back to summary |
|---|---|
| public int addIntegerInfo(int i) Adds a new
| |
| addInterfaceMethodrefInfo | back to summary |
|---|---|
| public int addInterfaceMethodrefInfo(int classInfo, int nameAndTypeInfo) Adds a new
| |
| addInvokeDynamicInfo | back to summary |
|---|---|
| public int addInvokeDynamicInfo(int bootstrap, int nameAndTypeInfo) Adds a new
| |
| addLongInfo | back to summary |
|---|---|
| public int addLongInfo(long l) Adds a new
| |
| addMethodHandleInfo | back to summary |
|---|---|
| public int addMethodHandleInfo(int kind, int index) Adds a new
| |
| addMethodrefInfo | back to summary |
|---|---|
| public int addMethodrefInfo(int classInfo, int nameAndTypeInfo) Adds a new
| |
| addMethodTypeInfo | back to summary |
|---|---|
| public int addMethodTypeInfo(int desc) Adds a new
| |
| addNameAndTypeInfo | back to summary |
|---|---|
| public int addNameAndTypeInfo(String name, String type) Adds a new | |
| addNameAndTypeInfo | back to summary |
|---|---|
| public int addNameAndTypeInfo(int name, int type) Adds a new
| |
| addStringInfo | back to summary |
|---|---|
| public int addStringInfo(String str) Adds a new This also adds a new
| |
| addUtf8Info | back to summary |
|---|---|
| public int addUtf8Info(String utf8) Adds a new
| |
| end | back to summary |
|---|---|
| pack-priv void end() Writes the contents of this class pool. | |
| Modifier and Type | Field and Description |
|---|---|
| protected ClassFileWriter. | |
| private int | |
| protected ByteStream |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | add(int
access flags. accessFlags, String the field name. name, String the field type. descriptor, ClassFileWriter.the attributes of the field. may be null. awAdds a new field. |
| public void | add(int
access flags. accessFlags, int the field name. an index indicating its name, int CONSTANT_Utf8_info.the field type. an index indicating its descriptor, ClassFileWriter.CONSTANT_Utf8_info.the attributes of the field. may be null. awAdds a new field. |
| pack-priv int | |
| pack-priv int | |
| pack-priv void |
| constPool | back to summary |
|---|---|
| protected ClassFileWriter. | |
| fieldCount | back to summary |
|---|---|
| private int fieldCount | |
| output | back to summary |
|---|---|
| protected ByteStream output | |
| FieldWriter | back to summary |
|---|---|
| pack-priv FieldWriter(ClassFileWriter. | |
| add | back to summary |
|---|---|
| public void add(int accessFlags, String name, String descriptor, ClassFileWriter. Adds a new field.
| |
| add | back to summary |
|---|---|
| public void add(int accessFlags, int name, int descriptor, ClassFileWriter. Adds a new field.
| |
| dataSize | back to summary |
|---|---|
| pack-priv int dataSize() | |
| size | back to summary |
|---|---|
| pack-priv int size() | |
| write | back to summary |
|---|---|
| pack-priv void write(OutputStream out) throws IOException Writes the added fields. | |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private int | |
| protected int | |
| protected ClassFileWriter. | |
| private boolean | |
| private int | |
| protected ByteStream | |
| protected int | |
| private int | |
| protected int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| public void | addCatch(int startPc, int endPc, int handlerPc, int
an index indicating a catchType)CONSTANT_Class_info.Appends an |
| public void | |
| public void | begin(int
access flags. accessFlags, String the method name. name, String the method signature. descriptor, String[] throws clause. It may be null.
The class names must be the JVM-internal
representations like exceptions, ClassFileWriter.java/lang/Exception.attributes to the awMethod_info.Starts Adding a new method. |
| public void | begin(int
access flags. accessFlags, int the method name. an index indicating its name, int CONSTANT_Utf8_info.the field type. an index indicating its descriptor, int[] CONSTANT_Utf8_info.throws clause. indexes indicating exceptions, ClassFileWriter.CONSTANT_Class_infos.
It may be null.attributes to the awMethod_info.Starts adding a new method. |
| public void | |
| pack-priv int | |
| public void | end(StackMapTable.
a stack map table. may be null. smap,attributes to the awCode_attribute.
may be null.Ends adding a new method. |
| pack-priv int | |
| public int | |
| pack-priv void | |
| private void |
| catchCount | back to summary |
|---|---|
| private int catchCount | |
| catchPos | back to summary |
|---|---|
| private int catchPos | |
| codeIndex | back to summary |
|---|---|
| protected int codeIndex | |
| constPool | back to summary |
|---|---|
| protected ClassFileWriter. | |
| isAbstract | back to summary |
|---|---|
| private boolean isAbstract | |
| methodCount | back to summary |
|---|---|
| private int methodCount | |
| output | back to summary |
|---|---|
| protected ByteStream output | |
| stackIndex | back to summary |
|---|---|
| protected int stackIndex | |
| startPos | back to summary |
|---|---|
| private int startPos | |
| throwsIndex | back to summary |
|---|---|
| protected int throwsIndex | |
| MethodWriter | back to summary |
|---|---|
| pack-priv MethodWriter(ClassFileWriter. | |
| add | back to summary |
|---|---|
| public void add(int b) Appends an 8bit value of bytecode.
| |
| add16 | back to summary |
|---|---|
| public void add16(int b) Appends a 16bit value of bytecode. | |
| add32 | back to summary |
|---|---|
| public void add32(int b) Appends a 32bit value of bytecode. | |
| addCatch | back to summary |
|---|---|
| public void addCatch(int startPc, int endPc, int handlerPc, int catchType) Appends an
| |
| addInvoke | back to summary |
|---|---|
| public void addInvoke(int opcode, String targetClass, String methodName, String descriptor) Appends a invokevirtual, inovkespecial, or invokestatic bytecode.
| |
| begin | back to summary |
|---|---|
| public void begin(int accessFlags, String name, String descriptor, String[] exceptions, ClassFileWriter. Starts Adding a new method.
| |
| begin | back to summary |
|---|---|
| public void begin(int accessFlags, int name, int descriptor, int[] exceptions, ClassFileWriter. Starts adding a new method.
| |
| codeEnd | back to summary |
|---|---|
| public void codeEnd(int maxStack, int maxLocals) Ends appending bytecode. | |
| dataSize | back to summary |
|---|---|
| pack-priv int dataSize() | |
| end | back to summary |
|---|---|
| public void end(StackMapTable. Ends adding a new method. The
| |
| numOfMethods | back to summary |
|---|---|
| pack-priv int numOfMethods() | |
| size | back to summary |
|---|---|
| public int size() Returns the length of the bytecode that has been added so far.
| |
| write | back to summary |
|---|---|
| pack-priv void write(OutputStream out) throws IOException Writes the added methods. | |
| writeThrows | back to summary |
|---|---|
| private void writeThrows(int[] exceptions) | |