# 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`

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.

See Also:
  - `InstructionList`
  - `Method`

- ## Field Summary

  ### Fields inherited from class FieldGenOrMethodGen

  `cp, name, type`

  ### Fields inherited from class AccessFlags

  `access_flags`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MethodGen(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 Type
  Method
  Description
  `void`
  `addAnnotationsAsAttribute(ConstantPoolGen cp)`
  Adds annotations as an attribute.
  `void`
  `addCodeAttribute(Attribute a)`
  Add an attribute to the code.
  `void`
  `addException(String className)`
  Add an exception possibly thrown by this method.
  `CodeExceptionGen`
  `addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType)`
  Add an exception handler, that is, specify region where a handler is active and an instruction where the actual handling is done.
  `LineNumberGen`
  `addLineNumber(InstructionHandle ih, int srcLine)`
  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, int slot, InstructionHandle start, InstructionHandle end, int origIndex)`
  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.
  `void`
  `addParameterAnnotation(int parameterIndex, AnnotationEntryGen annotation)`
  Adds a parameter annotation.
  `void`
  `addParameterAnnotationsAsAttribute(ConstantPoolGen cp)`
  Adds parameter annotations as an attribute.
  `MethodGen`
  `copy(String className, ConstantPoolGen cp)`
  Creates a deep copy of this method.
  `boolean`
  `equals(Object obj)`
  Return value as defined by given BCELComparator strategy.
  `List<AnnotationEntryGen>`
  `getAnnotationsOnParameter(int i)`
  Return a list of AnnotationGen objects representing parameter annotations.
  `String`
  `getArgumentName(int i)`
  Gets the argument name at the specified index.
  `String[]`
  `getArgumentNames()`
  Gets all argument names.
  `Type`
  `getArgumentType(int i)`
  Gets the argument type at the specified index.
  `Type[]`
  `getArgumentTypes()`
  Gets all argument types.
  `String`
  `getClassName()`
  Gets the class that contains this method.
  `Attribute[]`
  `getCodeAttributes()`
  Gets all attributes of this method.
  `static BCELComparator<FieldGenOrMethodGen>`
  `getComparator()`
  Gets the comparison strategy object.
  `CodeExceptionGen[]`
  `getExceptionHandlers()`
  Gets array of declared exception handlers.
  `String[]`
  `getExceptions()`
  Gets array of thrown exceptions.
  `InstructionList`
  `getInstructionList()`
  Gets the instruction list.
  `LineNumberGen[]`
  `getLineNumbers()`
  Gets array of line numbers.
  `LineNumberTable`
  `getLineNumberTable(ConstantPoolGen cp)`
  Gets the 'LineNumberTable' attribute of all the local variables of this method.
  `LocalVariableGen[]`
  `getLocalVariables()`
  Gets array of declared local variables sorted by index.
  `LocalVariableTable`
  `getLocalVariableTable(ConstantPoolGen cp)`
  Gets the 'LocalVariableTable' attribute of all the local variables of this method.
  `LocalVariableTypeTable`
  `getLocalVariableTypeTable()`
  Gets the 'LocalVariableTypeTable' attribute of this method.
  `int`
  `getMaxLocals()`
  Gets the maximum number of local variables.
  `int`
  `getMaxStack()`
  Gets the maximum stack size.
  `static int`
  `getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et)`
  Computes stack usage of an instruction list by performing control flow analysis.
  `Method`
  `getMethod()`
  Gets method object.
  `Type`
  `getReturnType()`
  Gets the return type.
  `String`
  `getSignature()`
  Gets the signature of method or field.
  `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`
  `removeLocalVariableTypeTable()`
  Remove the LocalVariableTypeTable
  `void`
  `removeNOPs()`
  Remove all NOPs from the instruction list (if possible) and update every object referring to them, that is, branch instructions, local variables and exception handlers.
  `void`
  `removeObserver(MethodObserver o)`
  Remove observer for this object.
  `void`
  `removeRuntimeAttributes(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.
  `void`
  `setArgumentName(int i, String name)`
  Sets the argument name at the specified index.
  `void`
  `setArgumentNames(String[] argNames)`
  Sets all argument names.
  `void`
  `setArgumentType(int i, Type type)`
  Sets the argument type at the specified index.
  `void`
  `setArgumentTypes(Type[] argTypes)`
  Sets all argument types.
  `void`
  `setClassName(String className)`
  Sets the class name.
  `static void`
  `setComparator(BCELComparator<FieldGenOrMethodGen> comparator)`
  Sets the comparison strategy object.
  `void`
  `setInstructionList(InstructionList il)`
  Sets the instruction list.
  `void`
  `setMaxLocals()`
  Compute maximum number of local variables.
  `void`
  `setMaxLocals(int m)`
  Sets maximum number of local variables.
  `void`
  `setMaxStack()`
  Computes max. stack size by performing control flow analysis.
  `void`
  `setMaxStack(int m)`
  Sets maximum stack size for this method.
  `void`
  `setReturnType(Type returnType)`
  Sets the return type.
  `void`
  `stripAttributes(boolean flag)`
  Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O.
  `final String`
  `toString()`
  Return string representation close to declaration format, 'public static void main(String[]) throws IOException', for example.
  `void`
  `update()`
  Call notify() method on all observers.

  ### Methods inherited from class FieldGenOrMethodGen

  `addAll, addAnnotationEntry, addAttribute, clone, getAnnotationEntries, getAttributes, getConstantPool, getName, getType, removeAnnotationEntries, removeAnnotationEntry, removeAttribute, removeAttributes, setConstantPool, setName, setType`

  ### Methods inherited from class 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`

  ### Methods inherited from class Object

  `finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## 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 qualifiers.
      `returnType` - method type.
      `argTypes` - argument types.
      `argNames` - argument names (if this is null, default names will be provided for them).
      `methodName` - name of method.
      `className` - 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 method, String className, ConstantPoolGen cp)
    Instantiate from existing method.
    Parameters:
      `method` - method.
      `className` - class name containing this method.
      `cp` - constant pool.

- ## Method Details

  - ### getComparator

    public static BCELComparator<FieldGenOrMethodGen> getComparator()
    Gets the comparison strategy object.
    Returns:
      Comparison strategy object.

  - ### getMaxStack

    public static int getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et)
    Computes stack usage of an instruction list by performing control flow analysis.
    Parameters:
      `cp` - the constant pool generator.
      `il` - the instruction list.
      `et` - the exception handlers.
    Returns:
      maximum stack depth used by method.

  - ### setComparator

    public static void setComparator(BCELComparator<FieldGenOrMethodGen> comparator)
    Sets the comparison strategy object.
    Parameters:
      `comparator` - Comparison strategy object.

  - ### addAnnotationsAsAttribute

    public void addAnnotationsAsAttribute(ConstantPoolGen cp)
    Adds annotations as an attribute.
    Parameters:
      `cp` - the constant pool generator.
    Since:
      6.0

  - ### 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 className)
    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, that is, 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 done.
      `catchType` - class type of handled exception or null if any exception is handled.
    Returns:
      new exception handler object.

  - ### addLineNumber

    public LineNumberGen addLineNumber(InstructionHandle ih, int srcLine)
    Give an instruction a line number corresponding to the source code line.
    Parameters:
      `ih` - instruction to tag.
      `srcLine` - the source line number.
    Returns:
      new line number object.
    See Also:
      - `LineNumber`

  - ### 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`

  - ### 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, int slot, InstructionHandle start, InstructionHandle end, int origIndex)
    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.
      `origIndex` - the index of the local variable prior to any modifications.
    Returns:
      new local variable object.
    See Also:
      - `LocalVariable`

  - ### addObserver

    public void addObserver(MethodObserver o)
    Add observer for this object.
    Parameters:
      `o` - the observer to add.

  - ### addParameterAnnotation

    public void addParameterAnnotation(int parameterIndex, AnnotationEntryGen annotation)
    Adds a parameter annotation.
    Parameters:
      `parameterIndex` - the parameter index.
      `annotation` - the annotation.

  - ### addParameterAnnotationsAsAttribute

    public void addParameterAnnotationsAsAttribute(ConstantPoolGen cp)
    Adds parameter annotations as an attribute.
    Parameters:
      `cp` - the constant pool generator.
    Since:
      6.0

  - ### copy

    public MethodGen copy(String className, ConstantPoolGen cp)
    Creates a deep copy of this method.
    Parameters:
      `className` - the class name.
      `cp` - the constant pool generator.
    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.
    Overrides:
      `equals` in class `Object`
    See Also:
      - `Object.equals(Object)`

  - ### getAnnotationsOnParameter

    public List<AnnotationEntryGen> getAnnotationsOnParameter(int i)
    Return a list of AnnotationGen objects representing parameter annotations.
    Parameters:
      `i` - the parameter index.
    Returns:
      list of AnnotationGen objects.
    Since:
      6.0

  - ### getArgumentName

    public String getArgumentName(int i)
    Gets the argument name at the specified index.
    Parameters:
      `i` - the argument index.
    Returns:
      the argument name.

  - ### getArgumentNames

    public String[] getArgumentNames()
    Gets all argument names.
    Returns:
      array of argument names.

  - ### getArgumentType

    public Type getArgumentType(int i)
    Gets the argument type at the specified index.
    Parameters:
      `i` - the argument index.
    Returns:
      the argument type.

  - ### getArgumentTypes

    public Type[] getArgumentTypes()
    Gets all argument types.
    Returns:
      array of argument types.

  - ### getClassName

    public String getClassName()
    Gets the class that contains this method.
    Returns:
      class that contains this method.

  - ### getCodeAttributes

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

  - ### getExceptionHandlers

    public CodeExceptionGen[] getExceptionHandlers()
    Gets array of declared exception handlers.
    Returns:
      array of declared exception handlers.

  - ### getExceptions

    public String[] getExceptions()
    Gets array of thrown exceptions.
    Returns:
      array of thrown exceptions.

  - ### getInstructionList

    public InstructionList getInstructionList()
    Gets the instruction list.
    Returns:
      the instruction list.

  - ### getLineNumbers

    public LineNumberGen[] getLineNumbers()
    Gets array of line numbers.
    Returns:
      array of line numbers.

  - ### getLineNumberTable

    public LineNumberTable getLineNumberTable(ConstantPoolGen cp)
    Gets the 'LineNumberTable' attribute of all the local variables of this method.
    Parameters:
      `cp` - the constant pool generator.
    Returns:
      'LineNumberTable' attribute of all the local variables of this method.

  - ### getLocalVariables

    public LocalVariableGen[] getLocalVariables()
    Gets array of declared local variables sorted by index. If the range of the variable has not been set yet, it will be set to be valid from the start to the end of the instruction list.
    Returns:
      array of declared local variables sorted by index.

  - ### getLocalVariableTable

    public LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)
    Gets the 'LocalVariableTable' attribute of all the local variables of this method.
    Parameters:
      `cp` - the constant pool generator.
    Returns:
      'LocalVariableTable' attribute of all the local variables of this method.

  - ### getLocalVariableTypeTable

    public LocalVariableTypeTable getLocalVariableTypeTable()
    Gets the 'LocalVariableTypeTable' attribute of this method.
    Returns:
      'LocalVariableTypeTable' attribute of this method.

  - ### getMaxLocals

    public int getMaxLocals()
    Gets the maximum number of local variables.
    Returns:
      the maximum number of local variables.

  - ### getMaxStack

    public int getMaxStack()
    Gets the maximum stack size.
    Returns:
      the maximum stack size.

  - ### getMethod

    public Method 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

    public Type getReturnType()
    Gets the return type.
    Returns:
      the return type.

  - ### getSignature

    public String getSignature()
    Description copied from class: `FieldGenOrMethodGen`
    Gets the signature of method or field.
    Specified by:
      `getSignature` in class `FieldGenOrMethodGen`
    Returns:
      signature of method/field.

  - ### hashCode

    public int hashCode()
    Return value as defined by given BCELComparator strategy. By default return the hash code of the method's name XOR signature.
    Overrides:
      `hashCode` in class `Object`
    See Also:
      - `Object.hashCode()`

  - ### removeCodeAttribute

    public void removeCodeAttribute(Attribute a)
    Remove a code attribute.
    Parameters:
      `a` - the attribute to remove.

  - ### removeCodeAttributes

    public void removeCodeAttributes()
    Remove all code attributes.

  - ### removeException

    public void removeException(String c)
    Remove an exception.
    Parameters:
      `c` - the exception to remove.

  - ### removeExceptionHandler

    public void removeExceptionHandler(CodeExceptionGen c)
    Remove an exception handler.
    Parameters:
      `c` - the exception handler to remove.

  - ### 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.
    Parameters:
      `l` - the line number to remove.

  - ### 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.
    Parameters:
      `l` - the local variable to remove.

  - ### removeLocalVariables

    public void removeLocalVariables()
    Remove all local variables.

  - ### removeLocalVariableTypeTable

    public void removeLocalVariableTypeTable()
    Remove the LocalVariableTypeTable

  - ### removeNOPs

    public void removeNOPs()
    Remove all NOPs from the instruction list (if possible) and update every object referring to them, that is, branch instructions, local variables and exception handlers.

  - ### removeObserver

    public void removeObserver(MethodObserver o)
    Remove observer for this object.
    Parameters:
      `o` - the observer to remove.

  - ### removeRuntimeAttributes

    public void removeRuntimeAttributes(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.
    Parameters:
      `attributes` - the attributes to remove.
    Since:
      6.5.0

  - ### setArgumentName

    public void setArgumentName(int i, String name)
    Sets the argument name at the specified index.
    Parameters:
      `i` - the argument index.
      `name` - the argument name.

  - ### setArgumentNames

    public void setArgumentNames(String[] argNames)
    Sets all argument names.
    Parameters:
      `argNames` - the argument names.

  - ### setArgumentType

    public void setArgumentType(int i, Type type)
    Sets the argument type at the specified index.
    Parameters:
      `i` - the argument index.
      `type` - the argument type.

  - ### setArgumentTypes

    public void setArgumentTypes(Type[] argTypes)
    Sets all argument types.
    Parameters:
      `argTypes` - the argument types.

  - ### setClassName

    public void setClassName(String className)
    Sets the class name.
    Parameters:
      `className` - the class name.

  - ### setInstructionList

    public void setInstructionList(InstructionList il)
    Sets the instruction list.
    Parameters:
      `il` - the instruction list.

  - ### setMaxLocals

    public void setMaxLocals()
    Compute maximum number of local variables.

  - ### setMaxLocals

    public void setMaxLocals(int m)
    Sets maximum number of local variables.
    Parameters:
      `m` - the maximum number of local variables.

  - ### setMaxStack

    public void setMaxStack()
    Computes max. stack size by performing control flow analysis.

  - ### setMaxStack

    public void setMaxStack(int m)
    Sets maximum stack size for this method.
    Parameters:
      `m` - the maximum stack size.

  - ### setReturnType

    public void setReturnType(Type returnType)
    Sets the return type.
    Parameters:
      `returnType` - the return type.

  - ### stripAttributes

    public void stripAttributes(boolean flag)
    Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O.
    Parameters:
      `flag` - whether to strip attributes.

  - ### toString

    public final String toString()
    Return string representation close to declaration format, 'public static void main(String[]) throws IOException', for example.
    Overrides:
      `toString` in class `Object`
    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 they have finished editing the object.