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
-
MethodGen(int, Type, Type[], String[], String, String, InstructionList, ConstantPoolGen)
- Declare method.
-
MethodGen(Method, String, ConstantPoolGen)
- Instantiate from existing method.
-
addAttribute(Attribute)
- Add an attribute to this method.
-
addCodeAttribute(Attribute)
- Add an attribute to the code.
-
addException(String)
- Add an exception possibly thrown by this method.
-
addExceptionHandler(InstructionHandle, InstructionHandle, InstructionHandle, ObjectType)
- Add an exception handler, i.e.
-
addExceptionHandler(InstructionHandle, InstructionHandle, InstructionHandle, String)
-
Deprecated.
-
addLineNumber(InstructionHandle, int)
- Give an instruction a line number corresponding to the source code line.
-
addLocalVariable(String, Type, InstructionHandle, InstructionHandle)
- Adds a local variable to this method and assigns an index automatically.
-
addLocalVariable(String, Type, int, InstructionHandle, InstructionHandle)
- Adds a local variable to this method.
-
getArgType(int)
-
-
getArgTypes()
-
-
getAttributes()
-
-
getClassName()
-
-
getCodeAttributes()
-
-
getConstantPool()
-
-
getExceptionHandlers()
-
-
getExceptions()
-
-
getInstructionList()
-
-
getLineNumbers()
-
-
getLineNumberTable(ConstantPoolGen)
-
-
getLocalVariables()
-
-
getLocalVariableTable(ConstantPoolGen)
-
-
getMaxLocals()
-
-
getMaxStack()
-
-
getMaxStack(ConstantPoolGen, InstructionList, CodeExceptionGen[])
- Computes stack usage of an instruction list by performing control flow analysis.
-
getMethod()
- Get method object.
-
getMethodName()
-
-
getMethodSignature()
-
-
getReturnType()
-
-
removeAttribute(Attribute)
- Remove an attribute.
-
removeCodeAttribute(Attribute)
- Remove a code attribute.
-
removeException(String)
- Remove an exception.
-
removeExceptionHandler(CodeExceptionGen)
- Remove an exception handler.
-
removeLineNumber(LineNumberGen)
- Remove a line number.
-
removeLocalVariable(LocalVariableGen)
- Remove a local variable, its slot will not be reused, if you do not use addLocalVariable
with an explicit `slot' argument.
-
removeNOPs()
- Remove all NOPs from the instruction list (if possible) and update every
object refering to them, i.e.
-
setArgType(int, Type)
-
-
setArgTypes(Type[])
-
-
setConstantPool(ConstantPoolGen)
-
-
setInstructionList(InstructionList)
-
-
setMaxLocals()
- Compute maximum number of local variables.
-
setMaxLocals(int)
- Set maximum number of local variables.
-
setMaxStack()
- Computes max.
-
setMaxStack(int)
- Set maximum stack size for this method.
-
setMethodName(String)
-
-
setReturnType(Type)
-
-
stripAttributes(boolean)
- Do not/Do produce attributes code attributesLineNumberTable and
LocalVariableTable, like javac -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
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)
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
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
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,
String catch_type)
- Note: addExceptionHandler() is deprecated.
Use above method
removeExceptionHandler
public void removeExceptionHandler(CodeExceptionGen c)
- Remove an exception handler.
getExceptionHandlers
public CodeExceptionGen[] getExceptionHandlers()
addException
public void addException(String class_name)
- Add an exception possibly thrown by this method.
- Parameters:
- class_name - (fully qualified) name of exception
removeException
public void removeException(String c)
- Remove an exception.
getExceptions
public 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(String method_name)
getMethodName
public String getMethodName()
getClassName
public 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 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
All Packages Class Hierarchy This Package Previous Next Index