JVM Class file structure

Source: Internet
Author: User
Definition: u11 bytes are non-negative values. u22 bytes are non-negative values ........ (others) the Java file structure is described as follows using struct: ClassFile {u4magic; must be 0xCAFEBABEu2minor_version; u2major_version; CLASS file structure primary and secondary version JA

Definition: u1 1 byte is the unit of non-negative value u2 2 byte is the unit of non-negative value u3 ........ (others) the Java file structure is described as follows using struct: ClassFile {u4 magic; // It must be 0 xCAFEBABE u2 minor_version; u2 major_version; // The primary and secondary versions of the CLASS file structure JA

Definition: u1

1 byte is a non-negative value in units

U2

The two bytes are non-negative values in units.

U3

........

(Others, and so on)

Java File StructureA description similar to struct is as follows:

ClassFile {

U4 magic;

// The value must be 0 xCAFEBABE.

U2 minor_version;

U2 major_version;

// CLASS File StructureMajor and minor version JAVA2 supports 45.0-46.

0

U2 constant_pool_count; // record constant information

Cp_info constant_pool [constant_pool_count-1];

// The count starts from 1.

U2 access_flags;

// Class/interface access permission

U2 this_class;

// Point to the valid index value in constant_poll

U2 super_class;

// 0 or point to the valid index value in constant_poll.

Terface must be non-0

U2 interfaces_count;

// Number of superinterfaces

U2 interfaces [interfaces_count];

// Count [0, count-1) corresponding to constant_po

An index value in ol

U2 fields_count;

Field_info fields [fields_count];

// Used to record class and instance variables

U2 methods_count;

Method_info methods [methods_count];

U2 attributes_count;

Attribute_info attributes [attributes_count];

}

Cp_info {

U1 tag;

U1 info [];

}

The tag has the following meanings:

CONSTANT_Class

7

CONSTANT_Fieldref

9

CONSTANT_Methodref

10

CONSTANT_InterfaceMethodref

11

CONSTANT_String

8

CONSTANT_Integer

3

CONSTANT_Float

4

CONSTANT_Long

5

CONSTANT_Double

6

CONSTANT_NameAndType

12

CONSTANT_Utf8

1

Cp_info corresponds StructureChanged

1. CONSTANT_Class

CONSTANT_Class_info {

U1 tag;

U2 name_index;

}

2. CONSTANT_Fieldref

CONSTANT_Fieldref_info {

U1 tag;

U2 class_index;

// Constant_pool index, corresponding to CONSTANT_Class _

Info

U2 name_and_type_index; // constant_pool index, corresponding to CONSTANT _

NameAndType_info

}

3. CONSTANT_Methodref

CONSTANT_Methodref_info {

U1 tag;

U2 class_index;

U2 name_and_type_index;

}

4. CONSTANT_InterfaceMethodref

CONSTANT_InterfaceMethodref_info {

U1 tag;

U2 class_index;

U2 name_and_type_index;

}

5. CONSTANT_String

CONSTANT_String_info {

U1 tag;

U2 string_index;

}

6. CONSTANT_Integer

CONSTANT_Integer_info {

U1 tag;

U4 bytes;

}

7. CONSTANT_Float

CONSTANT_Float_info {

U1 tag;

U4 bytes;

}

8. CONSTANT_Long

CONSTANT_Long_info {

U1 tag;

U4 high_bytes;

U4 low_bytes;

}

9. CONSTANT_Double

CONSTANT_Double_info {

U1 tag;

U4 high_bytes;

U4 low_bytes

}

10. CONSTANT_NameAndType

CONSTANT_NameAndType_info {

U1 tag;

U2 name_index;

U2 descriptor_index;

}

11. CONSTANT_Utf8

CONSTANT_Utf8_info {

U1 tag;

U2 length;

U1 bytes [length];

}

Access_flags indicates the following:

ACC_PUBLIC

Zero x 0001

ACC_FINAL

Zero x 0010

ACC_SUPER

Zero x 0020

ACC_INTERFACE

Zero x 0200

ACC_ABSTRACT

Zero x 0400

If it is an interface, you must set ACC_INTERFACE. If it is not set to an ACC_INTERFACE, it is defined as one.

Instead of interfaces.

If ACC_INTERFACE is set, the ACC_ABSTRACT bit must also be set. You can also set the AC

C_PUBLIC.

ACC_SUPER is used to indicate invokespecial semantics. Sun's old JAVA compiler does not set ACC_SUPER

And the old JVM

The ACC_SUPER bit is ignored, but the new compiler should implement the invokespecial semantics.

Other unspecified bits are reserved for future use, and the compiler should set them to 0.

Click them.

This_class:

The index value in constant_pool. The cp_info of the element to which it points is equivalent to CONSTANT_Class.

_ Info

CONSTANT_Class_info {

U1 tag;

// The value must be CONSTANT_Class (7)

U2 name_index;

// Point to an index value in constant_pool

}

Name_index: cp_info is equivalent to CONSTANT_Utf8_info.

CONSTANT_Utf8_info {

U1 tag;

// The value must be CONSTANT_Utf8 (1)

U2 length;

U1 bytes [length];

// Utf8 encoded string

}

Field_info {

U2 access_flags;

// Access Control

U2 name_index;

// Index in constant_pool, corresponding to CONSTANT_Utf8_inf

O description.

U2 descriptor_index; // The index in constant_pool, corresponding to CONSTANT_Utf8_ I

Nfo description.

U2 attributes_count;

Attribute_info attributes [attributes_count]; // attribute_info will be stored in mo

Description after thods.

}

The value of access_flages in field_info is as follows:

ACC_PUBLIC

Zero x 0001

ACC_PRIVATE

Zero x 0002

ACC_PROTECTED

Zero x 0004

ACC_STATIC

Zero x 0008

ACC_FINAL

Zero x 0010

ACC_VOLATILE

Zero x 0040

ACC_TRANSIENT

Zero x 0080

Obviously, the ACC_FINAL and ACC_VOLATILE cannot be used at the same time, and the first three are mutually exclusive.

The interface must be set to the ACC_PUBLIC, ACC_STATIC, and ACC_FINAL bits, and cannot be set to other bits.

Other unspecified bits are retained for future use, and the compiler should set it to 0.

When ignoring them.

Methods indicates all methods in the class.

Method_info {

U2 access_flags;

U2 name_index;

// Point to the constant_pool entry, corresponding to CONSTANT _

Utf8_info

U2 descriptor_index;

// Point to the constant_pool entry, corresponding to CONS

TANT_Utf8_info

U2 attributes_count;

Attribute_info attributes [attributes_count];

// Only four types of codes, Exceptions, Synthetic, and Deprecated can appear here

Type attributes

}

Access_flags access permissions are described as follows:

ACC_PUBLIC

Zero x 0001

ACC_PRIVATE

Zero x 0002

ACC_PROTECTED

Zero x 0004

ACC_STATIC

Zero x 0008

ACC_FINAL

Zero x 0010

ACC_SYNCHRONIZED

Zero x 0020

ACC_NATIVE

Zero x 0100

ACC_ABSTRACT

Zero x 0400

ACC_STRICT

Zero x 0800

Attribute_info {

U2 attribute_name_index;

// Index in constant_pool, corresponding to CONSTANT_U

Tf8_info description.

U4 attribute_length;

U1 info [attribute_length];

}

Predefined attributes include:

1. SourceFile: attribute_info is replaced:

SourceFile_attribute {

U2 attribute_name_index;

U4 attribute_length;

U2 sourcefile_index; // point to a CONSTANT_Ut in constant_pool

F8_info Structure.

}

2. Constantvalue: attribute_info is replaced:

Constantvalue_attribute {

U2 attribute_name_index;

U4 attribute_length;

// It must be 2

U2 constantvalue_index;

}

Constantvalue_index has the following meanings:

Long

CONSTANT_Long

Float

CONSTANT_Float

Double

CONSTANT_Double

Int, short, char, byte, boolean

CONSTANT_Integer

String

CONSTANT_String

Constantvalue is used in field_info to describe a static constant,

In this case, the access_flags of field_info should be ACC_STATIC.

3. Code: attribute_info is replaced:

Code_attribute {

U2 attribute_name_index;

U4 attribute_length;

U2 max_stack;

// Maximum stack depth available when executing this function

U2 max_locals; // The maximum number of local variables available for executing this function, including Parameters

Number.

// Note: A long/double is equivalent to the number of two variables.

U4 code_length; // The code length used by this function.

U1 code [code_length]; // The Real bytecode used to implement this function

U2 prediction_table_length;

{

U2 start_pc;

U2 end_pc; // [start_pc, end_p

C) Part

U2

Handler_pc; // What does handler_pc do !!

U2

Catch_type; // The index pointing to the constant_pool, corresponding to the CONSTANT

_ Class_info

} Prediction_table [prediction_table_length];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.