org.mozilla.classfile

Class ClassFileWriter

public class ClassFileWriter extends Object

ClassFileWriter A ClassFileWriter is used to write a Java class file. Methods are provided to create fields and methods, and within methods to write Java bytecodes.

Author: Roger Lawrence

Field Summary
static shortACC_ABSTRACT
static shortACC_FINAL
static shortACC_NATIVE
static shortACC_PRIVATE
static shortACC_PROTECTED
static shortACC_PUBLIC
static shortACC_STATIC
static shortACC_SYNCHRONIZED
static shortACC_TRANSIENT
static shortACC_VOLATILE
Constructor Summary
ClassFileWriter(String className, String superClassName, String sourceFileName)
Construct a ClassFileWriter for a class.
Method Summary
intacquireLabel()
voidadd(int theOpCode)
Add the single-byte opcode to the current method.
voidadd(int theOpCode, int theOperand)
Add a single-operand opcode to the current method.
voidadd(int theOpCode, int theOperand1, int theOperand2)
Add the given two-operand bytecode to the current method.
voidadd(int theOpCode, String className)
voidadd(int theOpCode, String className, String fieldName, String fieldType)
voidaddALoad(int local)
Load object from the given local into stack.
voidaddAStore(int local)
Store object from stack top into the given local.
voidaddDLoad(int local)
Load double from the given local into stack.
voidaddDStore(int local)
Store double from stack top into the given local.
voidaddExceptionHandler(int startLabel, int endLabel, int handlerLabel, String catchClassName)
voidaddField(String fieldName, String type, short flags)
Add a field to the class.
voidaddField(String fieldName, String type, short flags, int value)
Add a field to the class.
voidaddField(String fieldName, String type, short flags, long value)
Add a field to the class.
voidaddField(String fieldName, String type, short flags, double value)
Add a field to the class.
voidaddFLoad(int local)
Load float from the given local into stack.
voidaddFStore(int local)
Store float from stack top into the given local.
voidaddILoad(int local)
Load integer from the given local into stack.
voidaddInterface(String interfaceName)
Add an interface implemented by this class.
voidaddInvoke(int theOpCode, String className, String methodName, String methodType)
voidaddIStore(int local)
Store integer from stack top into the given local.
voidaddLineNumberEntry(short lineNumber)
voidaddLLoad(int local)
Load long from the given local into stack.
voidaddLoadConstant(int k)
Generate the load constant bytecode for the given integer.
voidaddLoadConstant(long k)
Generate the load constant bytecode for the given long.
voidaddLoadConstant(float k)
Generate the load constant bytecode for the given float.
voidaddLoadConstant(double k)
Generate the load constant bytecode for the given double.
voidaddLoadConstant(String k)
Generate the load constant bytecode for the given string.
voidaddLoadThis()
Load "this" into stack.
voidaddLStore(int local)
Store long from stack top into the given local.
voidaddPush(int k)
Generate code to load the given integer on stack.
voidaddPush(boolean k)
voidaddPush(long k)
Generate code to load the given long on stack.
voidaddPush(double k)
Generate code to load the given double on stack.
voidaddPush(String k)
Generate the code to leave on stack the given string even if the string encoding exeeds the class file limit for single string constant
intaddTableSwitch(int low, int high)
voidaddVariableDescriptor(String name, String type, int startPC, int register)
Add Information about java variable to use when generating the local variable table.
voidadjustStackTop(int delta)
static StringclassNameToSignature(String name)
Convert Java class name in dot notation into "Lname-with-dots-replaced-by-slashes;" form suitable for use as JVM type signatures.
StringgetClassName()
intgetCurrentCodeOffset()
Get the current offset into the code of the current method.
shortgetStackTop()
booleanisUnderStringSizeLimit(String k)
Check if k fits limit on string constant size imposed by class file format.
voidmarkHandler(int theLabel)
voidmarkLabel(int label)
voidmarkLabel(int label, short stackTop)
voidmarkTableSwitchCase(int switchStart, int caseIndex)
voidmarkTableSwitchCase(int switchStart, int caseIndex, int stackTop)
voidmarkTableSwitchDefault(int switchStart)
voidsetFlags(short flags)
Set the class's flags.
voidsetTableSwitchJump(int switchStart, int caseIndex, int jumpTarget)
voidstartMethod(String methodName, String type, short flags)
Add a method and begin adding code.
voidstopMethod(short maxLocals)
Complete generation of the method.
byte[]toByteArray()
Get the class file as array of bytesto the OutputStream.
voidwrite(OutputStream oStream)
Write the class file to the OutputStream.

