Class Code

All Implemented Interfaces:
Cloneable, Node

public final class Code extends Attribute
This class represents a chunk of Java byte code contained in a method. It is instantiated by the Attribute.readAttribute() method. A Code attribute contains informations about operand stack, local variables, byte code and the exceptions handled within this method. This attribute has attributes itself, namely LineNumberTable which is used for debugging purposes and LocalVariableTable which contains information about the local variables.
 Code_attribute {
   u2 attribute_name_index;
   u4 attribute_length;
   u2 max_stack;
   u2 max_locals;
   u4 code_length;
   u1 code[code_length];
   u2 exception_table_length;
   {
     u2 start_pc;
     u2 end_pc;
     u2 handler_pc;
     u2 catch_type;
   } exception_table[exception_table_length];
   u2 attributes_count;
   attribute_info attributes[attributes_count];
 }
 
See Also:
  • Constructor Details Link icon

    • Code Link icon

      public Code(Code code)
      Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy.
      Parameters:
      code - The source Code.
    • Code Link icon

      public Code(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool)
      Parameters:
      nameIndex - Index pointing to the name Code
      length - Content length in bytes
      maxStack - Maximum size of stack
      maxLocals - Number of local variables
      code - Actual byte code
      exceptionTable - of handled exceptions
      attributes - Attributes of code: LineNumber or LocalVariable
      constantPool - Array of constants
  • Method Details Link icon

    • accept Link icon

      public void accept(Visitor v)
      Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
      Specified by:
      accept in interface Node
      Specified by:
      accept in class Attribute
      Parameters:
      v - Visitor object
    • copy Link icon

      public Attribute copy(ConstantPool constantPool)
      Specified by:
      copy in class Attribute
      Parameters:
      constantPool - the constant pool to duplicate
      Returns:
      deep copy of this attribute
    • dump Link icon

      public void dump(DataOutputStream file) throws IOException
      Dump code attribute to file stream in binary format.
      Overrides:
      dump in class Attribute
      Parameters:
      file - Output file stream
      Throws:
      IOException - if an I/O error occurs.
    • getAttributes Link icon

      Returns:
      Collection of code attributes.
      See Also:
    • getCode Link icon

      public byte[] getCode()
      Returns:
      Actual byte code of the method.
    • getExceptionTable Link icon

      Returns:
      Table of handled exceptions.
      See Also:
    • getLineNumberTable Link icon

      Returns:
      LineNumberTable of Code, if it has one
    • getLocalVariableTable Link icon

      Returns:
      LocalVariableTable of Code, if it has one
    • getLocalVariableTypeTable Link icon

      Gets the local variable type table attribute LocalVariableTypeTable.
      Returns:
      LocalVariableTypeTable of Code, if it has one, null otherwise.
      Since:
      6.10.0
    • getMaxLocals Link icon

      public int getMaxLocals()
      Returns:
      Number of local variables.
    • getMaxStack Link icon

      public int getMaxStack()
      Returns:
      Maximum size of stack used by this method.
    • getStackMap Link icon

      Finds the attribute of StackMap instance.
      Returns:
      StackMap of Code, if it has one, else null.
      Since:
      6.8.0
    • setAttributes Link icon

      public void setAttributes(Attribute[] attributes)
      Parameters:
      attributes - the attributes to set for this Code
    • setCode Link icon

      public void setCode(byte[] code)
      Parameters:
      code - byte code
    • setExceptionTable Link icon

      public void setExceptionTable(CodeException[] exceptionTable)
      Parameters:
      exceptionTable - exception table
    • setMaxLocals Link icon

      public void setMaxLocals(int maxLocals)
      Parameters:
      maxLocals - maximum number of local variables
    • setMaxStack Link icon

      public void setMaxStack(int maxStack)
      Parameters:
      maxStack - maximum stack size
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Attribute
      Returns:
      String representation of code chunk.
    • toString Link icon

      public String toString(boolean verbose)
      Converts this object to a String.
      Parameters:
      verbose - Provides verbose output when true.
      Returns:
      String representation of code chunk.