class file Structure

Source: Internet
Author: User
Tags class definition constant field table modifier uppercase character

1. Overview

A class file is a set of binary streams that are based on 8-bit bytes, and each data item is tightly arranged in a class file in a compact order, with no delimiters added, which makes almost all of the content stored in the entire class file a necessary data for the program to run.

According to the Java Virtual Machine specification, the class file format is stored in a pseudo-structure similar to the C language structure, with only two data types: unsigned number and table.

Unsigned number: belongs to the base data type, with U1, U2, U4, U8 to represent 1, 2, 4, 8 bytes of unsigned numbers, respectively. The unsigned number can be used to describe numbers, index references, quantity values, or string values encoded by UTF-8.

table: A conforming data type consisting of multiple unsigned numbers or other tables as data items, all of which habitually end with "_info".

The entire class file is essentially a single table.


The next step is to look at what data is specifically contained in the class file in a strictly ordered stream of bytes:


One thing we need to be aware of is that cp_info,cp_info represents a constant pool, in which the constant_pool[constant_pool_count-1] is used to represent a constant pool with Constant_pool_count-1 constants. It is here to take the form of arrays, but do not mistakenly think that all the constant pool of constant length is the same, in fact, this place is just to facilitate the description of the way the array is used, but this is not like a programming language there, an int array, each int length is the same. With this in mind, we look back at what each item in the picture represents.


The following first gives a simple Java program, the code compiled into the class file based on the class file structure of the introduction, the code is as follows:

public class testclass{  
    private int num;  
      
    public Int Inc. () {for  
        (int i=0; i<10; i++) {  
            num = i;  
        }  
        return num;  
    }  
      
