org.mozilla.classfile
Class ClassFileWriter

java.lang.Object
  |
  +--org.mozilla.javascript.LabelTable
        |
        +--org.mozilla.classfile.ClassFileWriter

public class ClassFileWriter
extends LabelTable

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 short ACC_ABSTRACT
           
static short ACC_FINAL
           
static short ACC_NATIVE
           
static short ACC_PRIVATE
           
static short ACC_PROTECTED
           
static short ACC_PUBLIC
           
static short ACC_STATIC
           
static short ACC_SYNCHRONIZED
           
static short ACC_TRANSIENT
           
static short ACC_VOLATILE
           
 
Constructor Summary
ClassFileWriter(java.lang.String className, java.lang.String superClassName, java.lang.String sourceFileName)
          Construct a ClassFileWriter for a class.
 
Method Summary
 void add(byte theOpCode)
          Add the single-byte opcode to the current method.
 void add(byte theOpCode, int theOperand)
          Add a single-operand opcode to the current method.
 void add(byte theOpCode, int theOperand1, int theOperand2)
          Add the given two-operand bytecode to the current method.
 void add(byte theOpCode, java.lang.String className)
           
 void add(byte theOpCode, java.lang.String className, java.lang.String fieldName, java.lang.String fieldType)
           
 void add(byte theOpCode, java.lang.String className, java.lang.String methodName, java.lang.String parametersType, java.lang.String returnType)
           
 void addExceptionHandler(int startLabel, int endLabel, int handlerLabel, java.lang.String catchClassName)
           
 void addField(java.lang.String fieldName, java.lang.String type, short flags)
          Add a field to the class.
 void addField(java.lang.String fieldName, java.lang.String type, short flags, double value)
          Add a field to the class.
 void addField(java.lang.String fieldName, java.lang.String type, short flags, int value)
          Add a field to the class.
 void addField(java.lang.String fieldName, java.lang.String type, short flags, long value)
          Add a field to the class.
 void addInterface(java.lang.String interfaceName)
          Add an interface implemented by this class.
 void addLineNumberEntry(short lineNumber)
           
 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 addLoadConstant(java.lang.String k)
          Generate the load constant bytecode for the given string.
 void addToCodeBuffer(byte b)
           
 void adjustStackTop(int delta)
           
static java.lang.String fullyQualifiedForm(java.lang.String name)
           
 int getCurrentCodeOffset()
          Get the current offset into the code of the current method.
 short getStackTop()
           
 int markHandler(int theLabel)
           
 int markLabel(int theLabel)
           
 int markLabel(int theLabel, short stackTop)
           
 void setFlags(short flags)
          Set the class's flags.
 void startMethod(java.lang.String methodName, java.lang.String type, short flags)
          Add a method and begin adding code.
 void stopMethod(short maxLocals, VariableTable vars)
          Complete generation of the method.
 void write(java.io.OutputStream oStream)
          Write the class file to the OutputStream.
 
Methods inherited from class org.mozilla.javascript.LabelTable
acquireLabel, markLabel
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACC_PUBLIC

public static final short ACC_PUBLIC

ACC_PRIVATE

public static final short ACC_PRIVATE

ACC_PROTECTED

public static final short ACC_PROTECTED

ACC_STATIC

public static final short ACC_STATIC

ACC_FINAL

public static final short ACC_FINAL

ACC_SYNCHRONIZED

public static final short ACC_SYNCHRONIZED

ACC_VOLATILE

public static final short ACC_VOLATILE

ACC_TRANSIENT

public static final short ACC_TRANSIENT

ACC_NATIVE

public static final short ACC_NATIVE

ACC_ABSTRACT

public static final short ACC_ABSTRACT
Constructor Detail

ClassFileWriter

public 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

addInterface

public 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.

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

fullyQualifiedForm

public static java.lang.String fullyQualifiedForm(java.lang.String name)

addField

public 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

addField

public 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

addField

public 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

addField

public 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

startMethod

public 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

stopMethod

public void stopMethod(short maxLocals,
                       VariableTable vars)
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 VariableTable of the variables for the method, or null if none

add

public void add(byte theOpCode)
Add the single-byte opcode to the current method.
Parameters:
theOpCode - the opcode of the bytecode

add

public void add(byte 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

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(java.lang.String k)
Generate the load constant bytecode for the given string.
Parameters:
k - the constant

add

public void add(byte 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(byte theOpCode,
                java.lang.String className)

add

public void add(byte theOpCode,
                java.lang.String className,
                java.lang.String fieldName,
                java.lang.String fieldType)

add

public void add(byte theOpCode,
                java.lang.String className,
                java.lang.String methodName,
                java.lang.String parametersType,
                java.lang.String returnType)

markLabel

public int markLabel(int theLabel)

markLabel

public int markLabel(int theLabel,
                     short stackTop)

markHandler

public int markHandler(int theLabel)

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()

adjustStackTop

public void adjustStackTop(int delta)

addToCodeBuffer

public void addToCodeBuffer(byte b)

addExceptionHandler

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

addLineNumberEntry

public void addLineNumberEntry(short lineNumber)

write

public void write(java.io.OutputStream oStream)
           throws java.io.IOException
Write 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