JVM - class file structure

  • Class File Structure
    • I. Overview
    • Two Class file structure summary
      • 2.1 magic number
      • 2.2 Class file version
      • 2.3 constant pool
      • 2.4 Access logo
      • 2.5 the current class index, and the index of the parent class interface index set
      • Field tables set 2.6
      • 2.7 Method set of tables
      • 2.8 attribute table collection

Class File Structure

I. Overview

  In Java, JVM code can understand is called 字节码(ie extension  .class file), it does not face any particular processor, only for the virtual machine. Java byte code language by the way, to a certain extent, solve the low efficiency of traditional interpreted language problems, while retaining the features of interpreted languages portable. So when running a Java program more efficient, and, because bytecode is not directed to a particular machine, therefore, no need to recompile the Java program can run on many different computer operating systems.

  Clojure (a dialect of Lisp language), Groovy, Scala and other languages are run on the Java Virtual Machine. The following figure shows the different languages are compiled with different compilers abnormal .classfile final run on the Java Virtual Machine. .classBinary format files can use  WinHex  view.

Java Virtual Machine

It can be said .classfiles are different languages an important bridge between the Java virtual machine, but also support cross-platform Java very important reason.

Two Class file structure summary

The Java Virtual Machine Specification, a single class file ClassFile structures:

