The Code attribute is a variable-length attribute in the attributes table of a method_info structure (§4.6). A Code attribute contains the Java Virtual Machine instructions and auxiliary information for a method, including an instance initialization method and a class or interface initialization method (§2.9.1, §2.9.2).
If the method is either native or abstract, and is not a class or interface initialization method, then its method_info structure must not have a Code attribute in its attributes table. Otherwise, its method_info structure must have exactly one Code attribute in its attributes table.
The Code attribute has the following format:
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 items of the Code_attribute structure are as follows:
-
attribute_name_index
The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing the string "Code". -
attribute_length
The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. -
max_stack
The value of the
max_stackitem gives the maximum depth of the operand stack of this method (§2.6.2) at any point during execution of the method. -
max_locals
The value of the
max_localsitem gives the number of local variables in the local variable array allocated upon invocation of this method (§2.6.1), including the local variables used to pass parameters to the method on its invocation.The greatest local variable index for a value of type
longordoubleismax_locals - 2. The greatest local variable index for a value of any other type ismax_locals - 1. -
code_length
The value of the
code_lengthitem gives the number of bytes in thecodearray for this method.The value of
code_lengthmust be greater than zero (as thecodearray must not be empty) and less than 65536. -
code[]
The
codearray gives the actual bytes of Java Virtual Machine code that implement the method.When the
codearray is read into memory on a byte-addressable machine, if the first byte of the array is aligned on a 4-byte boundary, the tableswitch and lookupswitch 32-bit offsets will be 4-byte aligned. (Refer to the descriptions of those instructions for more information on the consequences ofcodearray alignment.)The detailed constraints on the contents of the
codearray are extensive and are given in a separate section (§4.9). -
exception_table_length
The value of the
exception_table_lengthitem gives the number of entries in theexception_tabletable. -
exception_table[]
Each entry in the
exception_tablearray describes one exception handler in thecodearray. The order of the handlers in theexception_tablearray is significant (§2.10).Each
exception_tableentry contains the following four items:-
start_pc, end_pc
The values of the two items
start_pcandend_pcindicate the ranges in thecodearray at which the exception handler is active. The value ofstart_pcmust be a valid index into thecodearray of the opcode of an instruction. The value ofend_pceither must be a valid index into thecodearray of the opcode of an instruction or must be equal tocode_length, the length of thecodearray. The value ofstart_pcmust be less than the value ofend_pc.The
start_pcis inclusive andend_pcis exclusive; that is, the exception handler must be active while the program counter is within the interval [start_pc,end_pc).The fact that
end_pcis exclusive is a historical mistake in the design of the Java Virtual Machine: if the Java Virtual Machine code for a method is exactly 65535 bytes long and ends with an instruction that is 1 byte long, then that instruction cannot be protected by an exception handler. A compiler writer can work around this bug by limiting the maximum size of the generated Java Virtual Machine code for any method, instance initialization method, or static initializer (the size of any code array) to 65534 bytes. -
handler_pc
The value of the
handler_pcitem indicates the start of the exception handler. The value of the item must be a valid index into thecodearray and must be the index of the opcode of an instruction. -
catch_type
If the value of the
catch_typeitem is nonzero, it must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_infostructure (§4.4.1) representing a class of exceptions that this exception handler is designated to catch. The exception handler will be called only if the thrown exception is an instance of the given class or one of its subclasses.The verifier checks that the class is
Throwableor a subclass ofThrowable(§4.9.2).If the value of the
catch_typeitem is zero, this exception handler is called for all exceptions.This is used to implement
finally(§3.13).
-
-
attributes_count
The value of the
attributes_countitem indicates the number of attributes of theCodeattribute. -
attributes[]
Each value of the
attributestable must be anattribute_infostructure (§4.7).A
Codeattribute can have any number of optional attributes associated with it.The attributes defined by this specification as appearing in the
attributestable of aCodeattribute are listed in Table 4.7-C.The rules concerning attributes defined to appear in the
attributestable of aCodeattribute are given in §4.7.The rules concerning non-predefined attributes in the
attributestable of aCodeattribute are given in §4.7.1.