Contents Prev Next Up


2.6 Attributes

Attributes are used at several different places in the class format. All attributes have the following format:

GenericAttribute_info {
 u2 attribute_name;
 u4 attribute_length;
 u1 info[attribute_length];
}

The attribute_name is a 16-bit index into the class's constant pool; the value of constant_pool[attribute_name] is a CONSTANT_Utf8 string giving the name of the attribute. The field attribute_length indicates the length of the subsequent information in bytes. This length does not include the six bytes of the attribute_name and attribute_length.

In the following text, whenever we allow attributes, we give the name of the attributes that are currently understood. In the future, more attributes will be added. Class file readers are expected to skip over and ignore the information in any attribute they do not understand.

SourceFile

The "SourceFile" attribute has the following format:

SourceFile_attribute {
 u2 attribute_name_index;
 u4 attribute_length;
 u2 sourcefile_index;
}

attribute_name_index

constant_pool[attribute_name_index] is the CONSTANT_Utf8 string "SourceFile".

attribute_length

The length of a SourceFile_attribute must be 2.

sourcefile_index

constant_pool[sourcefile_index] is a CONSTANT_Utf8 string giving the source file from which this class file was compiled.

ConstantValue

The "ConstantValue" attribute has the following format:

ConstantValue_attribute {
 u2 attribute_name_index;
 u4 attribute_length;
 u2 constantvalue_index;
}

attribute_name_index

constant_pool[attribute_name_index] is the CONSTANT_Utf8 string "ConstantValue".

attribute_length

The length of a ConstantValue_attribute must be 2.

constantvalue_index

constant_pool[constantvalue_index]gives the constant value for this field.

The constant pool entry must be of a type appropriate to the field, as shown by the following table:
longCONSTANT_Long
floatCONSTANT_Float
doubleCONSTANT_Double
int, short, char, byte, booleanCONSTANT_Integer

Code

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[attribute_count];

}

attribute_name_index

constant_pool[attribute_name_index] is the CONSTANT_Utf8 string "Code".

attribute_length

This field indicates the total length of the "Code" attribute, excluding the initial six bytes.

max_stack

Maximum number of entries on the operand stack that will be used during execution of this method. See the other chapters in this spec for more information on the operand stack.

max_locals

Number of local variable slots used by this method. See the other chapters in this spec for more information on the local variables.

code_length

The number of bytes in the virtual machine code for this method.

code

These are the actual bytes of the virtual machine code that implement the method. When read into memory, if the first byte of code is aligned onto a multiple-of-four boundary the the tableswitch and tablelookup opcode entries will be aligned; see their description for more information on alignment requirements.

exception_table_length

The number of entries in the following exception table.

exception_table

Each entry in the exception table describes one exception handler in the code.

start_pc, end_pc

The two fields start_pc and end_pc indicate the ranges in the code at which the exception handler is active. The values of both fields are offsets from the start of the code. start_pc is inclusive. end_pc is exclusive.

handler_pc

This field indicates the starting address of the exception handler. The value of the field is an offset from the start of the code.

catch_type

If catch_type is nonzero, then constant_pool[catch_type] will be the class of exceptions that this exception handler is designated to catch. This exception handler should only be called if the thrown exception is an instance of the given class.

If catch_type is zero, this exception handler should be called for all exceptions.

attributes_count

This field indicates the number of additional attributes about code. The "Code" attribute can itself have attributes.

attributes

A "Code" attribute can have any number of optional attributes associated with it. Each attribute has a name, and other additional information. Currently, the only code attributes defined are the "LineNumberTable" and "LocalVariableTable," both of which contain debugging information.

Exceptions Table

This table is used by compilers which indicate which Exceptions a method is declared to throw:

Exceptions_attribute {
 u2 attribute_name_index;
 u4 attribute_length;
 u2 number_of_exceptions;
 u2 exception_index_table[number_of_exceptions];
}

attribute_name_index

constant_pool[attribute_name_index] will be the CONSTANT_Utf8 string "Exceptions".

attribute_length

This field indicates the total length of the Exceptions_attribute, excluding the initial six bytes.

number_of_exceptions

This field indicates the number of entries in the following exception index table.

exception_index_table

Each value in this table is an index into the constant pool. For each table element (exception_index_table[i] != 0, where 0 <= i < number_of_exceptions), then constant_pool[exception_index+table[i]] is a Exception that this class is declared to throw.

LineNumberTable

This attribute is used by debuggers and the exception handler to determine which part of the virtual machine code corresponds to a given location in the source. The LineNumberTable_attribute has the following format:

LineNumberTable_attribute {
 u2  attribute_name_index;
 u4  attribute_length;
 u2  line_number_table_length;
 {  u2       start_pc;    
  u2    line_number;
 }  line_number_table[line_number_table_length];
}

attribute_name_index

constant_pool[attribute_name_index] will be the CONSTANT_Utf8 string "LineNumberTable".

attribute_length

This field indicates the total length of the LineNumberTable_attribute, excluding the initial six bytes.

line_number_table_length

This field indicates the number of entries in the following line number table.

line_number_table

Each entry in the line number table indicates that the line number in the source file changes at a given point in the code.

start_pc

This field indicates the place in the code at which the code for a new line in the source begins. source_pc <<SHOULD THAT BE start_pc?>> is an offset from the beginning of the code.

line_number

The line number that begins at the given location in the file.

LocalVariableTable

This attribute is used by debuggers to determine the value of a given local variable during the dynamic execution of a method. The format of the LocalVariableTable_attribute is as follows:

LocalVariableTable_attribute {
 u2 attribute_name_index;
 u4 attribute_length;
 u2 local_variable_table_length;
 {  u2     start_pc;
    u2    length;
    u2    name_index;
    u2    signature_index;
    u2    slot;
 }  local_variable_table[local_variable_table_length];
} 

attribute_name_index

constant_pool[attribute_name_index] will be the CONSTANT_Utf8 string "LocalVariableTable".

attribute_length

This field indicates the total length of the LineNumberTable_attribute, excluding the initial six bytes.

local_variable_table_length

This field indicates the number of entries in the following local variable table.

local_variable_table

Each entry in the local variable table indicates a code range during which a local variable has a value. It also indicates where on the stack the value of that variable can be found.

start_pc, length

The given local variable will have a value at the code between start_pc and start_pc + length. The two values are both offsets from the beginning of the code.

name_index, signature_index

constant_pool[name_index]and constant_pool[signature_index] are CONSTANT_Utf8 strings giving the name and signature of the local variable.

slot

The given variable will be the slotth local variable in the method's frame.



Contents Prev Next Up