Field Detail

ACC_ABSTRACT

public static final short ACC_ABSTRACT

ACC_FINAL

public static final short ACC_FINAL

ACC_NATIVE

public static final short ACC_NATIVE

ACC_PRIVATE

public static final short ACC_PRIVATE

ACC_PROTECTED

public static final short ACC_PROTECTED

ACC_PUBLIC

public static final short ACC_PUBLIC

ACC_STATIC

public static final short ACC_STATIC

ACC_SYNCHRONIZED

public static final short ACC_SYNCHRONIZED

ACC_TRANSIENT

public static final short ACC_TRANSIENT

ACC_VOLATILE

public static final short ACC_VOLATILE

Constructor Detail

ClassFileWriter

public ClassFileWriter(String className, String superClassName, String sourceFileName)
Construct a ClassFileWriter for a class.

Parameters: className the name of the class to write, including full package qualification. superClassName the name of the superclass of the class to write, including full package qualification. sourceFileName the name of the source file to use for producing debug information, or null if debug information is not desired

Method Detail

acquireLabel

public int acquireLabel()

add

public void add(int theOpCode)
Add the single-byte opcode to the current method.

Parameters: theOpCode the opcode of the bytecode

add

public void add(int theOpCode, int theOperand)
Add a single-operand opcode to the current method.

Parameters: theOpCode the opcode of the bytecode theOperand the operand of the bytecode

add

public void add(int theOpCode, int theOperand1, int theOperand2)
Add the given two-operand bytecode to the current method.

Parameters: theOpCode the opcode of the bytecode theOperand1 the first operand of the bytecode theOperand2 the second operand of the bytecode

add

public void add(int theOpCode, String className)

add

public void add(int theOpCode, String className, String fieldName, String fieldType)

addALoad

public void addALoad(int local)
Load object from the given local into stack.

Parameters: local number of local register

addAStore

public void addAStore(int local)
Store object from stack top into the given local.

Parameters: local number of local register

addDLoad

public void addDLoad(int local)
Load double from the given local into stack.

Parameters: local number of local register

addDStore

public void addDStore(int local)
Store double from stack top into the given local.

Parameters: local number of local register

addExceptionHandler

public void addExceptionHandler(int startLabel, int endLabel, int handlerLabel, String catchClassName)

addField

public void addField(String fieldName, String type, short flags)
Add a field to the class.

Parameters: fieldName the name of the field type the type of the field using ... flags the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together

addField

public void addField(String fieldName, String type, short flags, int value)
Add a field to the class.

Parameters: fieldName the name of the field type the type of the field using ... flags the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together value an initial integral value

addField

public void addField(String fieldName, String type, short flags, long value)
Add a field to the class.

Parameters: fieldName the name of the field type the type of the field using ... flags the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together value an initial long value

addField

public void addField(String fieldName, String type, short flags, double value)
Add a field to the class.

Parameters: fieldName the name of the field type the type of the field using ... flags the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together value an initial double value

addFLoad

public void addFLoad(int local)
Load float from the given local into stack.

Parameters: local number of local register

addFStore

public void addFStore(int local)
Store float from stack top into the given local.

Parameters: local number of local register

addILoad

public void addILoad(int local)
Load integer from the given local into stack.

Parameters: local number of local register

addInterface

public void addInterface(String interfaceName)
Add an interface implemented by this class. This method may be called multiple times for classes that implement multiple interfaces.

Parameters: interfaceName a name of an interface implemented by the class being written, including full package qualification.

addInvoke

public void addInvoke(int theOpCode, String className, String methodName, String methodType)

addIStore

public void addIStore(int local)
Store integer from stack top into the given local.

