A class
file consists of a single ClassFile
structure:
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
The interpretation of the field types is as shown in Table 4.2.
BaseType Character Type Interpretation B byte
signed byte C char
Unicode character D double
double-precision floating-point value F float
single-precision floating-point value I int
integer J long
long integer L
;
reference
an instance of class
S short
signed short Z boolean
true
or false
[ reference
one array dimension
All constant_pool
table entries have the following general format:
u1 tag;
u1 info[];
Each item in the constant_pool
table must begin with a 1-byte tag indicating the kind of cp_info
entry. The contents of the info
array vary with the value of tag
. The valid tags and their values are listed in Table 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.
Fields, methods, and interface methods are represented by similar structures:
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
}
The CONSTANT_String_info
structure is used to represent constant objects of the type String
: CONSTANT_String_info {
u1 tag;
}
The CONSTANT_Integer_info
and CONSTANT_Float_info
structures represent 4-byte numeric (int
and float
) constants:
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
CONSTANT_Float_info {
u1 tag;
u4 bytes;
The CONSTANT_Long_info
and CONSTANT_Double_info
represent 8-byte numeric (long
and double
) constants:
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
The CONSTANT_NameAndType_info
structure is used to represent a field or method, without indicating which class or interface type it belongs to:
CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
Each field is described by a field_info
structure. No two fields in one class
file may have the same name and descriptor (§4.3.2). The format of this structure is
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
access_flags
The value of the access_flags
item is a mask of flags used to denote access permission to and properties of this field. The interpretation of each flag, when set, is as shown in
method_info
structure. No two methods in one class
file may have the same name and descriptor . The structure has the following format:
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
access_flags
The value of the access_flags
item is a mask of flags used to denote access permission to and properties of this method. The interpretation of each flag, when set, is as shown in Table 4.5.
Attributes are used in the ClassFile
, field_info
, method_info
, and Code_attribute
structures of the class
file format. All attributes have the following general format:
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
The ConstantValue
attribute is a fixed-length attribute used in the attributes
table of the field_info
structures
The ConstantValue
attribute has the following format:
ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
}
The Code
attribute is a variable-length attribute used in the attributes
table of method_info
structures
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 Exceptions
attribute is a variable-length attribute used in the attributes
table of a method_info
The Exceptions
attribute has the following format:
Exceptions_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_exceptions;
u2 exception_index_table[number_of_exceptions];
}
The InnerClasses
attribute5 is a variable-length attribute in the attributes
table of the ClassFile
The InnerClasses
attribute has the following format:
InnerClasses_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_classes;
{
u2 inner_class_info_index;
u2 outer_class_info_index;
u2 inner_name_index;
u2 inner_class_access_flags;
}
classes[number_of_classes];
}
The Synthetic
attribute6 is a fixed-length attribute in the attributes
table of ClassFile
, field_info
, and method_info
structures.
The Synthetic
attribute has the following format:
Synthetic_attribute {
u2 attribute_name_index;
u4 attribute_length;
}
The SourceFile
attribute is an optional fixed-length attribute in the attributes
table of the ClassFile
structure
The SourceFile
attribute has the following format:
SourceFile_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 sourcefile_index;
}
The LineNumberTable
attribute is an optional variable-length attribute in the attributes
table of a Code
attribute.
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];
}The LocalVariableTable
attribute is an optional variable-length attribute of a Code
attribute.
The LocalVariableTable
attribute has the following format:
LocalVariableTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_table_length;
{ u2 start_pc;
u2 length;
u2 name_index;
u2 descriptor_index;
u2 index;
}
local_variable_table[local_variable_table_length];
}
The Deprecated
attribute7 is an optional fixed-length attribute in the attributes
table of ClassFile
(§4.1), field_info
(§4.5), and method_info
(§4.6)
structures.
The Deprecated
attribute has the following format:
Deprecated_attribute {
u2 attribute_name_index;
u4 attribute_length;
}