Top Description Inners Fields Constructors Methods
javassist.bytecode

public Class ClassFileWriter

extends Object
Class Inheritance
Imports
java.io.DataOutputStream, .IOException, .OutputStream

A quick class-file writer. This is useful when a generated class file is simple and the code generation should be fast.

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; }
}
Since
3.13

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ClassFileWriter.AttributeWriter

This writes attributes.

public static class
public static class
public static class

Field Summary

Modifier and TypeField and Description
private ClassFileWriter.ConstPoolWriter
private ClassFileWriter.FieldWriter
private ClassFileWriter.MethodWriter
private ByteStream
pack-priv int
pack-priv int

Constructor Summary

AccessConstructor and Description
public
ClassFileWriter(int
the major version (ClassFile#JAVA_4, ClassFile#JAVA_5, ...).
major
,
int
the minor version (0 for JDK 1.3 and later).
minor
)

Constructs a class file writer.

Method Summary

Modifier and TypeMethod and Description
public byte[]
end(int
access flags.
accessFlags
,
int
this class. an index indicating its CONSTANT_Class_info.
thisClass
,
int
super class. an index indicating its CONSTANT_Class_info.
superClass
,
int[]
implemented interfaces. index numbers indicating their ClassInfo. It may be null.
interfaces
,
ClassFileWriter.AttributeWriter
attributes of the class file. May be null.
aw
)

Ends 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 CONSTANT_Class_info.
thisClass
,
int
super class. an index indicating its CONSTANT_Class_info.
superClass
,
int[]
implemented interfaces. index numbers indicating their CONSTATNT_Class_info. It may be null.
interfaces
,
ClassFileWriter.AttributeWriter
attributes of the class file. May be null.
aw
)

Ends writing and writes the contents of the class file into the given output stream.

public ClassFileWriter.ConstPoolWriter
getConstPool()

Returns a constant pool.

public ClassFileWriter.FieldWriter
getFieldWriter()

Returns a filed writer.

public ClassFileWriter.MethodWriter
getMethodWriter()

Returns a method writer.

pack-priv static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

constPoolback to summary
private ClassFileWriter.ConstPoolWriter constPool
fieldsback to summary
private ClassFileWriter.FieldWriter fields
methodsback to summary
private ClassFileWriter.MethodWriter methods
outputback to summary
private ByteStream output
superClassback to summary
pack-priv int superClass
thisClassback to summary
pack-priv int thisClass

Constructor Detail

ClassFileWriterback to summary
public ClassFileWriter(int major, int minor)

Constructs a class file writer.

Parameters
major:int

the major version (ClassFile#JAVA_4, ClassFile#JAVA_5, ...).

minor:int

the minor version (0 for JDK 1.3 and later).

Method Detail

endback to summary
public byte[] end(int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter.AttributeWriter aw)

Ends writing and returns the contents of the class file.

Parameters
accessFlags:int

access flags.

thisClass:int

this class. an index indicating its CONSTANT_Class_info.

superClass:int

super class. an index indicating its CONSTANT_Class_info.

interfaces:int[]

implemented interfaces. index numbers indicating their ClassInfo. It may be null.

aw:ClassFileWriter.AttributeWriter

attributes of the class file. May be null.

See Also
AccessFlag
endback to summary
public void end(DataOutputStream out, int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter.AttributeWriter aw) throws IOException

Ends writing and writes the contents of the class file into the given output stream.

Parameters
accessFlags:int

access flags.

thisClass:int

this class. an index indicating its CONSTANT_Class_info.

superClass:int

super class. an index indicating its CONSTANT_Class_info.

interfaces:int[]

implemented interfaces. index numbers indicating their CONSTATNT_Class_info. It may be null.

aw:ClassFileWriter.AttributeWriter

attributes of the class file. May be null.

See Also
AccessFlag
getConstPoolback to summary
public ClassFileWriter.ConstPoolWriter getConstPool()

Returns a constant pool.

getFieldWriterback to summary
public ClassFileWriter.FieldWriter getFieldWriter()

Returns a filed writer.

getMethodWriterback to summary
public ClassFileWriter.MethodWriter getMethodWriter()

Returns a method writer.

writeAttributeback to summary
pack-priv static void writeAttribute(ByteStream bs, ClassFileWriter.AttributeWriter aw, int attrCount)
javassist.bytecode back to summary

public Interface ClassFileWriter.AttributeWriter


This writes attributes.

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);
    }
};

