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. Use stripAttributes() if you don't like this.
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.
addCodeAttribute
public void addCodeAttribute(Attribute a)
Add an attribute to the code. Currently, the JVM knows about the
LineNumberTable, LocalVariableTable and StackMap attributes,
where the former two will be generated automatically and the
latter is used for the MIDP only. Other attributes will be
ignored by the JVM but do no harm.
a
- attribute to be added
addException
public void addException(String class_name)
Add an exception possibly thrown by this method.
class_name
- (fully qualified) name of exception
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.
start_pc
- Start of region (inclusive)end_pc
- End of region (inclusive)handler_pc
- Where handling is donecatch_type
- class type of handled exception or null if any
exception is handled
- new exception handler object
addLineNumber
public LineNumberGen addLineNumber(InstructionHandle ih,
int src_line)
Give an instruction a line number corresponding to the source code line.
addLocalVariable
public LocalVariableGen addLocalVariable(String name,
Type type,
int slot,
InstructionHandle start,
InstructionHandle end)
Adds a local variable to this method.
name
- variable nametype
- variable typeslot
- the index of the local variable, if type is long or double, the next available
index is slot+2start
- from where the variable is validend
- until where the variable is valid
- new local variable object
addLocalVariable
public LocalVariableGen addLocalVariable(String name,
Type type,
InstructionHandle start,
InstructionHandle end)
Adds a local variable to this method and assigns an index automatically.
name
- variable nametype
- variable typestart
- from where the variable is valid, if this is null,
it is valid from the startend
- until where the variable is valid, if this is null,
it is valid to the end
- new local variable object
addObserver
public void addObserver(MethodObserver o)
Add observer for this object.
getArgumentName
public String getArgumentName(int i)
getArgumentNames
public String[] getArgumentNames()
getArgumentType
public Type getArgumentType(int i)
getArgumentTypes
public Type[] getArgumentTypes()
getClassName
public String getClassName()
- class that contains this method
getCodeAttributes
public Attribute[] getCodeAttributes()
- all attributes of this method.
getExceptions
public String[] getExceptions()
getMaxLocals
public int getMaxLocals()
getMaxStack
public int getMaxStack()
getMethod
public Method getMethod()
Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively,
before calling this method (the same applies for max locals).
getReturnType
public Type getReturnType()
removeCodeAttribute
public void removeCodeAttribute(Attribute a)
Remove a code attribute.
removeCodeAttributes
public void removeCodeAttributes()
Remove all code attributes.
removeException
public void removeException(String c)
Remove an exception.
removeExceptionHandler
public void removeExceptionHandler(CodeExceptionGen c)
Remove an exception handler.
removeExceptionHandlers
public void removeExceptionHandlers()
Remove all line numbers.
removeExceptions
public void removeExceptions()
Remove all exceptions.
removeLineNumber
public void removeLineNumber(LineNumberGen l)
Remove a line number.
removeLineNumbers
public void removeLineNumbers()
Remove all line numbers.
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 index argument.
removeLocalVariables
public void removeLocalVariables()
Remove all local variables.
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.
removeObserver
public void removeObserver(MethodObserver o)
Remove observer for this object.
setArgumentName
public void setArgumentName(int i,
String name)
setArgumentNames
public void setArgumentNames(String[] arg_names)
setArgumentType
public void setArgumentType(int i,
Type type)
setArgumentTypes
public void setArgumentTypes(Type[] arg_types)
setClassName
public void setClassName(String class_name)
setMaxLocals
public void setMaxLocals()
Compute maximum number of local variables.
setMaxLocals
public void setMaxLocals(int m)
Set maximum number of local variables.
setMaxStack
public void setMaxStack()
Computes max. stack size by performing control flow analysis.
setMaxStack
public void setMaxStack(int m)
Set maximum stack size for this method.
setReturnType
public void setReturnType(Type return_type)
stripAttributes
public void stripAttributes(boolean flag)
Do not/Do produce attributes code attributesLineNumberTable and
LocalVariableTable, like javac -O
toString
public final String toString()
Return string representation close to declaration format,
`public static void main(String[]) throws IOException', e.g.
- String representation of the method.
update
public void update()
Call notify() method on all observers. This method is not called
automatically whenever the state has changed, but has to be
called by the user after he has finished editing the object.