Package org.mozilla.classfile
Class ClassFileWriter
java.lang.Object
org.mozilla.classfile.ClassFileWriter
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Thrown 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 final class
-
Field Summary
Modifier and TypeFieldDescriptionstatic final short
static final short
static final short
static final short
static final short
static final short
static final short
static final short
static final short
static final short
static final short
-
Constructor Summary
ConstructorDescriptionClassFileWriter
(String className, String superClassName, String sourceFileName) Construct a ClassFileWriter for a class. -
Method Summary
Modifier and TypeMethodDescriptionint
void
add
(int theOpCode) Add the single-byte opcode to the current method.void
add
(int theOpCode, int theOperand) Add a single-operand opcode to the current method.void
add
(int theOpCode, int theOperand1, int theOperand2) Add the given two-operand bytecode to the current method.void
void
void
addALoad
(int local) Load object from the given local into stack.void
addAStore
(int local) Store object from stack top into the given local.void
addDLoad
(int local) Load double from the given local into stack.void
addDStore
(int local) Store double from stack top into the given local.void
addExceptionHandler
(int startLabel, int endLabel, int handlerLabel, String catchClassName) void
Add a field to the class.void
Add a field to the class.void
Add a field to the class.void
Add a field to the class.void
addFLoad
(int local) Load float from the given local into stack.void
addFStore
(int local) Store float from stack top into the given local.void
addILoad
(int local) Load integer from the given local into stack.void
addInterface
(String interfaceName) Add an interface implemented by this class.void
void
addInvokeDynamic
(String methodName, String methodType, ClassFileWriter.MHandle bsm, Object... bsmArgs) void
addIStore
(int local) Store integer from stack top into the given local.void
addLineNumberEntry
(short lineNumber) void
addLLoad
(int local) Load long from the given local into stack.void
addLoadConstant
(double k) Generate the load constant bytecode for the given double.void
addLoadConstant
(float k) Generate the load constant bytecode for the given float.void
addLoadConstant
(int k) Generate the load constant bytecode for the given integer.void
addLoadConstant
(long k) Generate the load constant bytecode for the given long.void
Generate the load constant bytecode for the given string.void
Load "this" into stack.void
addLStore
(int local) Store long from stack top into the given local.void
addPush
(boolean k) void
addPush
(double k) Generate code to load the given double on stack.void
addPush
(int k) Generate code to load the given integer on stack.void
addPush
(long k) Generate code to load the given long on stack.void
Generate the code to leave on stack the given string even if the string encoding exeeds the class file limit for single string constantint
addTableSwitch
(int low, int high) void
addVariableDescriptor
(String name, String type, int startPC, int register) Add Information about java variable to use when generating the local variable table.void
adjustStackTop
(int delta) 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.final String
int
Get the current offset into the code of the current method.int
getLabelPC
(int label) int
boolean
Check if k fits limit on string constant size imposed by class file format.void
markHandler
(int theLabel) void
markLabel
(int label) void
markLabel
(int label, int stackTop) final void
markTableSwitchCase
(int switchStart, int caseIndex) final void
markTableSwitchCase
(int switchStart, int caseIndex, int stackTop) final void
markTableSwitchDefault
(int switchStart) void
setFlags
(short flags) Set the class's flags.void
setStackTop
(short n) void
setTableSwitchJump
(int switchStart, int caseIndex, int jumpTarget) Set a jump case for a tableswitch instruction.void
startMethod
(String methodName, String type, short flags) Add a method and begin adding code.void
stopMethod
(int maxLocals) Complete generation of the method.byte[]
Get the class file as array of bytesto the OutputStream.void
write
(OutputStream oStream) Write the class file to the OutputStream.
-
Field Details
-
ACC_PUBLIC
public static final short ACC_PUBLIC- See Also:
-
ACC_PRIVATE
public static final short ACC_PRIVATE- See Also:
-
ACC_PROTECTED
public static final short ACC_PROTECTED- See Also:
-
ACC_STATIC
public static final short ACC_STATIC- See Also:
-
ACC_FINAL
public static final short ACC_FINAL- See Also:
-
ACC_SUPER
public static final short ACC_SUPER- See Also:
-
ACC_SYNCHRONIZED
public static final short ACC_SYNCHRONIZED- See Also:
-
ACC_VOLATILE
public static final short ACC_VOLATILE- See Also:
-
ACC_TRANSIENT
public static final short ACC_TRANSIENT- See Also:
-
ACC_NATIVE
public static final short ACC_NATIVE- See Also:
-
ACC_ABSTRACT
public static final short ACC_ABSTRACT- See Also:
-
-
Constructor Details
-
ClassFileWriter
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 Details
-
getClassName
-
addInterface
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.
-
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
-
classNameToSignature
Convert Java class name in dot notation into "Lname-with-dots-replaced-by-slashes;" form suitable for use as JVM type signatures. -
addField
Add a field to the class.- Parameters:
fieldName
- the name of the fieldtype
- the type of the field using ...flags
- the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together
-
addField
Add a field to the class.- Parameters:
fieldName
- the name of the fieldtype
- the type of the field using ...flags
- the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd togethervalue
- an initial integral value
-
addField
Add a field to the class.- Parameters:
fieldName
- the name of the fieldtype
- the type of the field using ...flags
- the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd togethervalue
- an initial long value
-
addField
Add a field to the class.- Parameters:
fieldName
- the name of the fieldtype
- the type of the field using ...flags
- the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd togethervalue
- an initial double value
-
addVariableDescriptor
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.
-
startMethod
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 methodtype
- a string representing the typeflags
- the attributes of the field, such as ACC_PUBLIC, etc. bitwise or'd together
-
stopMethod
public void stopMethod(int 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
-
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 bytecodetheOperand
- the operand of the bytecode
-
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
Generate the load constant bytecode for the given string.- Parameters:
k
- the constant
-
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 bytecodetheOperand1
- the first operand of the bytecodetheOperand2
- the second operand of the bytecode
-
add
-
add
-
addInvoke
-
addInvokeDynamic
public void addInvokeDynamic(String methodName, String methodType, ClassFileWriter.MHandle bsm, Object... bsmArgs) -
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
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
-
isUnderStringSizeLimit
Check if k fits limit on string constant size imposed by class file format.- Parameters:
k
- the string constant
-
addIStore
public void addIStore(int local) Store integer from stack top into the given local.- Parameters:
local
- number of local register
-
addLStore
public void addLStore(int local) Store long from stack top into the given local.- 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
-
addDStore
public void addDStore(int local) Store double from stack top into the given local.- 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
-
addILoad
public void addILoad(int local) Load integer from the given local into stack.- Parameters:
local
- number of local register
-
addLLoad
public void addLLoad(int local) Load long from the given local into stack.- Parameters:
local
- number of local register
-
addFLoad
public void addFLoad(int local) Load float from the given local into stack.- 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
-
addALoad
public void addALoad(int local) Load object from the given local into stack.- Parameters:
local
- number of local register
-
addLoadThis
public void addLoadThis()Load "this" into stack. -
addTableSwitch
public int addTableSwitch(int low, int high) -
markTableSwitchDefault
public final void markTableSwitchDefault(int switchStart) -
markTableSwitchCase
public final void markTableSwitchCase(int switchStart, int caseIndex) -
markTableSwitchCase
public final void markTableSwitchCase(int switchStart, int caseIndex, int stackTop) -
setTableSwitchJump
public 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. -
acquireLabel
public int acquireLabel() -
markLabel
public void markLabel(int label) -
markLabel
public void markLabel(int label, int stackTop) -
markHandler
public void markHandler(int theLabel) -
getLabelPC
public int getLabelPC(int label) -
getCurrentCodeOffset
public int getCurrentCodeOffset()Get the current offset into the code of the current method.- Returns:
- an integer representing the offset
-
getStackTop
public int getStackTop() -
setStackTop
public void setStackTop(short n) -
adjustStackTop
public void adjustStackTop(int delta) -
addExceptionHandler
public void addExceptionHandler(int startLabel, int endLabel, int handlerLabel, String catchClassName) -
addLineNumberEntry
public void addLineNumberEntry(short lineNumber) -
write
Write the class file to the OutputStream.- Parameters:
oStream
- the stream to write to- Throws:
IOException
- if writing to the stream produces an exception
-
toByteArray
public byte[] toByteArray()Get the class file as array of bytesto the OutputStream.
-