de.fub.bytecode.generic
Class MethodGen

java.lang.Object
  |
  +--de.fub.bytecode.classfile.AccessFlags
        |
        +--de.fub.bytecode.generic.MethodGen
All Implemented Interfaces:
Constants

public class MethodGen
extends AccessFlags
implements Constants

Template class for building up a method. This is done by defining exception handlers, adding thrown exceptions, local variables and attributes, whereas the `LocalVariableTable' and `LineNumberTable' attributes will be set automatically for the code. While generating code it may be necessary to insert NOP operations. You can use the `removeNOPs' method to get rid off them. The resulting method object can be obtained via the `getMethod()' method.

Version:
$Id: MethodGen.java,v 1.19 2000/08/10 08:36:23 dahm Exp $
Author:
M. Dahm, Patrick C. Beard
See Also:
InstructionList, Method

Fields inherited from class de.fub.bytecode.classfile.AccessFlags
access_flags
 
Fields inherited from interface de.fub.bytecode.Constants
AALOAD, AASTORE, ACC_ABSTRACT, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_SUPER, ACC_SYNCHRONIZED, ACC_TRANSIENT, ACC_VOLATILE, ACCESS_NAMES, ACONST_NULL, ALOAD, ALOAD_0, ALOAD_1, ALOAD_2, ALOAD_3, ANEWARRAY, ANEWARRAY_QUICK, ARETURN, ARRAYLENGTH, ASTORE, ASTORE_0, ASTORE_1, ASTORE_2, ASTORE_3, ATHROW, ATTR_CODE, ATTR_CONSTANT_VALUE, ATTR_DEPRECATED, ATTR_EXCEPTIONS, ATTR_INNER_CLASSES, ATTR_LINE_NUMBER_TABLE, ATTR_LOCAL_VARIABLE_TABLE, ATTR_PMG, ATTR_SIGNATURE, ATTR_SOURCE_FILE, ATTR_SYNTHETIC, ATTR_UNKNOWN, ATTRIBUTE_NAMES, BALOAD, BASTORE, BIPUSH, BREAKPOINT, CALOAD, CASTORE, CHECKCAST, CHECKCAST_QUICK, CONSTANT_Class, CONSTANT_Double, CONSTANT_Fieldref, CONSTANT_Float, CONSTANT_Integer, CONSTANT_InterfaceMethodref, CONSTANT_Long, CONSTANT_Methodref, CONSTANT_NameAndType, CONSTANT_NAMES, CONSTANT_String, CONSTANT_Utf8, CONSUME_STACK, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DLOAD_0, DLOAD_1, DLOAD_2, DLOAD_3, DMUL, DNEG, DREM, DRETURN, DSTORE, DSTORE_0, DSTORE_1, DSTORE_2, DSTORE_3, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAD_0, FLOAD_1, FLOAD_2, FLOAD_3, FMUL, FNEG, FREM, FRETURN, FSTORE, FSTORE_0, FSTORE_1, FSTORE_2, FSTORE_3, FSUB, GETFIELD, GETFIELD_QUICK, GETFIELD_QUICK_W, GETFIELD2_QUICK, GETSTATIC, GETSTATIC_QUICK, GETSTATIC2_QUICK, GOTO, GOTO_W, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILLEGAL_OPCODE, ILLEGAL_TYPE, ILOAD, ILOAD_0, ILOAD_1, ILOAD_2, ILOAD_3, IMPDEP1, IMPDEP2, IMUL, INEG, INSTANCEOF, INSTANCEOF_QUICK, INT2BYTE, INT2CHAR, INT2SHORT, INVOKEINTERFACE, INVOKEINTERFACE_QUICK, INVOKENONVIRTUAL, INVOKENONVIRTUAL_QUICK, INVOKESPECIAL, INVOKESTATIC, INVOKESTATIC_QUICK, INVOKESUPER_QUICK, INVOKEVIRTUAL, INVOKEVIRTUAL_QUICK, INVOKEVIRTUAL_QUICK_W, INVOKEVIRTUALOBJECT_QUICK, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISTORE_0, ISTORE_1, ISTORE_2, ISTORE_3, ISUB, IUSHR, IXOR, JSR, JSR_W, KNOWN_ATTRIBUTES, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDC_QUICK, LDC_W, LDC_W_QUICK, LDC2_W, LDC2_W_QUICK, LDIV, LLOAD, LLOAD_0, LLOAD_1, LLOAD_2, LLOAD_3, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSTORE_0, LSTORE_1, LSTORE_2, LSTORE_3, LSUB, LUSHR, LXOR, MAJOR, MAJOR_1_1, MAJOR_1_2, MAX_ACC_FLAG, MAX_BYTE, MAX_SHORT, MINOR, MINOR_1_1, MINOR_1_2, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, MULTIANEWARRAY_QUICK, NEW, NEW_QUICK, NEWARRAY, NO_OF_OPERANDS, NOP, OPCODE_NAMES, POP, POP2, PRODUCE_STACK, PUSH, PUTFIELD, PUTFIELD_QUICK, PUTFIELD_QUICK_W, PUTFIELD2_QUICK, PUTSTATIC, PUTSTATIC_QUICK, PUTSTATIC2_QUICK, RESERVED, RET, RETURN, SALOAD, SASTORE, SHORT_TYPE_NAMES, SIPUSH, SWAP, SWITCH, T_ARRAY, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_OBJECT, T_REFERENCE, T_SHORT, T_UNKNOWN, T_VOID, TABLESWITCH, TYPE_NAMES, TYPE_OF_OPERANDS, UNDEFINED, UNPREDICTABLE, WIDE
 
Constructor Summary
MethodGen(int access_flags, Type return_type, Type[] arg_types, java.lang.String[] arg_names, java.lang.String method_name, java.lang.String class_name, InstructionList il, ConstantPoolGen cp)
          Declare method.
MethodGen(Method m, java.lang.String class_name, ConstantPoolGen cp)
          Instantiate from existing method.
 
Method Summary
 void addAttribute(Attribute a)
          Add an attribute to this method.
 void addCodeAttribute(Attribute a)
          Add an attribute to the code.
 void addException(java.lang.String class_name)
          Add an exception possibly thrown by this method.
 CodeExceptionGen addExceptionHandler(InstructionHandle start_pc, InstructionHandle end_pc, InstructionHandle handler_pc, ObjectType catch_type)
          Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.
 CodeExceptionGen addExceptionHandler(InstructionHandle start_pc, InstructionHandle end_pc, InstructionHandle handler_pc, java.lang.String catch_type)
          Deprecated. Use above method
 LineNumberGen addLineNumber(InstructionHandle ih, int src_line)
          Give an instruction a line number corresponding to the source code line.
 LocalVariableGen addLocalVariable(java.lang.String name, Type type, InstructionHandle start, InstructionHandle end)
          Adds a local variable to this method and assigns an index automatically.
 LocalVariableGen addLocalVariable(java.lang.String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
          Adds a local variable to this method.
 Type getArgType(int i)
           
 Type[] getArgTypes()
           
 Attribute[] getAttributes()
           
 java.lang.String getClassName()
           
 Attribute[] getCodeAttributes()
           
 ConstantPoolGen getConstantPool()
           
 CodeExceptionGen[] getExceptionHandlers()
           
 java.lang.String[] getExceptions()
           
 InstructionList getInstructionList()
           
 LineNumberGen[] getLineNumbers()
           
 LineNumberTable getLineNumberTable(ConstantPoolGen cp)
           
 LocalVariableGen[] getLocalVariables()
           
 LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)
           
 int getMaxLocals()
           
 int getMaxStack()
           
static int getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et)
          Computes stack usage of an instruction list by performing control flow analysis.
 Method getMethod()
          Get method object.
 java.lang.String getMethodName()
           
 java.lang.String getMethodSignature()
           
 Type getReturnType()
           
 void removeAttribute(Attribute a)
          Remove an attribute.
 void removeCodeAttribute(Attribute a)
          Remove a code attribute.
 void removeException(java.lang.String c)
          Remove an exception.
 void removeExceptionHandler(CodeExceptionGen c)
          Remove an exception handler.
 void removeLineNumber(LineNumberGen l)
          Remove a line number.
 void removeLocalVariable(LocalVariableGen l)
          Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit `slot' argument.
 void removeNOPs()
          Remove all NOPs from the instruction list (if possible) and update every object refering to them, i.e., branch instructions, local variables and exception handlers.
 void setArgType(int i, Type type)
           
 void setArgTypes(Type[] arg_types)
           
 void setConstantPool(ConstantPoolGen cp)
           
 void setInstructionList(InstructionList il)
           
 void setMaxLocals()
          Compute maximum number of local variables.
 void setMaxLocals(int m)
          Set maximum number of local variables.
 void setMaxStack()
          Computes max.
 void setMaxStack(int m)
          Set maximum stack size for this method.
 void setMethodName(java.lang.String method_name)
           
 void setReturnType(Type return_type)
           
 void stripAttributes(boolean flag)
          Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O
 
