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 information 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];
}

The layout above uses the standard Code header. This class also supports compact Code headers found in historical class file versions 45.0 through 45.2. Here, "compact" describes the narrower encoding of three fields: max_stack and max_locals each use u1, and code_length uses u2, instead of u2, u2, and u4, respectively. The notation u1, u2, and u4 denotes unsigned one-, two-, and four-byte values. These three fields therefore occupy 4 bytes instead of 8, excluding the common six-byte attribute prefix. The layout of that prefix and the remaining Code fields is unchanged; the bytecode itself is not compressed. isCompactCodeHeader() identifies this narrower layout.

Class file versions 45.0 through 45.2 also occur with standard Code headers accepted by modern JVMs, so the version alone does not identify the layout. ClassParser uses standard headers by default; ClassParser.setCompactCodeHeaderEnabled(boolean) explicitly selects compact headers for those versions.

See Also:
  • Constructor Details

    • Code

      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

      public Code(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool)
      Constructs a Code attribute using a standard eight-byte header.

      Use setCompactCodeHeader(boolean) to select the compact four-byte header after construction.

      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.
      Throws:
      ClassFormatException - Thrown if the code array is greater than Const.MAX_CODE_SIZE.
  • Method Details

    • accept

      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. That is, the hierarchy of methods, fields, attributes, and other elements forms a tree of objects.
      Specified by:
      accept in interface Node
      Specified by:
      accept in class Attribute
      Parameters:
      v - Visitor object.
    • copy

      public Attribute copy(ConstantPool constantPool)
      Creates a deep copy of this attribute.
      Specified by:
      copy in class Attribute
      Parameters:
      constantPool - The constant pool to duplicate.
      Returns:
      deep copy of this attribute.
    • dump

      public void dump(DataOutputStream file) throws IOException
      Dumps code attribute to file stream in binary format.
      Overrides:
      dump in class Attribute
      Parameters:
      file - Output file stream.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a compact header's max_stack or max_locals is outside 0 through 255, or its code length exceeds 65535 bytes.
    • getAttributes

      Gets the collection of code attributes.
      Returns:
      Collection of code attributes.
      See Also:
    • getCode

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

      Gets the table of handled exceptions.
      Returns:
      Table of handled exceptions.
      See Also:
    • getLineNumberTable

      Gets the LineNumberTable of Code, if it has one.
      Returns:
      LineNumberTable of Code, if it has one.
    • getLocalVariableTable

      Gets the LocalVariableTable of Code, if it has one.
      Returns:
      LocalVariableTable of Code, if it has one.
    • getLocalVariableTypeTable

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

      public int getMaxLocals()
      Gets the number of local variables.
      Returns:
      Number of local variables.
    • getMaxStack

      public int getMaxStack()
      Gets the maximum size of stack used by this method.
      Returns:
      Maximum size of stack used by this method.
    • getStackMap

      Gets the StackMap attribute.
      Returns:
      StackMap of Code, if it has one, else null.
      Since:
      6.8.0
    • isCompactCodeHeader

      public boolean isCompactCodeHeader()
      Gets whether this attribute uses the compact four-byte Code header.
      Returns:
      Whether max_stack, max_locals, and code_length use u1, u1, and u2, respectively, instead of the standard u2, u2, and u4.
      Since:
      6.14.0
      See Also:
    • setAttributes

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

      public void setCode(byte[] code)
      Sets the byte code.
      Parameters:
      code - byte code.
      Throws:
      ClassFormatException - Thrown if the code array is greater than Const.MAX_CODE_SIZE.
    • setCompactCodeHeader

      public void setCompactCodeHeader(boolean compactCodeHeader)
      Sets the Code header layout and recalculates the attribute length.

      Compact headers encode max_stack, max_locals, and code_length as u1, u1, and u2, respectively, totaling 4 bytes instead of the standard 8. This limits max_stack and max_locals to 0 through 255 and the code length to 65535 bytes. The remaining attribute layout is unchanged. Compact headers are intended for historical class file versions 45.0 through 45.2. Enabling compact headers validates these values before changing the layout. Subsequent changes to them are checked by dump(DataOutputStream).

      This setting controls this attribute's own dump. When dumping an entire class, JavaClass.dump(DataOutputStream) selects one layout for all method Code attributes based on the class version and the methods' header settings, without changing this attribute's setting.

      Parameters:
      compactCodeHeader - Whether to use a compact four-byte header (true) or a standard eight-byte header (false).
      Throws:
      ClassFormatException - Thrown if compactCodeHeader is true and the current values do not fit in a compact header.
      Since:
      6.14.0
    • setExceptionTable

      public void setExceptionTable(CodeException[] exceptionTable)
      Sets the exception table.
      Parameters:
      exceptionTable - exception table.
    • setMaxLocals

      public void setMaxLocals(int maxLocals)
      Sets the maximum number of local variables.
      Parameters:
      maxLocals - maximum number of local variables.
    • setMaxStack

      public void setMaxStack(int maxStack)
      Sets the maximum stack size.
      Parameters:
      maxStack - maximum stack size.
    • toString

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

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