Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Class DE.fub.inf.JVM.ClassGen.MethodGen

java.lang.Object
  |
  +--DE.fub.inf.JVM.ClassGen.MethodGen

public final class MethodGen
extends java.lang.Object
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.6 1998/10/15 13:17:15 dahm Exp $
Author:
M. Dahm
See Also:
InstructionList, Method

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.
CodeExceptionGen addExceptionHandler(InstructionHandle start_pc, InstructionHandle end_pc, InstructionHandle handler_pc, java.lang.String catch_type)
          Add an exception handler, i.e. specify region where a handler is active and an instruction where the actual handling is done.
void addException(java.lang.String class_name)
          Add an exception possibly thrown by this 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, int slot, InstructionHandle start, InstructionHandle end)
          Add a local variable to this method.
LocalVariableGen addLocalVariable(java.lang.String name, Type type, InstructionHandle start, InstructionHandle end)
          Add a local variable to this method and assign an index automatically.
int getAccessFlags()
           
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()
           
Method getMethod(int max_stack)
          Deprecated. Get method object.
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 removeExceptionHandler(CodeExceptionGen c)
          Remove an exception handler.
void removeException(java.lang.String c)
          Remove an exception.
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 setAccessFlags(short access_flags)
           
void setArgTypes(Type[] arg_types)
           
void setMaxLocals(int m)
          Set maximum stack size for this method.
void setMaxStack(int m)
          Set maximum number of local variables.
void setMethodName(java.lang.String method_name)
           
void setReturnType(Type return_type)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notifyAll, notify, 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
il - instruction list associated with this method, may be null only for abstract 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)
Add 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)
Add a local variable to this method and assign an index automatically.
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, 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 final LocalVariableGen[] getLocalVariables()

getLocalVariableTable

public final 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 final LineNumberGen[] getLineNumbers()

getLineNumberTable

public final 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,
                                            java.lang.String 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 - which exception is handled (fully qualified class name)
Returns:
new exception handler object

removeExceptionHandler

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

getExceptionHandlers

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

getMethod

public final Method getMethod(int max_stack)
Deprecated.  
Get method object. The maximum stack size has to be supplied by the user, since it is hard to determine statically with just the byte code at hands. I.e. you'd have to identify loops and such. The stack size can be computed much easier on the fly when creating the code.
Parameters:
max_stack - maximum stack size of this method
Returns:
method object

getMethod

public Method getMethod()
Get method object. The maximum stack size has to be supplied by the user, since it is hard to determine statically with just the byte code at hands. I.e. you'd have to identify loops and such. The stack size can be computed much easier on the fly when creating the code. So never forget to call setMaxStack() before calling this method.
Returns:
method object

removeNOPs

public final 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 stack size for this method.

setMaxStack

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

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

setAccessFlags

public void setAccessFlags(short access_flags)

getAccessFlags

public int getAccessFlags()

getInstructionList

public InstructionList getInstructionList()

getConstantPool

public ConstantPoolGen getConstantPool()

getMethodSignature

public java.lang.String getMethodSignature()

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD