All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class de.fub.bytecode.generic.MethodGen

java.lang.Object
   |
   +----de.fub.bytecode.classfile.AccessFlags
           |
           +----de.fub.bytecode.generic.MethodGen

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.17 1999/09/09 07:42:58 dahm Exp $
Author:
M. Dahm, Patrick C. Beard
See Also:
InstructionList, Method

Constructor Index

 o MethodGen(int, Type, Type[], String[], String, String, InstructionList, ConstantPoolGen)
Declare method.
 o MethodGen(Method, String, ConstantPoolGen)
Instantiate from existing method.

Method Index

 o addAttribute(Attribute)
Add an attribute to this method.
 o addCodeAttribute(Attribute)
Add an attribute to the code.
 o addException(String)
Add an exception possibly thrown by this method.
 o addExceptionHandler(InstructionHandle, InstructionHandle, InstructionHandle, ObjectType)
Add an exception handler, i.e.
 o addExceptionHandler(InstructionHandle, InstructionHandle, InstructionHandle, String)
Deprecated.
 o addLineNumber(InstructionHandle, int)
Give an instruction a line number corresponding to the source code line.
 o addLocalVariable(String, Type, InstructionHandle, InstructionHandle)
Adds a local variable to this method and assigns an index automatically.
 o addLocalVariable(String, Type, int, InstructionHandle, InstructionHandle)
Adds a local variable to this method.
 o getArgType(int)
 o getArgTypes()
 o getAttributes()
 o getClassName()
 o getCodeAttributes()
 o getConstantPool()
 o getExceptionHandlers()
 o getExceptions()
 o getInstructionList()
 o getLineNumbers()
 o getLineNumberTable(ConstantPoolGen)
 o getLocalVariables()
 o getLocalVariableTable(ConstantPoolGen)
 o getMaxLocals()
 o getMaxStack()
 o getMaxStack(ConstantPoolGen, InstructionList, CodeExceptionGen[])
Computes stack usage of an instruction list by performing control flow analysis.
 o getMethod()
Get method object.
 o getMethodName()
 o getMethodSignature()
 o getReturnType()
 o removeAttribute(Attribute)
Remove an attribute.
 o removeCodeAttribute(Attribute)
Remove a code attribute.
 o removeException(String)
Remove an exception.
 o removeExceptionHandler(CodeExceptionGen)
Remove an exception handler.
 o removeLineNumber(LineNumberGen)
Remove a line number.
 o removeLocalVariable(LocalVariableGen)
Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit `slot' argument.
 o removeNOPs()
Remove all NOPs from the instruction list (if possible) and update every object refering to them, i.e.
 o setArgType(int, Type)
 o setArgTypes(Type[])
 o setConstantPool(ConstantPoolGen)
 o setInstructionList(InstructionList)
 o setMaxLocals()
Compute maximum number of local variables.
 o setMaxLocals(int)
Set maximum number of local variables.
 o setMaxStack()
Computes max.
 o setMaxStack(int)
Set maximum stack size for this method.
 o setMethodName(String)
 o setReturnType(Type)
 o stripAttributes(boolean)
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O

Constructors

 o MethodGen
 public MethodGen(int access_flags,
                  Type return_type,
                  Type arg_types[],
                  String arg_names[],
                  String method_name,
                  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
 o MethodGen
 public MethodGen(Method m,
                  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)

Methods

 o addLocalVariable
 public LocalVariableGen addLocalVariable(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
 o addLocalVariable
 public LocalVariableGen addLocalVariable(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
 o 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.

 o getLocalVariables
 public LocalVariableGen[] getLocalVariables()
 o getLocalVariableTable
 public LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)
Returns:
`LocalVariableTable' attribute of all the local variables of this method.
 o 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
 o removeLineNumber
 public void removeLineNumber(LineNumberGen l)
Remove a line number.

 o getLineNumbers
 public LineNumberGen[] getLineNumbers()
 o getLineNumberTable
 public LineNumberTable getLineNumberTable(ConstantPoolGen cp)
Returns:
`LineNumberTable' attribute of all the local variables of this method.
 o 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
 o addExceptionHandler
 public CodeExceptionGen addExceptionHandler(InstructionHandle start_pc,
                                             InstructionHandle end_pc,
                                             InstructionHandle handler_pc,
                                             String catch_type)
Note: addExceptionHandler() is deprecated. Use above method

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

 o getExceptionHandlers
 public CodeExceptionGen[] getExceptionHandlers()
 o addException
 public void addException(String class_name)
Add an exception possibly thrown by this method.

Parameters:
class_name - (fully qualified) name of exception
 o removeException
 public void removeException(String c)
Remove an exception.

 o getExceptions
 public String[] getExceptions()
 o 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
 o removeAttribute
 public void removeAttribute(Attribute a)
Remove an attribute.

 o getAttributes
 public Attribute[] getAttributes()
Returns:
all attributes of this method.
 o 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
 o removeCodeAttribute
 public void removeCodeAttribute(Attribute a)
Remove a code attribute.

 o getCodeAttributes
 public Attribute[] getCodeAttributes()
Returns:
all attributes of this method.
 o 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
 o 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.

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

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

 o getMaxLocals
 public int getMaxLocals()
 o getMaxStack
 public int getMaxStack()
 o setMethodName
 public void setMethodName(String method_name)
 o getMethodName
 public String getMethodName()
 o getClassName
 public String getClassName()
 o setReturnType
 public void setReturnType(Type return_type)
 o getReturnType
 public Type getReturnType()
 o setArgTypes
 public void setArgTypes(Type arg_types[])
 o getArgTypes
 public Type[] getArgTypes()
 o setArgType
 public void setArgType(int i,
                        Type type)
 o getArgType
 public Type getArgType(int i)
 o getInstructionList
 public InstructionList getInstructionList()
 o setInstructionList
 public void setInstructionList(InstructionList il)
 o getConstantPool
 public ConstantPoolGen getConstantPool()
 o setConstantPool
 public void setConstantPool(ConstantPoolGen cp)
 o getMethodSignature
 public String getMethodSignature()
 o setMaxStack
 public void setMaxStack()
Computes max. stack size by performing control flow analysis.

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

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

 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

All Packages  Class Hierarchy  This Package  Previous  Next  Index