Method Summary

Modifier and TypeMethod and Description
public int
size()

Returns the number of attributes that this writer will write.

public void
write(DataOutputStream out)

Writes all the contents of the attributes.

Method Detail

sizeback to summary
public int size()

Returns the number of attributes that this writer will write.

writeback 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 attribute_info.

javassist.bytecode back to summary

public final Class ClassFileWriter.ConstPoolWriter

extends Object
Class Inheritance

Constant Pool.

Field Summary

Modifier and TypeField and Description
protected int
pack-priv ByteStream
protected int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int[]

Returns:

an array of indexes indicating CONSTANT_Class_infos.
addClassInfo
(String[] classNames)

Makes CONSTANT_Class_info objects for each class name.

public int

Returns:

the index of the added entry.
addClassInfo
(String
the JVM-internal representation of a class name. e.g. java/lang/Object.
jvmname
)

Adds a new CONSTANT_Class_info structure.

public int

Returns:

the index of the added entry.
addClassInfo
(int
name_index
name
)

Adds a new CONSTANT_Class_info structure.

public int

Returns:

the index of the added entry.
addDoubleInfo
(double d)

Adds a new CONSTANT_Double_info structure.

public int

Returns:

the index of the added entry.
addDynamicInfo
(int
bootstrap_method_attr_index.
bootstrap
,
int
name_and_type_index.
nameAndTypeInfo
)

Adds a new CONSTANT_Dynamic_info structure.

public int

Returns:

the index of the added entry.
addFieldrefInfo
(int
class_index
classInfo
,
int
name_and_type_index.
nameAndTypeInfo
)

Adds a new CONSTANT_Fieldref_info structure.

public int

Returns:

the index of the added entry.
addFloatInfo
(float f)

Adds a new CONSTANT_Float_info structure.

public int

Returns:

the index of the added entry.
addIntegerInfo
(int i)

Adds a new CONSTANT_Integer_info structure.

public int

Returns:

the index of the added entry.
addInterfaceMethodrefInfo
(int
class_index
classInfo
,
int
name_and_type_index.
nameAndTypeInfo
)

Adds a new CONSTANT_InterfaceMethodref_info structure.

public int

Returns:

the index of the added entry.
addInvokeDynamicInfo
(int
bootstrap_method_attr_index.
bootstrap
,
int
name_and_type_index.
nameAndTypeInfo
)

Adds a new CONSTANT_InvokeDynamic_info structure.

public int

Returns:

the index of the added entry.
addLongInfo
(long l)

Adds a new CONSTANT_Long_info structure.

public int

Returns:

the index of the added entry.
addMethodHandleInfo
(int
reference_kind such as REF_invokeStatic.
kind
,
int
reference_index.
index
)

Adds a new CONSTANT_MethodHandle_info structure.

public int

Returns:

the index of the added entry.
addMethodrefInfo
(int
class_index
classInfo
,
int
name_and_type_index.
nameAndTypeInfo
)

Adds a new CONSTANT_Methodref_info structure.

public int

Returns:

the index of the added entry.
addMethodTypeInfo
(int
descriptor_index.
desc
)

Adds a new CONSTANT_MethodType_info structure.

public int

Returns:

the index of the added entry.
addNameAndTypeInfo
(String
name_index
name
,
String
descriptor_index
type
)

Adds a new CONSTANT_NameAndType_info structure.

public int

Returns:

the index of the added entry.
addNameAndTypeInfo
(int
name_index
name
,
int
descriptor_index
type
)

Adds a new CONSTANT_NameAndType_info structure.

public int

Returns:

the index of the added entry.
addStringInfo
(String str)

Adds a new CONSTANT_String_info structure.

public int

Returns:

the index of the added entry.
addUtf8Info
(String utf8)

