JVM learning (1): class loading mechanism

Class File Structure Analysis:

Online to see articles on the Java Class file parsing write more comprehensive:

https://www.jianshu.com/p/247e2475fc3a

http://tech.dianwoda.com/2018/03/28/jvm-classjie-xi-wen-jian-ge-shi/

https://blog.csdn.net/hywo125/article/details/90770393

 

Class loading mechanism:

Step One: Load

1. Obtain a binary byte stream

2. static storage structure into a run-time data structure area method

Run-time data area are: Methods District, Heap], [virtual machine stack, native method stacks, program counter]

3. The stack generates a class object in Java, as an access method to the inlet region

 

Step Two: Verify

1. Verify Class File ID: Orlando Magic Number: cafebabe

2. Verify that the Class file version number

3. Verify the constant pool (Constant type, constant type data structure is correct, UTF-8 meets standards)

Each portion 4.Class file (table field, method tables, etc.) are correct

The metadata validation (parent class verification, validation inheritance, final field validation, etc.)

6. bytecode verification, the most complex step (verification command)

7. The reference symbol verification (verification by reference symbol to find whether fields, methods, classes)

 

During the verification of error will be reported:

IncompatibleClassChangeError ---> Class File Error

Unsupported major.minor version xx.x ---> version has problems (often referred to JDK version issue)

IllgalAccessError; NoSuchFieldError; NoSuchMethodError etc.

 

The third step: Preparation

Allocate memory for the set class variables and class variables initialization phase, only static class variables for memory allocation

 

例:(1)static int n=1;(2)static final int n=2;

At initialization difference:

(1) After the initialization, the initialization value is 0, not 2, because at that time did not execute any Java methods, static blocks and finally called CLINT

(2) corresponds to the case where the constant pool (ConstantValue), n in the preparation phase has been assigned to 2

 

Instance variables and class variables differences:

Class variables: generally referred to as static variables

Examples of variables: when an object is instantiated, the instance variables created with object creation, along with the destruction of the object and destroy

 

Step Four: Parse

Parsing of symbolic references, the symbolic reference becomes a direct reference

Direct reference: to the target is a pointer or offset

 

Mainly involves: classes, interfaces, fields, methods and other

The following types:

CONSTANT_Class_info

CONSTANT_Field_info

CONSTANT_Methodref_info

CONSTANT_InterfaceMethodred_info

CONSTANT_MethodType_info

CONSTANT_MethodHandler_info

CONSTANT_InvokeDynamic_info

 

1. The field of analysis:

In this class to find there is no matching field ---> If the class has an interface, the interface to find a match to the upper field ---> Search parent

E.g:

class A extends B implements C,D{
private String str;
}

Search order: first in class A (the present type) to find, and then find from C, Class D (parent interface), the next to Class B (parent) to find, finally to find the class Object

Can not find will complain: java.lang.NoSuchFieldError

If you find but do not have permission (private): java.lang.illegalAccessError

 

2. The method of class analysis:

class A extends B implements C,D{
private void test(){
......
}
}

In this class there are ways to find there is no matching method if the parent does not go looking for a match, did not find the interface method to go inside to find a list of matching

If this class is not found, the interface to find, then this class is explained: abstract abstract class

If it is not found error: java.lang.NoSuchMethodError

If you find but do not have permission (private): java.lang.illegalAccessError

 

3. The interface method of analysis:

In this class there is a method which does not find a match, then in the parent recursive search interface

If it is not found error: java.lang.NoSuchMethodError

The interface is generally not private, so there is no illegalAccessError

 

Step five: initialization

Initialization <init> class

<Clinit> static variables, static initialization block; if no static variables and the code block, then no clint

For example the following code:

class A{
 static int i=2;//clint
 static {//clint
   System.out.println("Hello World!");
 }
 int n;//init
}

 

Step 6:

Need to explain

 

Step seven: Uninstall

Behind garbage collection to learn

 

Above verification, preparation, also known as the three-step analytical connection

Guess you like

Origin www.cnblogs.com/xuyiqing/p/12466415.html