Methods inherited from class de.fub.bytecode.classfile.AccessFlags
getAccessFlags, isAbstract, isAbstract, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isSynchronized, isSynchronized, isTransient, isTransient, isVolatile, isVolatile, setAccessFlags
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodGen

public MethodGen(int access_flags,
                 Type return_type,
                 Type[] arg_types,
                 java.lang.String[] arg_names,
                 java.lang.String method_name,
                 java.lang.String class_name,
                 InstructionList il,
                 ConstantPoolGen cp)
Declare method. If the method is non-static the constructor automatically declares a local variable `$this' in slot 0. The actual code is contained in the `il' parameter, which may further manipulated by the user. But he must take care not to remove any instruction (handles) that are still referenced from this object. For example one may not add a local variable and later remove the instructions it refers to without causing havoc. It is safe however if you remove that local variable, too.
Parameters:
access_flags - access qualifiers
return_type - method type
arg_types - argument types
arg_names - argument names (if this is null, default names will be provided for them)
method_name - name of method
class_name - class name containing this method (may be null, if you don't care)
il - instruction list associated with this method, may be null only for abstract or native methods
cp - constant pool

MethodGen

public MethodGen(Method m,
                 java.lang.String class_name,
                 ConstantPoolGen cp)
Instantiate from existing method.
Parameters:
m - method
class_name - class name containing this method
cp - constant pool (must contain the same entries as the method's constant pool)
Method Detail

addLocalVariable

public LocalVariableGen addLocalVariable(java.lang.String name,
                                         Type type,
                                         int slot,
                                         InstructionHandle start,
                                         InstructionHandle end)
Adds a local variable to this method.
Parameters:
name - variable name
type - variable type
slot - the index of the local variable, if type is long or double, the next available index is slot+2
start - from where the variable is valid
end - until where the variable is valid
Returns:
new local variable object
See Also:
LocalVariable

addLocalVariable

public LocalVariableGen addLocalVariable(java.lang.String name,
                                         Type type,
                                         InstructionHandle start,
                                         InstructionHandle end)
Adds a local variable to this method and assigns an index automatically.
Parameters:
name - variable name
type - variable type
start - from where the variable is valid, if this is null, it is valid from the start
end - until where the variable is valid, if this is null, it is valid to the end
Returns:
new local variable object
See Also:
LocalVariable

removeLocalVariable

public void removeLocalVariable(LocalVariableGen l)
Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit `slot' argument.

getLocalVariables

public LocalVariableGen[] getLocalVariables()

getLocalVariableTable

public LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)
Returns:
`LocalVariableTable' attribute of all the local variables of this method.

addLineNumber

public LineNumberGen addLineNumber(InstructionHandle ih,
                                   int src_line)
Give an instruction a line number corresponding to the source code line.
Parameters:
ih - instruction to tag
Returns:
new line number object
See Also:
LineNumber

removeLineNumber

public void removeLineNumber(LineNumberGen l)
Remove a line number.

getLineNumbers

public LineNumberGen[] getLineNumbers()

getLineNumberTable

public LineNumberTable getLineNumberTable(ConstantPoolGen cp)
Returns:
`LineNumberTable' attribute of all the local variables of this method.

addExceptionHandler

public CodeExceptionGen addExceptionHandler(InstructionHandle start_pc,
                                            InstructionHandle end_pc,
                                            InstructionHandle handler_pc,
                                            ObjectType catch_type)
Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.
Parameters:
start_pc - Start of region
end_pc - End of region
handler_pc - Where handling is done
catch_type - fully qualified class name of handled exception or null if any exception is handled
Returns:
new exception handler object

addExceptionHandler

public CodeExceptionGen addExceptionHandler(InstructionHandle start_pc,
                                            InstructionHandle end_pc,
                                            InstructionHandle handler_pc,
                                            java.lang.String catch_type)
Deprecated. Use above method


removeExceptionHandler

public void removeExceptionHandler(CodeExceptionGen c)
Remove an exception handler.

getExceptionHandlers

public CodeExceptionGen[] getExceptionHandlers()

addException

public void addException(java.lang.String class_name)
Add an exception possibly thrown by this method.
Parameters:
class_name - (fully qualified) name of exception

removeException

public void removeException(java.lang.String c)
Remove an exception.

getExceptions

public java.lang.String[] getExceptions()

addAttribute

public void addAttribute(Attribute a)
Add an attribute to this method. Currently, the JVM knows about the `Code' and `Exceptions' attribute, which will be generated automatically. Other attributes will be ignored by the JVM but do no harm.
Parameters:
a - attribute to be added

removeAttribute

public void removeAttribute(Attribute a)
Remove an attribute.

getAttributes

public Attribute[] getAttributes()
Returns:
all attributes of this method.

addCodeAttribute

public void addCodeAttribute(Attribute a)
Add an attribute to the code. Currently, the JVM knows about the `LineNumberTable' and `LocalVariableTable' attributes, which will be generated automatically. Other attributes will be ignored by the JVM but do no harm.
Parameters:
a - attribute to be added

removeCodeAttribute

public void removeCodeAttribute(Attribute a)
Remove a code attribute.

getCodeAttributes

public Attribute[] getCodeAttributes()
Returns:
all attributes of this method.

getMethod

public Method getMethod()
Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, before calling this method. This method should be called exactly once when the buildup is finished.
Returns:
method object

removeNOPs

public void removeNOPs()
Remove all NOPs from the instruction list (if possible) and update every object refering to them, i.e., branch instructions, local variables and exception handlers.

setMaxLocals

public void setMaxLocals(int m)
Set maximum number of local variables.

setMaxStack

public void setMaxStack(int m)
Set maximum stack size for this method.

getMaxLocals

public int getMaxLocals()

getMaxStack

public int getMaxStack()

setMethodName

public void setMethodName(java.lang.String method_name)

getMethodName

public java.lang.String getMethodName()

getClassName

public java.lang.String getClassName()

setReturnType

public void setReturnType(Type return_type)

getReturnType

public Type getReturnType()

setArgTypes

public void setArgTypes(Type[] arg_types)

getArgTypes

public Type[] getArgTypes()

setArgType

public void setArgType(int i,
                       Type type)

getArgType

public Type getArgType(int i)

getInstructionList

public InstructionList getInstructionList()

setInstructionList

public void setInstructionList(InstructionList il)

getConstantPool

public ConstantPoolGen getConstantPool()

setConstantPool

public void setConstantPool(ConstantPoolGen cp)

getMethodSignature

public java.lang.String getMethodSignature()

setMaxStack

public void setMaxStack()
Computes max. stack size by performing control flow analysis.

setMaxLocals

public void setMaxLocals()
Compute maximum number of local variables. May be a little bit to large, but who cares ...

stripAttributes

public void stripAttributes(boolean flag)
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O

getMaxStack

public static int getMaxStack(ConstantPoolGen cp,
                              InstructionList il,
                              CodeExceptionGen[] et)
Computes stack usage of an instruction list by performing control flow analysis.
Returns:
maximum stack depth used by method