{ClassFile 
    u4 Magic; logo // Class file 
    U2 minor_version; // Class version number of small  U2 major_version; // Class of major version number constant_pool_count U2; // constant pool constant_pool number cp_info [constant_pool_count - 1]; // access_flags constant pool U2; // class access this_class marker U2; // current class super_class U2; // parent interfaces_count U2; // the interfaces Interface U2 [interfaces_count]; // a class can implement multiple interfaces fields_count U2; / / field_info fields class field attribute file [fields_count]; // there may be a class field u2 methods_count; number of files // class methods method method_info [methods_count]; // a class can have a plurality of methods u2 attributes_count;// number of attributes such attributes Attributes table attribute_info [attributes_count]; // set attribute table}

The following details about some of the components Class file structure involved.

Class file byte code structure organizational chart (previously saved online, very good, original source unknown):

Byte code class files schematic structural organization

2.1 magic number

    u4 magic; logo // Class file

Class first four bytes of each file is called magic number (Magic Number), its only purpose is to determine whether the file is a file that can be Class virtual machine receives.

Programmers often like to use some special numbers indicate the fixed document type or other special meaning.

2.2 Class file version

    minor_version U2; // Class version number of small 
    U2 major_version; // Class of major version number

Then the magic number of four bytes of storage is the version number of Class file: The fifth and sixth are the minor version number, seventh and eighth is the major version number.

High version of the Java Virtual Machine can perform low version of the compiler generated Class files, but lower version of the Java Virtual Machine can not perform high version of the compiler-generated Class file. So, when we actually developed to ensure the development of JDK version of the JDK version and consistent production environment.

2.3 constant pool

    constant_pool_count U2; // the constant pool number 
    cp_info constant_pool [constant_pool_count - . 1]; // the constant pool

紧接着主次版本号之后的是常量池,常量池的数量是 constant_pool_count-1(常量池计数器是从1开始计数的,将第0项常量空出来是有特殊考虑的,索引值为0代表“不引用任何一个常量池项”)。

常量池主要存放两大常量:字面量和符号引用。字面量比较接近于 Java 语言层面的的常量概念,如文本字符串、声明为 final 的常量值等。而符号引用则属于编译原理方面的概念。包括下面三类常量:

  • 类和接口的全限定名
  • 字段的名称和描述符
  • 方法的名称和描述符

常量池中每一项常量都是一个表,这14种表有一个共同的特点:开始的第一位是一个 u1 类型的标志位 -tag 来标识常量的类型,代表当前这个常量属于哪种常量类型.

类型 标志(tag) 描述
CONSTANT_utf8_info 1 UTF-8编码的字符串
CONSTANT_Integer_info 3 整形字面量
CONSTANT_Float_info 4 浮点型字面量
CONSTANT_Long_info 长整型字面量
CONSTANT_Double_info 双精度浮点型字面量
CONSTANT_Class_info 类或接口的符号引用
CONSTANT_String_info 字符串类型字面量
CONSTANT_Fieldref_info 字段的符号引用
CONSTANT_Methodref_info 10 类中方法的符号引用
CONSTANT_InterfaceMethodref_info 11 接口中方法的符号引用
CONSTANT_NameAndType_info 12 字段或方法的符号引用
CONSTANT_MothodType_info 16 标志方法类型
CONSTANT_MethodHandle_info 15 表示方法句柄
CONSTANT_InvokeDynamic_info 18 表示一个动态方法调用点

.class 文件可以通过javap -v class类名 指令来看一下其常量池中的信息(javap -v class类名-> temp.txt :将结果输出到 temp.txt 文件)。

2.4 访问标志

  在常量池结束之后,紧接着的两个字节代表访问标志,这个标志用于识别一些类或者接口层次的访问信息,包括:这个 Class 是类还是接口,是否为 public 或者 abstract 类型,如果是类的话是否声明为 final 等等。

类访问和属性修饰符:

Access classes and attribute modifiers

我们定义了一个 Employee 类

package top.snailclimb.bean;
public class Employee {
   ... }

通过javap -v class类名 指令来看一下类的访问标志。

Access logo View class

2.5 当前类索引,父类索引与接口索引集合

    u2             this_class;//当前类
    u2             super_class;//父类
    u2             interfaces_count;//接口 u2 interfaces[interfaces_count];//一个雷可以实现多个接口

  类索引用于确定这个类的全限定名,父类索引用于确定这个类的父类的全限定名,由于 Java 语言的单继承,所以父类索引只有一个,除了 java.lang.Object 之外,所有的 java 类都有父类,因此除了 java.lang.Object 外,所有 Java 类的父类索引都不为 0。

  接口索引集合用来描述这个类实现了那些接口,这些被实现的接口将按implents(如果这个类本身是接口的话则是extends) 后的接口顺序从左到右排列在接口索引集合中。

2.6 字段表集合

    u2             fields_count;//Class 文件的字段的个数
    field_info     fields[fields_count];//一个类会可以有个字段

字段表(field info)用于描述接口或类中声明的变量。字段包括类级变量以及实例变量,但不包括在方法内部声明的局部变量。

field info(字段表) 的结构:

Structure fields of the table

  • access_flags: 字段的作用域(public ,private,protected修饰符),是实例变量还是类变量(static修饰符),可否被序列化(transient 修饰符),可变性(final),可见性(volatile 修饰符,是否强制从主内存读写)。
  • name_index: 对常量池的引用,表示的字段的名称;
  • descriptor_index: 对常量池的引用,表示字段和方法的描述符;
  • attributes_count: 一个字段还会拥有一些额外的属性,attributes_count 存放属性的个数;
  • attributes[attributes_count]: 存放具体属性具体内容。

上述这些信息中,各个修饰符都是布尔值,要么有某个修饰符,要么没有,很适合使用标志位来表示。而字段叫什么名字、字段被定义为什么数据类型这些都是无法固定的,只能引用常量池中常量来描述。

字段的 access_flags 的取值:

access_flags the field Value

2.7 方法表集合

    u2             methods_count;//Class 文件的方法的数量
    method_info    methods[methods_count];//一个类可以有个多个方法

methods_count represents the number of methods, while the method table method_info representation.

Class file storage format is described and description of the methods employed for the field is almost exactly the same way. The method as field Structure Table tables, sequentially comprising the access flag, the name of the index, the index descriptor, several set attribute table.

method_info (table method) Structure:

Structural approach table

access_flag method table of values:

access_flag value method table

Note: Because the volatilemodifier and transientmodifier can not be modified method, the method of access flag table does not correspond to these two signs, but adds synchronized, native, abstractand other key modification methods, so there is more for those keywords corresponding symbol .

2.8 attribute table collection

   u2 attributes_count; number of attributes such // attribute table 
   attribute_info Attributes [attributes_count]; // set attribute table

  Can bring their own set of Class file attribute table, field table, the method table, for certain scenarios describe proprietary information. Class files and the order of other data items required different lengths and content restrictions set attribute table is slightly looser, the attribute table is no longer required each having strict order, and they do not duplicate existing attribute name, and any achieve compilers can write their own definition of the attribute information to the attribute table, ignores attribute it does not recognize the Java virtual machine is running.

 

Source: https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/jvm/%E7%B1%BB%E6%96%87%E4%BB%B6%E7%BB%93%E6 % 9E% 84.md

Guess you like

Origin www.cnblogs.com/myseries/p/10979516.html