Package org.apache.bcel.generic
Class MethodGen
java.lang.Object
org.apache.bcel.classfile.AccessFlags
org.apache.bcel.generic.FieldGenOrMethodGen
org.apache.bcel.generic.MethodGen
- All Implemented Interfaces:
Cloneable,NamedAndTyped
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.
- See Also:
-
Field Summary
Fields inherited from class org.apache.bcel.generic.FieldGenOrMethodGen
cp, name, typeFields inherited from class org.apache.bcel.classfile.AccessFlags
access_flags -
Constructor Summary
ConstructorsConstructorDescriptionMethodGen(int accessFlags, Type returnType, Type[] argTypes, String[] argNames, String methodName, String className, InstructionList il, ConstantPoolGen cp) Declare method.MethodGen(Method method, String className, ConstantPoolGen cp) Instantiate from existing method. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidAdd an attribute to the code.voidaddException(String className) Add an exception possibly thrown by this method.addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType) Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.addLineNumber(InstructionHandle ih, int srcLine) Give an instruction a line number corresponding to the source code line.addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end) Adds a local variable to this method.addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end, int origIndex) Adds a local variable to this method.addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end) Adds a local variable to this method and assigns an index automatically.voidAdd observer for this object.voidaddParameterAnnotation(int parameterIndex, AnnotationEntryGen annotation) voidcopy(String className, ConstantPoolGen cp) booleanReturn value as defined by given BCELComparator strategy.getAnnotationsOnParameter(int i) Return a list of AnnotationGen objects representing parameter annotationsgetArgumentName(int i) String[]getArgumentType(int i) Type[]static BCELComparator<FieldGenOrMethodGen>String[]intintstatic intgetMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et) Computes stack usage of an instruction list by performing control flow analysis.Gets method object.inthashCode()Return value as defined by given BCELComparator strategy.voidRemove a code attribute.voidRemove all code attributes.voidRemove an exception.voidRemove an exception handler.voidRemove all line numbers.voidRemove all exceptions.voidRemove a line number.voidRemove all line numbers.voidRemove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument.voidRemove all local variables.voidRemove the LocalVariableTypeTablevoidRemove all NOPs from the instruction list (if possible) and update every object referring to them, i.e., branch instructions, local variables and exception handlers.voidRemove observer for this object.voidremoveRuntimeAttributes(Attribute[] attributes) Would prefer to make this private, but need a way to test if client is using BCEL version 6.5.0 or later that contains fix for BCEL-329.voidsetArgumentName(int i, String name) voidsetArgumentNames(String[] argNames) voidsetArgumentType(int i, Type type) voidsetArgumentTypes(Type[] argTypes) voidsetClassName(String className) static voidsetComparator(BCELComparator<FieldGenOrMethodGen> comparator) voidvoidCompute maximum number of local variables.voidsetMaxLocals(int m) Sets maximum number of local variables.voidComputes max.voidsetMaxStack(int m) Sets maximum stack size for this method.voidsetReturnType(Type returnType) voidstripAttributes(boolean flag) Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -Ofinal StringtoString()Return string representation close to declaration format, 'public static void main(String[]) throws IOException', e.g.voidupdate()Call notify() method on all observers.Methods inherited from class org.apache.bcel.generic.FieldGenOrMethodGen
addAll, addAnnotationEntry, addAttribute, clone, getAnnotationEntries, getAttributes, getConstantPool, getName, getType, removeAnnotationEntries, removeAnnotationEntry, removeAttribute, removeAttributes, setConstantPool, setName, setTypeMethods 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, isVarArgs, isVarArgs, isVolatile, isVolatile, setAccessFlags, setModifiers
-
Constructor Details
-
MethodGen
public MethodGen(int accessFlags, Type returnType, Type[] argTypes, String[] argNames, String methodName, String className, 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 they 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:
accessFlags- access qualifiersreturnType- method typeargTypes- argument typesargNames- argument names (if this is null, default names will be provided for them)methodName- name of methodclassName- 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 methodscp- constant pool
-
MethodGen
Instantiate from existing method.- Parameters:
method- methodclassName- class name containing this methodcp- constant pool
-
-
Method Details
-
getComparator
- Returns:
- Comparison strategy object.
-
getMaxStack
Computes stack usage of an instruction list by performing control flow analysis.- Returns:
- maximum stack depth used by method
-
setComparator
- Parameters:
comparator- Comparison strategy object.
-
addAnnotationsAsAttribute
- Since:
- 6.0
-
addCodeAttribute
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
Add an exception possibly thrown by this method.- Parameters:
className- (fully qualified) name of exception
-
addExceptionHandler
public CodeExceptionGen addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType) Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.- Parameters:
startPc- Start of region (inclusive)endPc- End of region (inclusive)handlerPc- Where handling is donecatchType- class type of handled exception or null if any exception is handled- Returns:
- new exception handler object
-
addLineNumber
Give an instruction a line number corresponding to the source code line.- Parameters:
ih- instruction to tag- Returns:
- new line number object
- See Also:
-
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 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- Returns:
- new local variable object
- See Also:
-
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end) Adds a local variable to this method.- Parameters:
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- Returns:
- new local variable object
- See Also:
-
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end, int origIndex) Adds a local variable to this method.- Parameters:
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 validorigIndex- the index of the local variable prior to any modifications- Returns:
- new local variable object
- See Also:
-
addObserver
Add observer for this object. -
addParameterAnnotation
-
addParameterAnnotationsAsAttribute
- Since:
- 6.0
-
copy
- Returns:
- deep copy of this method
-
equals
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. -
getAnnotationsOnParameter
Return a list of AnnotationGen objects representing parameter annotations- Since:
- 6.0
-
getArgumentName
-
getArgumentNames
-
getArgumentType
-
getArgumentTypes
-
getClassName
- Returns:
- class that contains this method
-
getCodeAttributes
- Returns:
- all attributes of this method.
-
getExceptionHandlers
-
getExceptions
-
getInstructionList
-
getLineNumbers
-
getLineNumberTable
- Returns:
- 'LineNumberTable' attribute of all the local variables of this method.
-
getLocalVariables
-
getLocalVariableTable
- Returns:
- 'LocalVariableTable' attribute of all the local variables of this method.
-
getLocalVariableTypeTable
- Returns:
- 'LocalVariableTypeTable' attribute of this method.
-
getMaxLocals
-
getMaxStack
-
getMethod
Gets 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
-
getSignature
- Specified by:
getSignaturein classFieldGenOrMethodGen- Returns:
- signature of method/field.
-
hashCode
Return value as defined by given BCELComparator strategy. By default return the hash code of the method's name XOR signature. -
removeCodeAttribute
Remove a code attribute. -
removeCodeAttributes
Remove all code attributes. -
removeException
Remove an exception. -
removeExceptionHandler
Remove an exception handler. -
removeExceptionHandlers
Remove all line numbers. -
removeExceptions
Remove all exceptions. -
removeLineNumber
Remove a line number. -
removeLineNumbers
Remove all line numbers. -
removeLocalVariable
Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument. -
removeLocalVariables
Remove all local variables. -
removeLocalVariableTypeTable
Remove the LocalVariableTypeTable -
removeNOPs
Remove all NOPs from the instruction list (if possible) and update every object referring to them, i.e., branch instructions, local variables and exception handlers. -
removeObserver
Remove observer for this object. -
removeRuntimeAttributes
Would prefer to make this private, but need a way to test if client is using BCEL version 6.5.0 or later that contains fix for BCEL-329.- Since:
- 6.5.0
-
setArgumentName
-
setArgumentNames
-
setArgumentType
-
setArgumentTypes
-
setClassName
-
setInstructionList
-
setMaxLocals
Compute maximum number of local variables. -
setMaxLocals
Sets maximum number of local variables. -
setMaxStack
Computes max. stack size by performing control flow analysis. -
setMaxStack
Sets maximum stack size for this method. -
setReturnType
-
stripAttributes
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O -
toString
Return string representation close to declaration format, 'public static void main(String[]) throws IOException', e.g. -
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 they have finished editing the object.
-