Understanding the Java class file structure

Source: Internet
Author: User

We all know that Java files are compiled into a binary class file, but in most cases we know more about Java syntax, which helps us to write beautiful code, but we can write more efficient code if we know what's lower in Java. When researching findbugs tools, because of their use of bcel to process class files, this requires us to have a certain understanding of the basic structure of class files. Here is purely a record of my Learning class file structure of some summary, to more systematic in-depth study can view the official document the Java Virtual machine specification.

    1. The class file is composed of 8 byte-based stream streams, which are arranged in strict order, and there is no gap between the bytes, and for data that exceeds 8 bytes, it is stored in the order of Big-endian.
    2. The class file structure uses a C-like structure to store data, there are two main types of data items, unsigned numbers and tables, unsigned numbers used to represent a number, index references, and strings, such as u1,u2,u4,u8, representing 1 bytes, 2 bytes, 4 bytes, 8 bytes of unsigned number, The table is a composite structure with multiple unsigned numbers and other tables.

As the following class file structure:

--from JVM Specification

The Magic value is a constant 0xCAFEBABE that can be used to identify whether a file is a class file.

Minor _version , major_version Two values specify the primary and secondary version number of the class file, which the JVM can use to determine if the class file is supported.

constant_pool_count This value equals the number of constants in the constant pool plus 1, and the Constant_pool index between 0 and Constant_pool_count is legal. The reason to add 1 is that when a data item index of 0 indicates that the data item does not reference constants in any constant pool.

constant_pool[] is used to store character constants, class or interface names, field names, method names, and other constants that may be involved, and the constant pool occupies almost 60% of the class file. Constants are stored in tables with the following structure:

Cp_info {
U1 tag; The 1-byte tag indicates what kind of constant
    U1 info[]; Other information, but all are fixed bytes
}
The JVM defines the following 14 constants, together with their corresponding tags.
  --from  JVM Specification

access_flags used to represent access information for a class or interface public,private ...,

--from JVM Specification

this_class A valid index value, and the value is indexed to a constant of type Constant_class_info, representing the class or interface defined by the class file.

Super_class represents the index value of the parent class of the current class, a constant in the constant pool to which the index value points, and if the value of Super_class is zero, the class file must be an object.

interfaces_count,interfaces[] Interfaces_count represents the number of interfaces implemented, and each interfaces[i] corresponds to a constant pool of constants, type Constant_class _info.

Fields_count , fields[] Fields_count represents the number of Field_info tables in a class, and Field_info represents the instance variables and classes of the class

variables, it is important to note that Field_info does not contain fields inherited from the parent class, and the structure of the Field_info is as follows:

Field_info {
U2 access_flags; Access rights, not specifically listed (Private,public ... )
U2 Name_index; A CONSTANT_Utf8_info constant of type that represents the field name
U2 Descriptor_index; Described below
U2 Attributes_count; Number of property sheets
Attribute_info Attributes[attributes_count]; Introduced later
}

The descriptor_index corresponds to a type constant for the constant pool CONSTANT_Utf8_info , and the field descriptor is represented as follows:

Fielddescriptor:
FieldType
FieldType:
BaseType
ObjectType
ArrayType
BaseType:
B byte  //byte a,描述符B
C char
D double
F float
I int
J long
S short
Z boolean
ObjectType:
LClassName;  //String str,描述符Ljava/lang/String;
ArrayType:
[ComponentType//An array representation, two-dimensional [[. Double[][], descriptor [[D
ComponentType:
FieldType

Methods_count , methods[] Methods_count represents the number of method tables, and the method table structure is as follows:

Method_info {
U2 access_flags; Access marks, not specifically listed (Private,public ... )
U2 Name_index; Method name, corresponding to the constant pool
U2 Descriptor_index; Described below
U2 Attributes_count; Number of attribute tables
Attribute_info Attributes[attributes_count]; Introduced later
}

The methoddescriptor is represented as follows:

Methoddescriptor:
(parameterdescriptor* ) Returndescriptor

Parameterdescriptor:
FieldType

Returndescriptor:
FieldType
Voiddescriptor
Voiddescriptor:
V

such as Object m (int i, double D, Thread t) {...} The descriptor of the method is: (Idljava/lang/thread;) ljava/lang/object;.

Attributes_count , attributes[] Attributes_count represents the number of attribute tables. Fields, methods, and class files all have attribute_info tables, but the types of tables are different and the table structure is as follows:

Attribute_info {
    U2 Attribute_name_index;
    
    U1 Info[attribute_length];
}

The following table is a pre-defined property type for the JVM:

--from JVM Specification

The above is roughly what the class file contains, and most of the information in the class is stored in Chang (the compiled instruction is in the Code property of the method). In-depth learning the JVM recommends reading the Java Virtual Machine specification, the article http://www.jb51.net/article/35187.htm the class file with an example.

Understanding the Java class file structure

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.