Java Class file format (8)

Source: Internet
Author: User


In the first article in this column, I deeply understand what a Java Virtual Machine is and what a virtual machine is. This blog is an overview of JVM. In the subsequent articles, I have been explaining the class file format. In this blog today, we will continue to explain other information in the class file. This article describes the attributes in the last part of the class file ). The attribute here and the attribute in the source file are not a concept. In the source file, the fields defined in the class are also called attributes. The attributes in the class file can be seen as the data structure that stores some additional information. Next we will introduce the attributes.


Attributes_count and attributes in the class file
Attributes_count is located under methods in the class file. It occupies two bytes and stores an integer, indicating the number of attributes in the class file.
Under attributes_count, attributes can be viewed as an array. Each array item is an attribute_info, and each attribute_info represents an attribute. Attributes contains attributes_count attribute_info.

It should be noted that the attribute will appear in multiple places, not only in the top-level ClassFile, but also in the data items in the class file, as shown in field_info, information used to describe specific fields. It can also appear in method_info to describe some information about specific methods. (Field_info and method_info have been introduced in the previous blog. If you do not understand them, refer to the previous blog: learn more about the Java Class file format (7 ))
The approximate format of attribute (attribute_info) is as follows:

Attribute_name_index occupies two bytes. It is an index pointing to a data item in the constant pool. It points to a CONSTANT_Utf8_info, which stores the name of the current attribute.
The four bytes under attribute_name_index are referred to as attribute_length, which indicates the length of the current attribute. This length does not include the first six bytes, that is, only the length of the actual attribute information (that is, info.

The data under attribute_length is info, and its length is specified by attribute_length mentioned above. It stores real attribute data.



Next we will introduce some important attributes in sequence, and those that are not very important will be included in the document.

SourceFile attribute in ClassFile
First, we will introduce a simple attribute: SourceFile. This attribute appears in the top-level class file. It describes the source file from which the class is compiled. Note that it describes the source file rather than the class. A source file can contain multiple classes. The format is as follows:
As mentioned above, attribute_name_index points to a CONSTANT_Utf8_info in the constant pool. This CONSTANT_Utf8_info stores the name string of this attribute, that is, "SourceFile ".
Attribute_length is the length of the attribute information. Here it is 2, because the info of this attribute is two bytes.

Sourcefile_index occupies two bytes, Which is why attribute_length is 2. Sourcefile_index points to a CONSTANT_Utf8_info in the constant pool. This CONSTANT_Utf8_info stores the file name of the source file that generates the class. The file name here does not include the path.
The following example shows the sample code:
package com.jg.zhang;public class Person {int age;int getAge(){return age;}}

Decompiled information:
public class com.jg.zhang.Person  SourceFile: "Person.java"Constant pool:.........  #20 = Utf8               SourceFile  #21 = Utf8               Person.java.........

The SourceFile: "Person. java" line in The Decompilation result is a simple representation of the SourceFile attribute. It can be viewed as a readable attribute_info. The CONSTANT_Utf8_info of item 20th in the constant pool below describes the attribute name (attribute_name_index) of this attribute, and the CONSTANT_Utf8_info of item 21st describes the file name of the source file.
The following is a legend. Note that the dotted line represents the constant pool area:




InnerClasses attribute in ClassFile
InnerClasses is an attribute that exists in the top-level class file. It describes the relationship between internal classes and peripheral classes. This is a relatively complex attribute, because each class may have multiple internal classes, and these internal classes may also have internal classes with multiple layers of nesting. The InnerClasses attribute in the peripheral class must describe all its internal classes, and the InnerClasses attribute in the internal class must also describe its peripheral classes.
This attribute is relatively complex, but it does not make much sense for us to understand the class file. So let's just give a brief introduction. For more information about this attribute, see pages 144th to 166 of deep Java virtual machine.
The structure of this attribute is as follows:


Attribute_name_index and attribute_length are not described too much. They are the same as those described above.

Number_of_classes describes the number of internal classes.
Classes can be seen as an array. Each item in this array is an inner_class_info, and each inner_class_info is a description of an internal class. The structure of each inner_class_info is as follows:



Synthetic attributes
The Synthetic attribute can appear in filed_info, method_info, and the top-level ClassFile, indicating this field. Methods or classes are not generated by user code (that is, they do not exist in the source file ), it is automatically added by the compiler. For example, the compiler adds a field to the internal class, which is a reference to the external class object. If a constructor is not defined, the compiler automatically adds a non-parameter constructor <init>. If static fields or static code blocks are defined, the compiler also adds a static initialization method <clinit> based on the actual situation. In addition, some mechanisms, such as dynamic proxy, will automatically generate bytecode files at runtime, because these classes are not compiled by the source files, therefore, the class files of these classes have a Synthetic attribute.
Its structure is as follows:


As you can see, it does not have the real attribute data info. It is just a symbolic attribute used to indicate the field where it is located. Methods or classes are automatically added by the compiler.
The following describes the instance code. The source code is as follows:
package com.jg.zhang;public class Person {static{System.out.println("static");}int age;int getAge(){return age;}}

The decompiled information is as follows:
{  int age;    flags:  static {};    .........  public com.jg.zhang.Person();        .........  int getAge();    .........}

The Decompilation result shows that the compiler automatically generates static initialization methods and constructor methods. It may be because the Synthetic attribute is optional (that is to say, the compiler of a certain version can choose not to add the Synthetic attribute), so no Synthetic attribute is found in the decompiled results.

ConstantValue attribute
The ConstantValue attribute appears in field_info in the class file, that is, it is a field-related attribute. Only one ConstantValue attribute can appear in each field_info. In addition, note that only static fields can have the ConstantValue attribute. This static field can be either final or final.
This attribute provides another Initialization Method for static variables. There are two ways to initialize static variables: one is to get the ConstantValue attribute, another method is static initialization. <clinit> different compilers and virtual machines can have different implementation methods. However, if the Virtual Machine decides to use the ConstantValue attribute to assign values to a static variable, the value assignment must be prior to the execution of the <clinit> method.
In addition, only static variables of the basic data type or String type can have the ConstantValue attribute. The reasons are described below.
The following describes its structure:


Attribute_name_index and attribute_length are not described too much. They are the same as those described above. Attribute_length is 2.

Under attribute_length is constantvalue_index, which is an index pointing to a data item in the constant pool. The data items in this constant pool store the values of the current field.
The data items in this constant pool can be different types of data items depending on the fields described by field_info. If the current field is of the byte, short, char, int, or boolean type, the data item in the constant pool to be pointed to will be a CONSTANT_Integer_info. If the current field is a long type field, the data item in the constant pool to be pointed will be a CONSTANT_Long_info. If the current field is a String type field, the constant pool data item pointed to is a CONSTANT_String_info. It must be noted that although java supports byte, short, char, and boolean types, JVM does not support these types, as shown in the class file, the constant pool in the class file does not have data items that correspond to these data types. These data types are treated as int during JVM execution, as shown in the class file, these types correspond to the CONSTANT_Integer_info data item in the constant pool.
This also explains why only static constants of the basic data type and String type have the ConstantValue attribute. Because constantvalue_index is only an index pointing to the constant pool, constants of other reference types will not exist in the constant pool.
The instance code is as follows:
package com.jg.zhang;public class Person {static final int a = 1;int age;int getAge(){return age;}}

The Decompilation result is as follows:
......Constant pool:    #7 = Utf8               ConstantValue   #8 = Integer            1 {  static final int a;    flags: ACC_STATIC, ACC_FINAL    ConstantValue: int 1    .........}

As you can see, field a in the source file is static final, so the compiler generates the ConstantValue attribute for filed_info of this field. This attribute is shown as follows. Note that the dotted range represents the constant pool area:




Deprecated attributes
The Deprecated attribute can exist in filed_info, method_info, and the top-level ClassFile, indicating that the field, method, or class is out of date. This attribute is used to support the @ deprecated annotation in the source file. That is to say, if the @ deprecated annotation is marked for a field, method or class in the source file, the compiler will generate a Deprecated attribute for this field, method or class in the class file.
The format of the Deprecated attribute is as follows:



Like the preceding attribute, the attribute_name_index attribute points to CONSTANT_Utf8_info in a constant pool. This CONSTANT_Utf8_info stores the attribute name "Deprecated ".
Attribute_length is always 0, because this attribute is only a flag information used to represent fields, methods, and classes that are out of date without any substantive attribute information.
The following code is used as an example. The Code is as follows:
package com.jg.zhang;public class Person {int age;@Deprecatedint getAge(){return age;}}

@ Deprecated is used in the getAge method. The following information is related to decompilation:
  ......  Constant pool:  ......  #18 = Utf8               Deprecated  ......{  ......  int getAge();    flags:    Deprecated: true    ......}

As you can see, there is a Deprecated: true line in the getAge method information, which indicates that the compiler adds the Deprecated attribute to the method_info of the getAge method. The CONSTANT_Utf8_info in the 18th item of the constant pool stores the attribute name "Deprecated" of the Deprecated attribute ".
Below is, the range of dotted lines indicates the constant pool area:



Summary
This article ends. This article mainly describes some attributes in the class file. These attributes can appear in peer places in the class file to describe some other information.
In the next blog, we will continue to explain other attributes. The attributes to be explained in the next blog are relatively important because they are mainly related to methods. So far, we have explained most of the information in the class file, including the constant pool, this_class, super_class, field_info, and method_info. Although method_info describes a method, the information about method_info only describes the method name, descriptor, and other signature information. However, methods also contain a lot of important information, such as bytecode instructions, exception handling blocks, and exceptions thrown by method declarations. How are these important information described in the class file? The next blog will reveal the answer, so stay tuned.



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.