org.apache.bcel.generic

Class MethodGen

Implemented Interfaces:
Cloneable, NamedAndTyped, java.io.Serializable

public class MethodGen
extends FieldGenOrMethodGen

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.
Version:
$Id: MethodGen.java 386056 2006-03-15 11:31:56Z tcurdt $
Authors:
M. Dahm
Patrick C. Beard [setMaxStack()]
See Also:
InstructionList, Method, Serialized Form

Nested Class Summary

(package private) static class
MethodGen.BranchStack
(package private) static class
MethodGen.BranchTarget

Field Summary

private static BCELComparator
_cmp
private String[]
arg_names
private Type[]
arg_types
private String
class_name
private List
code_attrs_vec
private List
exception_vec
private InstructionList
il
private List
line_number_vec
private int
max_locals
private int
max_stack
private List
observers
private boolean
strip_attributes
private List
throws_vec
private List
variable_vec

Fields inherited from class org.apache.bcel.generic.FieldGenOrMethodGen

attribute_vec, cp, name, type

Fields inherited from class org.apache.bcel.classfile.AccessFlags

access_flags

Constructor Summary

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.
MethodGen(Method m, String class_name, ConstantPoolGen cp)
Instantiate from existing method.

Method Summary

void
addCodeAttribute(Attribute a)
Add an attribute to the code.
void
addException(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.
LineNumberGen
addLineNumber(InstructionHandle ih, int src_line)
Give an instruction a line number corresponding to the source code line.
LocalVariableGen
addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
Adds a local variable to this method.
LocalVariableGen
addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end)
Adds a local variable to this method and assigns an index automatically.
void
addObserver(MethodObserver o)
Add observer for this object.
MethodGen
copy(String class_name, ConstantPoolGen cp)
boolean
equals(Object obj)
Return value as defined by given BCELComparator strategy.
String
getArgumentName(int i)
String[]
getArgumentNames()
Type
getArgumentType(int i)
Type[]
getArgumentTypes()
String
getClassName()
Attribute[]
getCodeAttributes()
private CodeException[]
getCodeExceptions()
static BCELComparator
getComparator()
CodeExceptionGen[]
getExceptionHandlers()
private ExceptionTable
getExceptionTable(ConstantPoolGen cp)
String[]
getExceptions()
InstructionList
getInstructionList()
LineNumberTable
getLineNumberTable(ConstantPoolGen cp)
LineNumberGen[]
getLineNumbers()
LocalVariableTable
getLocalVariableTable(ConstantPoolGen cp)
LocalVariableGen[]
getLocalVariables()
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.
Type
getReturnType()
String
getSignature()
int
hashCode()
Return value as defined by given BCELComparator strategy.
void
removeCodeAttribute(Attribute a)
Remove a code attribute.
void
removeCodeAttributes()
Remove all code attributes.
void
removeException(String c)
Remove an exception.
void
removeExceptionHandler(CodeExceptionGen c)
Remove an exception handler.
void
removeExceptionHandlers()
Remove all line numbers.
void
removeExceptions()
Remove all exceptions.
void
removeLineNumber(LineNumberGen l)
Remove a line number.
void
removeLineNumbers()
Remove all line numbers.
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.
void
removeLocalVariables()
Remove all local variables.
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
removeObserver(MethodObserver o)
Remove observer for this object.
void
setArgumentName(int i, String name)
void
setArgumentNames(String[] arg_names)
void
setArgumentType(int i, Type type)
void
setArgumentTypes(Type[] arg_types)
void
setClassName(String class_name)
static void
setComparator(BCELComparator comparator)
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
setReturnType(Type return_type)
private static void
sort(LocalVariableGen[] vars, int l, int r)
Sort local variables by index
void
stripAttributes(boolean flag)
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O
String
toString()
Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g.
void
update()
Call notify() method on all observers.

Methods inherited from class org.apache.bcel.generic.FieldGenOrMethodGen

addAttribute, clone, getAttributes, getConstantPool, getName, getSignature, getType, removeAttribute, removeAttributes, setConstantPool, setName, setType

Methods inherited from class org.apache.bcel.classfile.AccessFlags

getAccessFlags, getModifiers, isAbstract, isAbstract, isAnnotation, isAnnotation, isEnum, isEnum, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isSynthetic, isSynthetic, isTransient, isTransient, isVolatile, isVolatile, setAccessFlags, setFlag, setModifiers

Field Details

_cmp

private static BCELComparator _cmp

arg_names

private String[] arg_names

arg_types

private Type[] arg_types

class_name

private String class_name

code_attrs_vec

private List code_attrs_vec

exception_vec

private List exception_vec

il

private InstructionList il

line_number_vec

private List line_number_vec

max_locals

private int max_locals

max_stack

private int max_stack

observers

private List observers

strip_attributes

private boolean strip_attributes

throws_vec

private List throws_vec

variable_vec

private List variable_vec

Constructor Details

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

Method Details

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.
Parameters:
a - attribute to be added

addException

public void addException(String class_name)
Add an exception possibly thrown by this method.
Parameters:
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.
Parameters:
start_pc - Start of region (inclusive)
end_pc - End of region (inclusive)
handler_pc - Where handling is done
catch_type - class type of handled exception or null if any exception is handled
Returns:
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.
Parameters:
ih - instruction to tag
Returns:
new line number object
See Also:
LineNumber

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

addObserver

public void addObserver(MethodObserver o)
Add observer for this object.

copy

public MethodGen copy(String class_name,
                      ConstantPoolGen cp)
Returns:
deep copy of this method

equals

public boolean equals(Object obj)
Return value as defined by given BCELComparator strategy. By default two MethodGen objects are said to be equal when their names and signatures are equal.
See Also:
java.lang.Object.equals(java.lang.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()
Returns:
class that contains this method

getCodeAttributes

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

getCodeExceptions

private CodeException[] getCodeExceptions()
Returns:
code exceptions for `Code' attribute

getComparator

public static BCELComparator getComparator()
Returns:
Comparison strategy object

getExceptionHandlers

public CodeExceptionGen[] getExceptionHandlers()

getExceptionTable

private ExceptionTable getExceptionTable(ConstantPoolGen cp)
Returns:
`Exceptions' attribute of all the exceptions thrown by this method.

getExceptions

public String[] getExceptions()

getInstructionList

public InstructionList getInstructionList()

getLineNumberTable

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

getLineNumbers

public LineNumberGen[] getLineNumbers()

getLocalVariableTable

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

getLocalVariables

public LocalVariableGen[] getLocalVariables()

getMaxLocals

public int getMaxLocals()

getMaxStack

public int getMaxStack()

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

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).
Returns:
method object

getReturnType

public Type getReturnType()

getSignature

public String getSignature()
Overrides:
getSignature in interface FieldGenOrMethodGen

hashCode

public int hashCode()
Return value as defined by given BCELComparator strategy. By default return the hashcode of the method's name XOR signature.
See Also:
java.lang.Object.hashCode()

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)

setComparator

public static void setComparator(BCELComparator comparator)
Parameters:
comparator - Comparison strategy object

setInstructionList

public void setInstructionList(InstructionList il)

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)

sort

private static final void sort(LocalVariableGen[] vars,
                               int l,
                               int r)
Sort local variables by index

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.
Returns:
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.