Adds a new CONSTANT_Utf8_info structure.

pack-priv void
end()

Writes the contents of this class pool.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

numback to summary
protected int num
outputback to summary
pack-priv ByteStream output
startPosback to summary
protected int startPos

Constructor Detail

ConstPoolWriterback to summary
pack-priv ConstPoolWriter(ByteStream out)

Method Detail

addClassInfoback to summary
public int[] addClassInfo(String[] classNames)

Makes CONSTANT_Class_info objects for each class name.

Returns:int[]

an array of indexes indicating CONSTANT_Class_infos.

addClassInfoback to summary
public int addClassInfo(String jvmname)

Adds a new CONSTANT_Class_info structure.

This also adds a CONSTANT_Utf8_info structure for storing the class name.

Parameters
jvmname:String

the JVM-internal representation of a class name. e.g. java/lang/Object.

Returns:int

the index of the added entry.

addClassInfoback to summary
public int addClassInfo(int name)

Adds a new CONSTANT_Class_info structure.

Parameters
name:int

name_index

Returns:int

the index of the added entry.

addDoubleInfoback to summary
public int addDoubleInfo(double d)

Adds a new CONSTANT_Double_info structure.

Returns:int

the index of the added entry.

addDynamicInfoback to summary
public int addDynamicInfo(int bootstrap, int nameAndTypeInfo)

Adds a new CONSTANT_Dynamic_info structure.

Parameters
bootstrap:int

bootstrap_method_attr_index.

nameAndTypeInfo:int

name_and_type_index.

Returns:int

the index of the added entry.

Since
3.26
addFieldrefInfoback to summary
public int addFieldrefInfo(int classInfo, int nameAndTypeInfo)

Adds a new CONSTANT_Fieldref_info structure.

Parameters
classInfo:int

class_index

nameAndTypeInfo:int

name_and_type_index.

Returns:int

the index of the added entry.

addFloatInfoback to summary
public int addFloatInfo(float f)

Adds a new CONSTANT_Float_info structure.

Returns:int

the index of the added entry.

addIntegerInfoback to summary
public int addIntegerInfo(int i)

Adds a new CONSTANT_Integer_info structure.

Returns:int

the index of the added entry.

addInterfaceMethodrefInfoback to summary
public int addInterfaceMethodrefInfo(int classInfo, int nameAndTypeInfo)

Adds a new CONSTANT_InterfaceMethodref_info structure.

Parameters
classInfo:int

class_index

nameAndTypeInfo:int

name_and_type_index.

Returns:int

the index of the added entry.

addInvokeDynamicInfoback to summary
public int addInvokeDynamicInfo(int bootstrap, int nameAndTypeInfo)

Adds a new CONSTANT_InvokeDynamic_info structure.

Parameters
bootstrap:int

bootstrap_method_attr_index.

nameAndTypeInfo:int

name_and_type_index.

Returns:int

the index of the added entry.

Since
3.17.1
addLongInfoback to summary
public int addLongInfo(long l)

Adds a new CONSTANT_Long_info structure.

Returns:int

the index of the added entry.

addMethodHandleInfoback to summary
public int addMethodHandleInfo(int kind, int index)

Adds a new CONSTANT_MethodHandle_info structure.

Parameters
kind:int

reference_kind such as REF_invokeStatic.

index:int

reference_index.

Returns:int

the index of the added entry.

Since
3.17.1
addMethodrefInfoback to summary
public int addMethodrefInfo(int classInfo, int nameAndTypeInfo)

Adds a new CONSTANT_Methodref_info structure.

Parameters
classInfo:int

class_index

nameAndTypeInfo:int

name_and_type_index.

Returns:int

the index of the added entry.

addMethodTypeInfoback to summary
public int addMethodTypeInfo(int desc)

Adds a new CONSTANT_MethodType_info structure.

Parameters
desc:int

descriptor_index.

Returns:int

the index of the added entry.

Since
3.17.1
addNameAndTypeInfoback to summary
public int addNameAndTypeInfo(String name, String type)

Adds a new CONSTANT_NameAndType_info structure.

