Package org.mozilla.classfile
Class ClassFileWriter
- java.lang.Object
- 
- org.mozilla.classfile.ClassFileWriter
 
- 
 public class ClassFileWriter extends java.lang.ObjectClassFileWriterA ClassFileWriter is used to write a Java class file. Methods are provided to create fields and methods, and within methods to write Java bytecodes. 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classClassFileWriter.ClassFileFormatExceptionThrown for cases where the error in generating the class file is due to a program size constraints rather than a likely bug in the compiler.static classClassFileWriter.MHandle
 - 
Field SummaryFields Modifier and Type Field Description static shortACC_ABSTRACTstatic shortACC_FINALstatic shortACC_NATIVEstatic shortACC_PRIVATEstatic shortACC_PROTECTEDstatic shortACC_PUBLICstatic shortACC_STATICstatic shortACC_SUPERstatic shortACC_SYNCHRONIZEDstatic shortACC_TRANSIENTstatic shortACC_VOLATILE
 - 
Constructor SummaryConstructors Constructor Description ClassFileWriter(java.lang.String className, java.lang.String superClassName, java.lang.String sourceFileName)Construct a ClassFileWriter for a class.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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, java.lang.String className)voidadd(int theOpCode, java.lang.String className, java.lang.String fieldName, java.lang.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, java.lang.String catchClassName)voidaddField(java.lang.String fieldName, java.lang.String type, short flags)Add a field to the class.voidaddField(java.lang.String fieldName, java.lang.String type, short flags, double value)Add a field to the class.voidaddField(java.lang.String fieldName, java.lang.String type, short flags, int value)Add a field to the class.voidaddField(java.lang.String fieldName, java.lang.String type, short flags, long 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(java.lang.String interfaceName)Add an interface implemented by this class.voidaddInvoke(int theOpCode, java.lang.String className, java.lang.String methodName, java.lang.String methodType)voidaddInvokeDynamic(java.lang.String methodName, java.lang.String methodType, ClassFileWriter.MHandle bsm, java.lang.Object... bsmArgs)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(double k)Generate the load constant bytecode for the given double.voidaddLoadConstant(float k)Generate the load constant bytecode for the given float.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(java.lang.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(boolean k)voidaddPush(double k)Generate code to load the given double on stack.voidaddPush(int k)Generate code to load the given integer on stack.voidaddPush(long k)Generate code to load the given long on stack.voidaddPush(java.lang.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 constantintaddTableSwitch(int low, int high)voidaddVariableDescriptor(java.lang.String name, java.lang.String type, int startPC, int register)Add Information about java variable to use when generating the local variable table.voidadjustStackTop(int delta)static java.lang.StringclassNameToSignature(java.lang.String name)Convert Java class name in dot notation into "Lname-with-dots-replaced-by-slashes;" form suitable for use as JVM type signatures.java.lang.StringgetClassName()intgetCurrentCodeOffset()Get the current offset into the code of the current method.intgetLabelPC(int label)shortgetStackTop()booleanisUnderStringSizeLimit(java.lang.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.voidsetStackTop(short n)voidsetTableSwitchJump(int switchStart, int caseIndex, int jumpTarget)Set a jump case for a tableswitch instruction.voidstartMethod(java.lang.String methodName, java.lang.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(java.io.OutputStream oStream)Write the class file to the OutputStream.
 
- 
- 
- 
Field Detail- 
ACC_PUBLICpublic static final short ACC_PUBLIC - See Also:
- Constant Field Values
 
 - 
ACC_PRIVATEpublic static final short ACC_PRIVATE - See Also:
- Constant Field Values
 
 - 
ACC_PROTECTEDpublic static final short ACC_PROTECTED - See Also:
- Constant Field Values
 
 - 
ACC_STATICpublic static final short ACC_STATIC - See Also:
- Constant Field Values
 
 - 
ACC_FINALpublic static final short ACC_FINAL - See Also:
- Constant Field Values
 
 - 
ACC_SUPERpublic static final short ACC_SUPER - See Also:
- Constant Field Values
 
 - 
ACC_SYNCHRONIZEDpublic static final short ACC_SYNCHRONIZED - See Also:
- Constant Field Values
 
 - 
ACC_VOLATILEpublic static final short ACC_VOLATILE - See Also:
- Constant Field Values
 
 - 
ACC_TRANSIENTpublic static final short ACC_TRANSIENT - See Also:
- Constant Field Values
 
 - 
ACC_NATIVEpublic static final short ACC_NATIVE - See Also:
- Constant Field Values
 
 - 
ACC_ABSTRACTpublic static final short ACC_ABSTRACT - See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
ClassFileWriterpublic ClassFileWriter(java.lang.String className, java.lang.String superClassName, java.lang.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- 
getClassNamepublic final java.lang.String getClassName() 
 - 
addInterfacepublic void addInterface(java.lang.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.
 
 - 
setFlagspublic 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
 
 - 
classNameToSignaturepublic static java.lang.String classNameToSignature(java.lang.String name) Convert Java class name in dot notation into "Lname-with-dots-replaced-by-slashes;" form suitable for use as JVM type signatures.
 - 
addFieldpublic void addField(java.lang.String fieldName, java.lang.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
 
 - 
addFieldpublic void addField(java.lang.String fieldName, java.lang.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
 
 - 
addFieldpublic void addField(java.lang.String fieldName, java.lang.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
 
 - 
addFieldpublic void addField(java.lang.String fieldName, java.lang.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
 
 - 
addVariableDescriptorpublic void addVariableDescriptor(java.lang.String name, java.lang.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.
 
 - 
startMethodpublic void startMethod(java.lang.String methodName, java.lang.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
 
 - 
stopMethodpublic 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
 
 - 
addpublic void add(int theOpCode) Add the single-byte opcode to the current method.- Parameters:
- theOpCode- the opcode of the bytecode
 
 - 
addpublic 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
 
 - 
addLoadConstantpublic void addLoadConstant(int k) Generate the load constant bytecode for the given integer.- Parameters:
- k- the constant
 
 - 
addLoadConstantpublic void addLoadConstant(long k) Generate the load constant bytecode for the given long.- Parameters:
- k- the constant
 
 - 
addLoadConstantpublic void addLoadConstant(float k) Generate the load constant bytecode for the given float.- Parameters:
- k- the constant
 
 - 
addLoadConstantpublic void addLoadConstant(double k) Generate the load constant bytecode for the given double.- Parameters:
- k- the constant
 
 - 
addLoadConstantpublic void addLoadConstant(java.lang.String k) Generate the load constant bytecode for the given string.- Parameters:
- k- the constant
 
 - 
addpublic 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
 
 - 
addpublic void add(int theOpCode, java.lang.String className)
 - 
addpublic void add(int theOpCode, java.lang.String className, java.lang.String fieldName, java.lang.String fieldType)
 - 
addInvokepublic void addInvoke(int theOpCode, java.lang.String className, java.lang.String methodName, java.lang.String methodType)
 - 
addInvokeDynamicpublic void addInvokeDynamic(java.lang.String methodName, java.lang.String methodType, ClassFileWriter.MHandle bsm, java.lang.Object... bsmArgs)
 - 
addPushpublic void addPush(int k) Generate code to load the given integer on stack.- Parameters:
- k- the constant
 
 - 
addPushpublic void addPush(boolean k) 
 - 
addPushpublic void addPush(long k) Generate code to load the given long on stack.- Parameters:
- k- the constant
 
 - 
addPushpublic void addPush(double k) Generate code to load the given double on stack.- Parameters:
- k- the constant
 
 - 
addPushpublic void addPush(java.lang.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
 
 - 
isUnderStringSizeLimitpublic boolean isUnderStringSizeLimit(java.lang.String k) Check if k fits limit on string constant size imposed by class file format.- Parameters:
- k- the string constant
 
 - 
addIStorepublic void addIStore(int local) Store integer from stack top into the given local.- Parameters:
- local- number of local register
 
 - 
addLStorepublic void addLStore(int local) Store long from stack top into the given local.- Parameters:
- local- number of local register
 
 - 
addFStorepublic void addFStore(int local) Store float from stack top into the given local.- Parameters:
- local- number of local register
 
 - 
addDStorepublic void addDStore(int local) Store double from stack top into the given local.- Parameters:
- local- number of local register
 
 - 
addAStorepublic void addAStore(int local) Store object from stack top into the given local.- Parameters:
- local- number of local register
 
 - 
addILoadpublic void addILoad(int local) Load integer from the given local into stack.- Parameters:
- local- number of local register
 
 - 
addLLoadpublic void addLLoad(int local) Load long from the given local into stack.- Parameters:
- local- number of local register
 
 - 
addFLoadpublic void addFLoad(int local) Load float from the given local into stack.- Parameters:
- local- number of local register
 
 - 
addDLoadpublic void addDLoad(int local) Load double from the given local into stack.- Parameters:
- local- number of local register
 
 - 
addALoadpublic void addALoad(int local) Load object from the given local into stack.- Parameters:
- local- number of local register
 
 - 
addLoadThispublic void addLoadThis() Load "this" into stack.
 - 
addTableSwitchpublic int addTableSwitch(int low, int high)
 - 
markTableSwitchDefaultpublic final void markTableSwitchDefault(int switchStart) 
 - 
markTableSwitchCasepublic final void markTableSwitchCase(int switchStart, int caseIndex)
 - 
markTableSwitchCasepublic final void markTableSwitchCase(int switchStart, int caseIndex, int stackTop)
 - 
setTableSwitchJumppublic void setTableSwitchJump(int switchStart, int caseIndex, int jumpTarget)Set a jump case for a tableswitch instruction. The jump target should be marked as a super block start for stack map generation.
 - 
acquireLabelpublic int acquireLabel() 
 - 
markLabelpublic void markLabel(int label) 
 - 
markLabelpublic void markLabel(int label, short stackTop)
 - 
markHandlerpublic void markHandler(int theLabel) 
 - 
getLabelPCpublic int getLabelPC(int label) 
 - 
getCurrentCodeOffsetpublic int getCurrentCodeOffset() Get the current offset into the code of the current method.- Returns:
- an integer representing the offset
 
 - 
getStackToppublic short getStackTop() 
 - 
setStackToppublic void setStackTop(short n) 
 - 
adjustStackToppublic void adjustStackTop(int delta) 
 - 
addExceptionHandlerpublic void addExceptionHandler(int startLabel, int endLabel, int handlerLabel, java.lang.String catchClassName)
 - 
addLineNumberEntrypublic void addLineNumberEntry(short lineNumber) 
 - 
writepublic void write(java.io.OutputStream oStream) throws java.io.IOExceptionWrite the class file to the OutputStream.- Parameters:
- oStream- the stream to write to
- Throws:
- java.io.IOException- if writing to the stream produces an exception
 
 - 
toByteArraypublic byte[] toByteArray() Get the class file as array of bytesto the OutputStream.
 
- 
 
-