Class Method

  • All Implemented Interfaces:
    MethodInfo

    public class Method
    extends java.lang.Object
    implements MethodInfo
    Method represents a method in a Java classfile. A method's name and value (the types of its parameters and its return type) are modeled as indices into it class's constant pool. A method has modifiers that determine whether it is public, private, static, final, etc. Methods have a number of attributes such as their Code and any Exceptions they may throw.
    See Also:
    Code, Exceptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean DEBUG  
    • Constructor Summary

      Constructors 
      Constructor Description
      Method​(java.io.DataInputStream in, ClassInfo classInfo)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()
      Creates a clone of this MethodInfo except that its declaring class does not know about it.
      byte[] code()
      Get the byte code array of the method.
      int codeLength()
      Returns the length of the Code array.
      ClassInfo declaringClass()
      Get the class which declared the method.
      Catch[] exceptionHandlers()
      Get the exception handlers in the method.
      int[] exceptionTypes()
      Get the indices into the constant pool of the types of the exceptions thrown by the method.
      LineNumberDebugInfo[] lineNumbers()
      Get the line numbers of the instructions in the method.
      LocalDebugInfo[] locals()
      Get the local variable debug info for the method.
      int maxLocals()
      Get the maximum number of locals used in the method.
      int maxStack()
      Get the maximum height of the operand stack.
      int modifiers()
      Get the modifiers of the method.
      int nameIndex()
      Get the index into the constant pool of the name of the method.
      void setCode​(byte[] bytes)
      Set the byte code array of the method.
      void setExceptionHandlers​(Catch[] handlers)
      Set the exception handlers in the method.
      void setLineNumbers​(LineNumberDebugInfo[] lineNumbers)
      Set the line numbers of the instructions in the method.
      void setLocals​(LocalDebugInfo[] locals)
      Set the local variable debug info for the method.
      void setMaxLocals​(int maxLocals)
      Set the maximum number of locals used in the method.
      void setMaxStack​(int maxStack)
      Set the maximum height of the operand stack.
      void setModifiers​(int modifiers)
      Set the modifiers of the method.
      void setNameIndex​(int name)
      Set the index into the constant pool of the name of the method.
      void setTypeIndex​(int type)
      Set the index into the constant pool of the type of the method.
      java.lang.String toString()
      Returns a string representation of the method.
      int typeIndex()
      Get the index into the constant pool of the type of the method.
      void write​(java.io.DataOutputStream out)
      Write the method to a class file.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEBUG

        public static boolean DEBUG
    • Constructor Detail

      • Method

        public Method​(java.io.DataInputStream in,
                      ClassInfo classInfo)
               throws java.io.IOException
        Constructor. Read a method from a class file.
        Parameters:
        in - The data stream of the class file.
        classInfo - The class file containing the method.
        Throws:
        java.io.IOException - If an error occurs while reading.
    • Method Detail

      • declaringClass

        public ClassInfo declaringClass()
        Get the class which declared the method.
        Specified by:
        declaringClass in interface MethodInfo
        Returns:
        The ClassInfo of the class which declared the method.
      • setNameIndex

        public void setNameIndex​(int name)
        Set the index into the constant pool of the name of the method.
        Specified by:
        setNameIndex in interface MethodInfo
        Parameters:
        name - The index into the constant pool of the name of the method.
      • setTypeIndex

        public void setTypeIndex​(int type)
        Set the index into the constant pool of the type of the method.
        Specified by:
        setTypeIndex in interface MethodInfo
        Parameters:
        type - The index into the constant pool of the type of the method.
      • nameIndex

        public int nameIndex()
        Get the index into the constant pool of the name of the method.
        Specified by:
        nameIndex in interface MethodInfo
        Returns:
        The index into the constant pool of the name of the method.
      • typeIndex

        public int typeIndex()
        Get the index into the constant pool of the type of the method.
        Specified by:
        typeIndex in interface MethodInfo
        Returns:
        The index into the constant pool of the type of the method.
      • setModifiers

        public void setModifiers​(int modifiers)
        Set the modifiers of the method. The values correspond to the constants in the Modifiers class.
        Specified by:
        setModifiers in interface MethodInfo
        Parameters:
        modifiers - A bit vector of modifier flags for the method.
        See Also:
        Modifiers
      • modifiers

        public int modifiers()
        Get the modifiers of the method. The values correspond to the constants in the Modifiers class.
        Specified by:
        modifiers in interface MethodInfo
        Returns:
        A bit vector of modifier flags for the method.
        See Also:
        Modifiers
      • maxStack

        public int maxStack()
        Get the maximum height of the operand stack.
        Specified by:
        maxStack in interface MethodInfo
        Returns:
        The maximum height of the operand stack.
      • setMaxStack

        public void setMaxStack​(int maxStack)
        Set the maximum height of the operand stack.
        Specified by:
        setMaxStack in interface MethodInfo
        Parameters:
        maxStack - The maximum height of the operand stack.
      • maxLocals

        public int maxLocals()
        Get the maximum number of locals used in the method.
        Specified by:
        maxLocals in interface MethodInfo
        Returns:
        The maximum number of locals used in the method.
      • setMaxLocals

        public void setMaxLocals​(int maxLocals)
        Set the maximum number of locals used in the method.
        Specified by:
        setMaxLocals in interface MethodInfo
        Parameters:
        maxLocals - The maximum number of locals used in the method.
      • code

        public byte[] code()
        Get the byte code array of the method.
        Specified by:
        code in interface MethodInfo
        Returns:
        The byte code array of the method.
      • codeLength

        public int codeLength()
        Returns the length of the Code array.
      • setCode

        public void setCode​(byte[] bytes)
        Set the byte code array of the method.
        Specified by:
        setCode in interface MethodInfo
        Parameters:
        bytes - The byte code array of the method.
      • exceptionTypes

        public int[] exceptionTypes()
        Get the indices into the constant pool of the types of the exceptions thrown by the method.
        Specified by:
        exceptionTypes in interface MethodInfo
        Returns:
        The indices into the constant pool of the types of the exceptions thrown by the method.
      • setLineNumbers

        public void setLineNumbers​(LineNumberDebugInfo[] lineNumbers)
        Set the line numbers of the instructions in the method.
        Specified by:
        setLineNumbers in interface MethodInfo
        Parameters:
        lineNumbers - The line numbers of the instructions in the method.
      • setLocals

        public void setLocals​(LocalDebugInfo[] locals)
        Set the local variable debug info for the method.
        Specified by:
        setLocals in interface MethodInfo
        Parameters:
        locals - The local variable debug info for the method.
      • lineNumbers

        public LineNumberDebugInfo[] lineNumbers()
        Get the line numbers of the instructions in the method.
        Specified by:
        lineNumbers in interface MethodInfo
        Returns:
        The line numbers of the instructions in the method.
      • locals

        public LocalDebugInfo[] locals()
        Get the local variable debug info for the method.
        Specified by:
        locals in interface MethodInfo
        Returns:
        The local variable debug info for the method.
      • exceptionHandlers

        public Catch[] exceptionHandlers()
        Get the exception handlers in the method.
        Specified by:
        exceptionHandlers in interface MethodInfo
        Returns:
        The exception handlers in the method.
      • setExceptionHandlers

        public void setExceptionHandlers​(Catch[] handlers)
        Set the exception handlers in the method.
        Specified by:
        setExceptionHandlers in interface MethodInfo
        Parameters:
        handlers - The exception handlers in the method.
      • write

        public void write​(java.io.DataOutputStream out)
                   throws java.io.IOException
        Write the method to a class file.
        Parameters:
        out - The data stream of the class file.
        Throws:
        java.io.IOException - If an error occurs while writing.
      • toString

        public java.lang.String toString()
        Returns a string representation of the method.
        Overrides:
        toString in class java.lang.Object
      • clone

        public java.lang.Object clone()
        Creates a clone of this MethodInfo except that its declaring class does not know about it.
        Specified by:
        clone in interface MethodInfo
        Overrides:
        clone in class java.lang.Object