Parameters
name:String

name_index

type:String

descriptor_index

Returns:int

the index of the added entry.

addNameAndTypeInfoback to summary
public int addNameAndTypeInfo(int name, int type)

Adds a new CONSTANT_NameAndType_info structure.

Parameters
name:int

name_index

type:int

descriptor_index

Returns:int

the index of the added entry.

addStringInfoback to summary
public int addStringInfo(String str)

Adds a new CONSTANT_String_info structure.

This also adds a new CONSTANT_Utf8_info structure.

Returns:int

the index of the added entry.

addUtf8Infoback to summary
public int addUtf8Info(String utf8)

Adds a new CONSTANT_Utf8_info structure.

Returns:int

the index of the added entry.

endback to summary
pack-priv void end()

Writes the contents of this class pool.

javassist.bytecode back to summary

public final Class ClassFileWriter.FieldWriter

extends Object
Class Inheritance

Field.

Field Summary

Modifier and TypeField and Description
protected ClassFileWriter.ConstPoolWriter
private int
protected ByteStream

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
add(int
access flags.
accessFlags
,
String
the field name.
name
,
String
the field type.
descriptor
,
ClassFileWriter.AttributeWriter
the attributes of the field. may be null.
aw
)

Adds a new field.

public void
add(int
access flags.
accessFlags
,
int
the field name. an index indicating its CONSTANT_Utf8_info.
name
,
int
the field type. an index indicating its CONSTANT_Utf8_info.
descriptor
,
ClassFileWriter.AttributeWriter
the attributes of the field. may be null.
aw
)

Adds a new field.

pack-priv int
pack-priv int
size()

pack-priv void
write(OutputStream out)

Writes the added fields.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

constPoolback to summary
protected ClassFileWriter.ConstPoolWriter constPool
fieldCountback to summary
private int fieldCount
outputback to summary
protected ByteStream output

Constructor Detail

FieldWriterback to summary
pack-priv FieldWriter(ClassFileWriter.ConstPoolWriter cp)

Method Detail

addback to summary
public void add(int accessFlags, String name, String descriptor, ClassFileWriter.AttributeWriter aw)

Adds a new field.

Parameters
accessFlags:int

access flags.

name:String

the field name.

descriptor:String

the field type.

aw:ClassFileWriter.AttributeWriter

the attributes of the field. may be null.

See Also
AccessFlag
addback to summary
public void add(int accessFlags, int name, int descriptor, ClassFileWriter.AttributeWriter aw)

Adds a new field.

Parameters
accessFlags:int

access flags.

name:int

the field name. an index indicating its CONSTANT_Utf8_info.

descriptor:int

the field type. an index indicating its CONSTANT_Utf8_info.

aw:ClassFileWriter.AttributeWriter

the attributes of the field. may be null.

See Also
AccessFlag
dataSizeback to summary
pack-priv int dataSize()
sizeback to summary
pack-priv int size()
writeback to summary
pack-priv void write(OutputStream out) throws IOException

Writes the added fields.

javassist.bytecode back to summary

public final Class ClassFileWriter.MethodWriter

extends Object
Class Inheritance

Method.

Field Summary

Modifier and TypeField and Description
private int
private int
protected int
protected ClassFileWriter.ConstPoolWriter
private boolean
private int
protected ByteStream
protected int
private int
protected int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

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

Appends an 8bit value of bytecode.

public void
add16(int b)

Appends a 16bit value of bytecode.

public void
add32(int b)

Appends a 32bit value of bytecode.

public void
addCatch(int startPc, int endPc, int handlerPc, int
an index indicating a CONSTANT_Class_info.
catchType
)

Appends an exception_table entry to the Code_attribute.

public void
addInvoke(int opcode, String targetClass, String methodName, String descriptor)

Appends a invokevirtual, inovkespecial, or invokestatic bytecode.

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 java/lang/Exception.
exceptions
,
ClassFileWriter.AttributeWriter
attributes to the Method_info.
aw
)

Starts Adding a new method.