Parameters: local number of local register

addLineNumberEntry

public void addLineNumberEntry(short lineNumber)

addLLoad

public void addLLoad(int local)
Load long from the given local into stack.

Parameters: local number of local register

addLoadConstant

public void addLoadConstant(int k)
Generate the load constant bytecode for the given integer.

Parameters: k the constant

addLoadConstant

public void addLoadConstant(long k)
Generate the load constant bytecode for the given long.

Parameters: k the constant

addLoadConstant

public void addLoadConstant(float k)
Generate the load constant bytecode for the given float.

Parameters: k the constant

addLoadConstant

public void addLoadConstant(double k)
Generate the load constant bytecode for the given double.

Parameters: k the constant

addLoadConstant

public void addLoadConstant(String k)
Generate the load constant bytecode for the given string.

Parameters: k the constant

addLoadThis

public void addLoadThis()
Load "this" into stack.

addLStore

public void addLStore(int local)
Store long from stack top into the given local.

Parameters: local number of local register

addPush

public void addPush(int k)
Generate code to load the given integer on stack.

Parameters: k the constant

addPush

public void addPush(boolean k)

addPush

public void addPush(long k)
Generate code to load the given long on stack.

Parameters: k the constant

addPush

public void addPush(double k)
Generate code to load the given double on stack.

Parameters: k the constant

addPush

public void addPush(String k)
Generate the code to leave on stack the given string even if the string encoding exeeds the class file limit for single string constant

Parameters: k the constant

addTableSwitch

public int addTableSwitch(int low, int high)

addVariableDescriptor

public void addVariableDescriptor(String name, String type, int startPC, int register)
Add Information about java variable to use when generating the local variable table.

Parameters: name variable name. type variable type as bytecode descriptor string. startPC the starting bytecode PC where this variable is live, or -1 if it does not have a Java register. register the Java register number of variable or -1 if it does not have a Java register.

adjustStackTop

public void adjustStackTop(int delta)

classNameToSignature

public static String classNameToSignature(String name)
Convert Java class name in dot notation into "Lname-with-dots-replaced-by-slashes;" form suitable for use as JVM type signatures.

getClassName

public final String getClassName()

getCurrentCodeOffset

public int getCurrentCodeOffset()
Get the current offset into the code of the current method.

Returns: an integer representing the offset

getStackTop

public short getStackTop()

isUnderStringSizeLimit

public boolean isUnderStringSizeLimit(String k)
Check if k fits limit on string constant size imposed by class file format.

Parameters: k the string constant

markHandler

public void markHandler(int theLabel)

markLabel

public void markLabel(int label)

markLabel

public void markLabel(int label, short stackTop)

markTableSwitchCase

public final void markTableSwitchCase(int switchStart, int caseIndex)

markTableSwitchCase

public final void markTableSwitchCase(int switchStart, int caseIndex, int stackTop)

markTableSwitchDefault

public final void markTableSwitchDefault(int switchStart)

setFlags

public void setFlags(short flags)
Set the class's flags. Flags must be a set of the following flags, bitwise or'd together: ACC_PUBLIC ACC_PRIVATE ACC_PROTECTED ACC_FINAL ACC_ABSTRACT TODO: check that this is the appropriate set

Parameters: flags the set of class flags to set

setTableSwitchJump

public void setTableSwitchJump(int switchStart, int caseIndex, int jumpTarget)

startMethod

public void startMethod(String methodName, String type, short flags)
Add a method and begin adding code. This method must be called before other methods for adding code, exception tables, etc. can be invoked.

Parameters: methodName the name of the method type a string representing the type flags the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together

stopMethod

public void stopMethod(short maxLocals)
Complete generation of the method. After this method is called, no more code can be added to the method begun with startMethod.

Parameters: maxLocals the maximum number of local variable slots (a.k.a. Java registers) used by the method vars the array of the variables for the method, or null if none

toByteArray

public byte[] toByteArray()
Get the class file as array of bytesto the OutputStream.

write

public void write(OutputStream oStream)
Write the class file to the OutputStream.

Parameters: oStream the stream to write to

Throws: IOException if writing to the stream produces an exception