Class Code


  • public class Code
    extends Attribute
    Code is used to store the Code attribute of a method in a class file. The Code attribute stores the raw bytecode of the method, the maximum stack height and maximum number of locals used by the method, and the exception handlers used in the method. Code may have several attributes. The local variable table and the line number table are modeled explicitly. All other attributes are modeled as generic attributes.
    See Also:
    Catch, GenericAttribute, LineNumberDebugInfo, LocalVariableTable
    • Constructor Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()  
      byte[] code()
      Get the bytes of the code.
      int codeLength()
      Return the length of the code array
      Catch[] exceptionHandlers()
      Get the exception handlers in the method.
      int length()
      Get the length of the attribute.
      LineNumberDebugInfo[] lineNumbers()
      Get the line number debug info for the code.
      LocalDebugInfo[] locals()
      Get the local variable debug info for the code.
      int maxLocals()
      Get the maximum number of locals used by the code.
      int maxStack()
      Get the maximum height of the operand stack used by the code.
      void setCode​(byte[] code)
      Set the bytes of the code.
      void setExceptionHandlers​(Catch[] handlers)
      Set the exception handlers in the method.
      void setLineNumbers​(LineNumberDebugInfo[] lineNumbers)
      Set the line number debug info for the code.
      void setLocals​(LocalDebugInfo[] locals)
      Set the local variable debug info for the code.
      void setMaxLocals​(int maxLocals)
      Set the maximum number of locals used by the code.
      void setMaxStack​(int maxStack)
      Set the maximum height of the operand stack used by the code.
      java.lang.String toString()
      Returns a string representation of the attribute.
      void writeData​(java.io.DataOutputStream out)
      Write the attribute to a data stream.
      • Methods inherited from class java.lang.Object

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

      • Code

        public Code​(ClassInfo classInfo,
                    java.io.DataInputStream in,
                    int index,
                    int len)
             throws java.io.IOException
        Constructor. Create a Code attribute from a data stream.
        Parameters:
        in - The data stream containing the class file.
        index - The index into the constant pool of the name of the attribute.
        len - The length of the attribute, excluding the header.
        Throws:
        java.io.IOException - If an error occurs while reading.
    • Method Detail

      • writeData

        public void writeData​(java.io.DataOutputStream out)
                       throws java.io.IOException
        Write the attribute to a data stream.
        Specified by:
        writeData in class Attribute
        Parameters:
        out - The data stream of the class file.
        Throws:
        java.io.IOException - If an error occurs while writing.
      • setMaxStack

        public void setMaxStack​(int maxStack)
        Set the maximum height of the operand stack used by the code.
        Parameters:
        maxStack - The maximum height of the stack.
      • setMaxLocals

        public void setMaxLocals​(int maxLocals)
        Set the maximum number of locals used by the code.
        Parameters:
        maxLocals - The maximum number of locals.
      • maxStack

        public int maxStack()
        Get the maximum height of the operand stack used by the code.
        Returns:
        The maximum number of locals.
      • maxLocals

        public int maxLocals()
        Get the maximum number of locals used by the code.
        Returns:
        The maximum number of locals.
      • setExceptionHandlers

        public void setExceptionHandlers​(Catch[] handlers)
        Set the exception handlers in the method.
        Parameters:
        handlers - The handlers.
      • length

        public int length()
        Get the length of the attribute.
        Overrides:
        length in class Attribute
        Returns:
        The length of the attribute.
      • lineNumbers

        public LineNumberDebugInfo[] lineNumbers()
        Get the line number debug info for the code.
        Returns:
        The line number debug info for the code.
      • locals

        public LocalDebugInfo[] locals()
        Get the local variable debug info for the code.
        Returns:
        The local variable debug info for the code.
      • setLineNumbers

        public void setLineNumbers​(LineNumberDebugInfo[] lineNumbers)
        Set the line number debug info for the code.
        Parameters:
        lineNumbers - The line number debug info for the code.
      • setLocals

        public void setLocals​(LocalDebugInfo[] locals)
        Set the local variable debug info for the code.
        Parameters:
        locals - The local variable debug info for the code.
      • exceptionHandlers

        public Catch[] exceptionHandlers()
        Get the exception handlers in the method.
        Returns:
        The handlers.
      • code

        public byte[] code()
        Get the bytes of the code.
        Returns:
        The code.
      • codeLength

        public int codeLength()
        Return the length of the code array
      • setCode

        public void setCode​(byte[] code)
        Set the bytes of the code.
        Parameters:
        code - The code.
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class Attribute
      • toString

        public java.lang.String toString()
        Returns a string representation of the attribute.
        Overrides:
        toString in class Attribute