public void
begin(int
access flags.
accessFlags
,
int
the method name. an index indicating its CONSTANT_Utf8_info.
name
,
int
the field type. an index indicating its CONSTANT_Utf8_info.
descriptor
,
int[]
throws clause. indexes indicating CONSTANT_Class_infos. It may be null.
exceptions
,
ClassFileWriter.AttributeWriter
attributes to the Method_info.
aw
)

Starts adding a new method.

public void
codeEnd(int maxStack, int maxLocals)

Ends appending bytecode.

pack-priv int
public void
end(StackMapTable.Writer
a stack map table. may be null.
smap
,
ClassFileWriter.AttributeWriter
attributes to the Code_attribute. may be null.
aw
)

Ends adding a new method.

pack-priv int
public int

Returns:

the length in bytes.
size
()

Returns the length of the bytecode that has been added so far.

pack-priv void
write(OutputStream out)

Writes the added methods.

private void
writeThrows(int[] exceptions)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

catchCountback to summary
private int catchCount
catchPosback to summary
private int catchPos
codeIndexback to summary
protected int codeIndex
constPoolback to summary
protected ClassFileWriter.ConstPoolWriter constPool
isAbstractback to summary
private boolean isAbstract
methodCountback to summary
private int methodCount
outputback to summary
protected ByteStream output
stackIndexback to summary
protected int stackIndex
startPosback to summary
private int startPos
throwsIndexback to summary
protected int throwsIndex

Constructor Detail

MethodWriterback to summary
pack-priv MethodWriter(ClassFileWriter.ConstPoolWriter cp)

Method Detail

addback to summary
public void add(int b)

Appends an 8bit value of bytecode.

See Also
Opcode
add16back to summary
public void add16(int b)

Appends a 16bit value of bytecode.

add32back to summary
public void add32(int b)

Appends a 32bit value of bytecode.

addCatchback to summary
public void addCatch(int startPc, int endPc, int handlerPc, int catchType)

Appends an exception_table entry to the Code_attribute. This method is available only after the codeEnd method is called.

Parameters
catchType:int

an index indicating a CONSTANT_Class_info.

addInvokeback to summary
public void addInvoke(int opcode, String targetClass, String methodName, String descriptor)

Appends a invokevirtual, inovkespecial, or invokestatic bytecode.

See Also
Opcode
beginback to summary
public void begin(int accessFlags, String name, String descriptor, String[] exceptions, ClassFileWriter.AttributeWriter aw)

Starts Adding a new method.

Parameters
accessFlags:int

access flags.

name:String

the method name.

descriptor:String

the method signature.

exceptions:String[]

throws clause. It may be null. The class names must be the JVM-internal representations like java/lang/Exception.

aw:ClassFileWriter.AttributeWriter

attributes to the Method_info.

beginback to summary
public void begin(int accessFlags, int name, int descriptor, int[] exceptions, ClassFileWriter.AttributeWriter aw)

Starts adding a new method.

Parameters
accessFlags:int

access flags.

name:int

the method name. an index indicating its CONSTANT_Utf8_info.

descriptor:int

the field type. an index indicating its CONSTANT_Utf8_info.

exceptions:int[]

throws clause. indexes indicating CONSTANT_Class_infos. It may be null.

aw:ClassFileWriter.AttributeWriter

attributes to the Method_info.

codeEndback to summary
public void codeEnd(int maxStack, int maxLocals)

Ends appending bytecode.

dataSizeback to summary
pack-priv int dataSize()
endback to summary
public void end(StackMapTable.Writer smap, ClassFileWriter.AttributeWriter aw)

Ends adding a new method. The add method must be called before the end method is called.

Parameters
smap:StackMapTable.Writer

a stack map table. may be null.

aw:ClassFileWriter.AttributeWriter

attributes to the Code_attribute. may be null.

numOfMethodsback to summary
pack-priv int numOfMethods()
sizeback to summary
public int size()

Returns the length of the bytecode that has been added so far.

Returns:int

the length in bytes.

Since
3.19
writeback to summary
pack-priv void write(OutputStream out) throws IOException

Writes the added methods.

writeThrowsback to summary
private void writeThrows(int[] exceptions)