• Aucun résultat trouvé

Class File Structure

Each Java class is defined by a binary stream, typically stored in a class file, consisting of 8-bit bytes. The stream content is described by a pseudo structure given in the JVM specification and quoted here in Listing 17.3. Although this might look like too much information, the structures presented in this and the following sections will help in understanding the genera-tion and instrumentagenera-tion of bytecode later.

LISTING 17.3 ClassFileStructure 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];

}

For clarity, the JVM specification defines pseudo-types u1, u2, and u4representing unsigned 1-, 2-, and 4-byte types, respectively. Table 17.3 lists each field of the ClassFilestructure and its meaning.

TA B L E 1 7 . 3 ClassFileFields

FIELD DESCRIPTION

Magic Class file format marker. It always has the value of 0xCAFEBABE.

Minor_version, major_version Version of JVM for which the class file was compiled. JVMs might support lower major versions but do not run higher major versions.

constant_pool_count Number of items in the constant pool array. The first item of the constant pool is reserved for internal JVM use, so the valid values of

constant_pool_countare 1and higher.

constant_pool[] An array of variable-length structures representing string constants, class and field names, and other constants.

access_flags A mask of modifiers used in class or interface declarations. The valid modifiers are ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_INTERFACE, and ACC_ABSTRACT.

this_class An index of the constant_poolarray item that describes this class.

super_class A zero or an index of the constant_poolarray item describing the super class for this class. For a class, a value of 0indicates that the super class is java.lang.Object.

interfaces_count Number of super interfaces of this class or interface.

interfaces[] An array of indexes of constant_poolitems describing the super interfaces of this class.

fields_count Number of items in the fieldsarray.

fields[] An array of variable-length structures describing the fields declared in this class.

Methods_count Number of items in the methodsarray.

Methods[] An array of variable-length structures describing the methods declared in this class, including the method bytecode.

attributes_count Number of items in the attributesarray.

Attributes[] An array of variable-length structures declaring attributes of this class file.

The standard attributes include SourceFile, LineNumberTable, and others. The JVM is required to ignore the attributes that are not known to it.

The constant pool deserves a little more attention because it is used frequently by other struc-tures. Any text string found in a Java class, regardless of its nature, is stored in the same pool of constants. This includes the class name, names of fields and methods, names of classes and methods the class invokes, and literal strings used inside the Java code. Anytime a name or string needs to be used, it is referred to by an index into the constant pool. The constant pool is an array of cp_infostructures, the general format of which is shown in Listing 17.4.

171 Class File Format

LISTING 17.4 Constant Pool Item Structure cp_info {

u1 tag;

u1 info[];

}

The actual items stored in the pool follow the structure that corresponds to the tag. For example, a string is defined using a CONSTANT_Stringstructure and a reference to a field using CONSTANT_Fieldref. The list of structures and their contents can be found in the JVM specifi-cation.

The ClassFilestructure uses three other structures: field_info, method_info, and

attribute_info. field_infois similar to method_info, so we’ll show only the method_info structure in Listing 17.5.

LISTING 17.5 method_infoStructure method_info {

u2 access_flags;

u2 name_index;

u2 descriptor_index;

u2 attributes_count;

attribute_info attributes[attributes_count];

}

The meanings of the method_infofields are given in Table 17.4.

TA B L E 1 7 . 4 method_infoFields

FIELD DESCRIPTION

access_flags A mask of modifiers describing the method accessibility and properties, including static, final, synchronized, native, and abstract.

name_index An index into the constant_poolarray item representing the method name.

descriptor_index An index into the constant_poolarray item representing the method descriptor.

attributes_count The number of items in the attributesarray.

attributes[] An array of method attributes. The attributes defined by the JVM specification include Codeand Exceptions. The attributes not recognized by the JVM are ignored.

Attributes

The attributes are used in the ClassFile, field_info, method_info, and Code_attribute structures to provide additional information that depends on the structure type. For example, class attributes include the source filename and debugging information, whereas method attributes include the bytecode and exceptions. Listing 17.6 shows the structure of attribute_info, and Table 17.5 lists its fields.

LISTING 17.6 attribute_infoStructure attribute_info {

u2 attribute_name_index;

u4 attribute_length;

u1 info[attribute_length];

}

TA B L E 1 7 . 5

attribute_infoFields

FIELD DESCRIPTION

attribute_name_index An index of the constant_poolitem representing the attribute name attribute_length The length of the attribute_infoarray in bytes

attribute_info The binary content of the attribute

The compilers and post processors are allowed to define and name new attributes, as long as they do not affect the semantics of the class. For instance, AOP implementations can use bytecode attributes to store the aspects defined for a class.