Class 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
    • Constructor Detail

      • MethodGen

        public MethodGen​(int access_flags,
                         Type return_type,
                         Type[] argTypes,
                         String[] argNames,
                         String method_name,
                         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 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
        argTypes - argument types
        argNames - argument names (if this is null, default names will be provided for them)
        method_name - 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 Detail

      • addLocalVariable

        public LocalVariableGen addLocalVariable​(String name,
                                                 Type type,
                                                 int slot,
                                                 InstructionHandle start,
                                                 InstructionHandle end,
                                                 int orig_index)
        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
        orig_index - the index of the local variable prior to any modifications
        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,
                                                 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
      • 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.
      • getLocalVariableTable

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

        public LocalVariableTypeTable getLocalVariableTypeTable()
        Returns:
        `LocalVariableTypeTable' attribute of this method.
      • 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
        Returns:
        new line number object
        See Also:
        LineNumber
      • removeLineNumber

        public void removeLineNumber​(LineNumberGen l)
        Remove a line number.
      • removeLineNumbers

        public void removeLineNumbers()
        Remove all line numbers.
      • getLineNumberTable

        public 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,
                                                    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
      • removeExceptionHandler

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

        public void removeExceptionHandlers()
        Remove all line numbers.
      • addException

        public void addException​(String className)
        Add an exception possibly thrown by this method.
        Parameters:
        className - (fully qualified) name of exception
      • removeException

        public void removeException​(String c)
        Remove an exception.
      • removeExceptions

        public void removeExceptions()
        Remove all exceptions.
      • getExceptions

        public String[] getExceptions()
      • 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
      • removeLocalVariableTypeTable

        public void removeLocalVariableTypeTable()
        Remove the LocalVariableTypeTable
      • removeCodeAttribute

        public void removeCodeAttribute​(Attribute a)
        Remove a code attribute.
      • removeCodeAttributes

        public void removeCodeAttributes()
        Remove all code attributes.
      • getCodeAttributes

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

        public void addAnnotationsAsAttribute​(ConstantPoolGen cp)
        Since:
        6.0
      • addParameterAnnotationsAsAttribute

        public void addParameterAnnotationsAsAttribute​(ConstantPoolGen cp)
        Since:
        6.0
      • removeRuntimeAttributes

        public void removeRuntimeAttributes​(Attribute[] attrs)
        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
      • 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
      • removeNOPs

        public void 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.
      • setMaxLocals

        public void setMaxLocals​(int m)
        Set maximum number of local variables.
      • getMaxLocals

        public int getMaxLocals()
      • setMaxStack

        public void setMaxStack​(int m)
        Set maximum stack size for this method.
      • getMaxStack

        public int getMaxStack()
      • getClassName

        public String getClassName()
        Returns:
        class that contains this method
      • setClassName

        public void setClassName​(String class_name)
      • setReturnType

        public void setReturnType​(Type return_type)
      • getReturnType

        public Type getReturnType()
      • setArgumentTypes

        public void setArgumentTypes​(Type[] arg_types)
      • getArgumentTypes

        public Type[] getArgumentTypes()
      • setArgumentType

        public void setArgumentType​(int i,
                                    Type type)
      • getArgumentType

        public Type getArgumentType​(int i)
      • setArgumentNames

        public void setArgumentNames​(String[] arg_names)
      • getArgumentNames

        public String[] getArgumentNames()
      • setArgumentName

        public void setArgumentName​(int i,
                                    String name)
      • getArgumentName

        public String getArgumentName​(int i)
      • setInstructionList

        public void setInstructionList​(InstructionList il)
      • setMaxStack

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

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

        public void stripAttributes​(boolean flag)
        Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O
      • 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
      • addObserver

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

        public void removeObserver​(MethodObserver o)
        Remove observer for this object.
      • 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.
      • toString

        public final String toString()
        Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g.
        Overrides:
        toString in class Object
        Returns:
        String representation of the method.
      • getAnnotationsOnParameter

        public List<AnnotationEntryGen> getAnnotationsOnParameter​(int i)
        Return a list of AnnotationGen objects representing parameter annotations
        Since:
        6.0
      • addParameterAnnotation

        public void addParameterAnnotation​(int parameterIndex,
                                           AnnotationEntryGen annotation)
      • getComparator

        public static BCELComparator getComparator()
        Returns:
        Comparison strategy object
      • setComparator

        public static void setComparator​(BCELComparator comparator)
        Parameters:
        comparator - Comparison strategy object
      • 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(java.lang.Object)
      • hashCode

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