Java JVM Learning Note III (class file Inspector)

Source: Internet
Author: User

Welcome loading Please indicate Source: Http://blog.csdn.net/yfqnihao

In the previous study we know that the class file is loaded by the type loader, but before or after loading the class file, the class file actually needs to be verified, which is today's learning topic,class file validator.

class file validator to ensure that the class file content has the correct internal structure, and the Java Virtual Machine class File Checker verifies the file before the bytecode executes, rather than verifying it in execution
The class file validator will perform four independent scans to complete the calibration work

The class file validator is divided into four separate scans to complete the verification.

First trip

When loading a sequence of bytes, this is the legitimacy of verifying the structure of the class file, For example, when you use the Copy command under Windowns to merge A. class file and a JPG file, the JVM will find that the class file has been hacked, the length of the file is not correct, and throws an exception when loading the class file!
So this check is happening on the binary data,

Second trip

Scan occurs in the method area, mainly for, semantic, lexical and grammatical analysis, that is to check whether this class can be compiled smoothly!

Third Trip

byte code Check
There are two poorly understood concepts involved in this check, the first is the byte stream and the second is the stack frame .
When executing bytecode, an opcode is executed once, and the Java Virtual machine forms the execution thread, and each thread has its own Java stack, which is what we call the stack frame. Each method has a stack frame.
It would be easier to understand the two concepts if the people who studied the assembly
byte stream = opcode + operand, which can be seen here as the pseudo-instruction + operand in the assembly, because the opcode is actually the "Assembly pseudo-directive" that is recognized by the JVM, and the concept of operands and the addition of data types in the assembly are not much different
Focus on the stack frame, stack frame is actually very good understanding, the stack frame has a local variable stack and the operation of the stack , these two pieces of memory is the time to put the data, the operand stack is used to hold the byte code instruction execution intermediate result, result or operand, and local variable area, is used to store local variable parameters, which is a good understanding
The verification process of this bytecode is the legal process of the byte stream, which is the validity of the checksum operation code.

and Java's class file encoding we call it bytecode, because each tune operation instruction is only one byte, in addition to two exceptions, all the opcode and their operands are byte-aligned, which makes the byte stream at the time of transmission with a small, more advantageous, the two exceptions are such a number of operation codes, Between the opcode and their operands is one to three bytes in the sky so that the operands are aligned in bytes.

Here is a diagram that describes the structure of the stack frame

Four trips

Check for symbol reference
Since most implementations of the JVM are deferred or dynamically linked, lazy loading means that when the JVM loads a Class A, if a class has references to other Class B, the virtual machine does not load the referenced class B into memory at the same time, but waits until it executes.
And this quoted Class B in the reference to its expression in Class A is mainly registered in the symbol table , and the process of the IV is when the need to use the referenced class B, the reference Class B in the reference Class A symbol reference name to the direct reference in memory
So the time of the fourth trip is unpredictable and takes place in the method area. The whole process is called dynamic connectivity .
Can be divided into two simple steps .
1. Find the referenced class (load it if necessary)
2. Replace the symbolic reference with a direct reference, such as a pointer to a class, field, or method, and use the direct reference directly when you need to use the referenced class next time, without having to load it again.

This process actually finds its traces in the loadclass of the ClassLoader class. Let's put out loadclass this method, and then we'll do a brief analysis.

    protected synchronizedClass<?> loadclass (String name,Booleanresolve)throwsClassNotFoundException {//First , check if the class has already been loadedClass C =Findloadedclass (name); if(c = =NULL) {        Try {        if(Parent! =NULL) {C= Parent.loadclass (Name,false); } Else{C=FINDBOOTSTRAPCLASS0 (name); }        } Catch(ClassNotFoundException e) {//If still not found, then invoke Findclass in order//To find the class.c =Findclass (name); }    }    if(Resolve) {resolveclass (c); }    returnC; }

LoadClass has two parameters, the first parameter is the fully qualified name of the class, the second parameter is the point we want to say, when this parameter is true, the LoadClass method executes the Resolveclass method, which is to replace the symbolic reference in the class with a direct reference. The method that is finally called is a local method, ResolveClass0.

Here is another point to note, Class.forName This static method we also often used to load the Class file byte code, then it and classloader what is the difference?

The difference is whether or not the Resolveclass method is executed, Class.forName always commits to connect and initialize the symbolic connection, and LoadClass has no such commitment.

Summarize:

The first scan , when the class is loaded, checks the internal structure of the class file to ensure that it can be compiled in a normal and secure
The second and third trips are made during the connection process, and these two trips are basically a syntax check, a lexical check
The fourth trip is the parsing of symbolic references and direct references, and this check confirms that the referenced classes, fields, and methods do exist.

Java JVM Learning Note III (class file Inspector)

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.