    public static void Main (string[] args) {  
        new TestClass (). Inc ();  
    }  
      
After compiling the code into a class file, using the 16 binary editor Winhex to open the class file, you can see the internal structure of class as follows:



2, magic number and class file version

class file format table, the first item is a U4 type of data item named Magic. This is because the first 4 bytes of each class file are called Magic Numbers, and its only purpose is to determine if the file is a class file that can be accepted by the virtual machine. Magic numbers are used in many file storage standards for identity recognition. For example, picture format. The use of magic numbers instead of extensions is primarily based on security considerations, because file extensions can be easily manipulated by people. The value of the magic number of the class file is: 0xCAFFBABE. The magic value of the class file is shown in the following figure:

The 4 bytes of the magic number are stored with the version number of the class file: the 5th and 6th bytes are the minor version (Minor version), and the 7th and 8th bytes are the major version numbers (Major version).

The image above represents the 5th and 6th byte values of the version number 0x0000, and the major version number is 0x0032, which is the decimal 50


3, Chang
Immediately following the major and minor version number is the constant pool entry , which is the data type most associated with other items in the class file structure. There are two main types of constants in a constant pool: literal (Literal) and symbolic references (symbolic Reference). Literal comparisons are similar to the concept of constants at the Java language level, such as text strings, constant values that are declared final, and so on. Symbolic references, however, fall into the concept of compilation principles, including the following three types of constants:
1. Fully qualified name of class and interface (Fully qualified name)
2. Name and descriptor of the field (descriptor)
3. The name and descriptor of the method.

To output a constant table using the JAVAP command:

<span style= "FONT-SIZE:14PX;"  
  >d:\jvm>javap-verbose TestClass Compiled from ' Testclass.java ' public class TestClass extends Java.lang.Object SourceFile: "Testclass.java" minor version:0 major version:50 Constant pool:const #1 = Method # 6. #20; Java/lang/object. " <init>:() V const #2 = Field #3. #21;    testclass.num:i Const #3 = Class #22; TestClass Const #4 = Method #3. #20; TestClass. " <init>:() V const #5 = Method #3. #23;    Testclass.inc: () I Const #6 = Class #24;  
Java/lang/object const #7 = Asciz num;  
Const #8 = Asciz I;  
Const #9 = Asciz <init>;  
Const #10 = Asciz () V;  
Const #11 = Asciz Code;  
Const #12 = Asciz linenumbertable;  
Const #13 = Asciz Inc;  
Const #14 = Asciz () I;  
Const #15 = Asciz stackmaptable;  
Const #16 = Asciz main;  
Const #17 = Asciz ([ljava/lang/string;) V; ConsT #18 = Asciz sourcefile;  
Const #19 = Asciz Testclass.java;       Const #20 = Nameandtype #9: #10;//"<init>":() V const #21 = nameandtype #7: #8;//num:i Const #22 = Asciz  
TestClass;  
  
Const #23 = Nameandtype #13: #14;//INC: () I const #24 = Asciz Java/lang/object;  
  {public TestClass (); Code:stack=1, Locals=1, args_size=1 0:aload_0 1:invokespecial #1; Method java/lang/object. "  
  <init> ":() V 4:return linenumbertable:line 1:0 Public Int Inc ();  
   code:stack=2, locals=2, args_size=1 0:iconst_0 1:istore_1 2:iload_1 3:bipush 10 5:if_icmpge 8:aload_0 9:iload_1 10:putfield #2;  
   Field num:i 13:iinc 1, 1 16:goto 2 19:aload_0 20:getfield #2;  
   Field num:i 23:ireturn linenumbertable:line 5:0 line 6:8 line 5:13 line 8:19 stackmaptAble:number_of_entries = 2 Frame_type = 252/* Append */Offset_delta = 2 locals = [int] Frame_  
  Type = +/* Chop */Offset_delta = public static void Main (java.lang.string[]); code:stack=2, Locals=1, args_size=1 0:new #3; Class TestClass 3:dup 4:invokespecial #4; Method "<init>":() V 7:invokevirtual #5; Method Inc: () I 10:pop 11:return linenumbertable:line 12:0 line 13:11} </span >


4 Access Flags
After the constant pool ends, the next two bytes represent the access flag (ACCESS_FLAGS), which identifies the access information at some class or interface level, including whether this class is an interface or a class, is defined as a public type, or is defined as an abstract type If it is a class, whether it is declared final, and so on.

5-Class Index, Parent-class index, and interface index collection
The class index (This_class) and the parent class index (SUPER_CLASS) are data of a U2 type, and the interface index collection (interfaces) is a collection of data of the U2 type, which is determined by the three data in the class file. The class index is used to determine the fully qualified name of this class, and the parent class index is used to determine the fully qualified name of the parent class of the class. Java does not allow multiple inheritance, so there is only one parent class index, except for Java.lang.Object, where all Java classes have no parent index of 0. The interface index collection is used to describe which interfaces this class implements, and all implemented interfaces are arranged from left to right in the index collection of the interface after the implements in the class definition (if the class is an interface, or extends).

6 Field table collection
The Field table (Field_info) is used to describe the variables declared in an interface or class. Fields include class-level variables and instance-level variables, but do not include variables declared inside a method. The information for a field includes: scope (public, private, protected modifier), instance variable or class variable (static modifier), variability (final), concurrency visibility (volatile modifier, whether to force read and write from main memory), Can serialize (transient modifier), field data type (base data type, object, array), field name. In this information, each modifier is a Boolean value, either, or not.
Fully qualified name: If the TestClass class is defined in the COM.CHENZHOU.JVM package, then the fully qualified name of this class is com/chenzhou/jvm/testclass.
Simple name: A simple name refers to a method or field name without a type and parameter decoration, in the example above, the Inc () method in the TestClass class and the simple name of the NUM field are "inc" and "Num", respectively.
Descriptors: Descriptors are used to describe the data type of a field, the parameter list of a method (including quantity, type, and order), and the return value. According to the descriptor rules, the base data type (Byte,char,double,float,int,long,short,boolean) and the void type representing no return value are represented by a uppercase character, while the object is represented by the fully qualified name of the character L plus the object. As shown in the following illustration:


7 Method table Collection
The description of the method in the class file storage format is almost identical to the description of the field. The structure of a method table is like a field table, with access flags, name indexes, descriptor indexes, and attribute table collections included at a time.

8 Property Sheet Collection
the Java Virtual Machine Specification Second Edition defines the attributes that should be recognized by 9 virtual machines, including: Code, Constantvalue, Deprecated, Exceptions, Innerclasses, Linenumbertable, localvariabletable, sourcefile, synthetic.


Reference Source:

JVM Learning Note (iv): class file structure

class file content and constant pool



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.