java-class loading mechanism (2)-the process of class loading

Source: Internet
Author: User

The loading process of the class

One, load

Definition: Gets the byte stream of the. class file, into the data structure of the method area, creating a Class object in memory as the data access portal for that class

Content: "1", gets the binary byte stream based on the fully qualified name of the class

"2" to convert the static storage structure represented by the byte stream into the runtime data structure of the method area

"3", which generates the Java.lang.Class object of the class in memory as a data access entry for the class of the method area (note: Memory refers to not necessarily a heap, such as a hotspot virtual machine is stored in the method area)

Get the byte stream mode from A. class file:

"1", the ZIP packet reads such as: Jar,ear,war and other format files

"2", network access such as: applets

"3", run-time calculation generated such as: Dynamic agent, java.lang.reflect.Proxy using Proxygenerator.generateproxyclass for a specific interface to generate a proxy class form "* $Proxy" binary byte stream

"4", other files such as: JSP file generation corresponding class class

"5", database such as: Middleware server SAP Netweaver

Other:

①, during class loading, a class object is created in memory, where the memory is not necessarily heap memory, depending on the virtual machine, such as: The hotspot virtual machine is stored in the method area

②, the load phase and the connection phase are interleaved, and bytecode validation may begin to load the end-of-connection phase

③, class loading can be divided into non-array classes and arrays of classes loaded

Non-array classes: Through 1, the system provides class loader 2, a user-defined class loader (overriding the LoadClass method of the ClassLoader)

Array classes: Created without the ClassLoader, created directly by the virtual machine

Array class creation rules: 1. If the array component is a reference type, the array is identified on the class name of the class loader that loads the reference type

2. If the array component is not a reference type, the Java virtual machine marks the array as associated with the Bootstrap class loader

3. Array visibility is consistent with its component types, such as not being public by default for reference types

Two, verify (connect)

Definition: Verify that the information contained in the byte stream of a class file is compliant with virtual machine requirements to avoid compromising virtual machine security

Why you need to verify: class files are not necessarily compiled by Java source code, for example: using the hexadecimal editor to write class files directly, and these non-Java source code compiled from the class file is not necessarily the correct format

Content: "1", File format verification (file)

Checks if the byte stream conforms to the class file format specification and can be processed by the current version of the virtual machine

such as: ①, whether the magic number 0xCAFEBABE begins

②, whether the primary and secondary versions are within the current Virtual machine processing range

③, whether the constant pool has an unsupported constant type

④, whether the index value pointing to a constant points to a non-existent constant and a constant of a non-conforming type

Whether the constants of type ⑤,constant_utf8_info conform to UTF8 encoding

... ...

"2", Meta data validation (data type)

Semantic analysis of bytecode information in accordance with the Java language Specification

such as: ①, whether there is a parent class (except Java.lang.Object)

②, whether the parent inherits classes that are not allowed to inherit (final decorated Class)

③, whether non-abstract classes implement all the methods required in the parent class and interface

④, whether the class's fields and methods conflict with the parent class (for example, overriding the parent class final field, the wrong overload, and so on)

... ...

"3", byte Code verification (method)

Determine whether program semantics are legitimate through data flow and control flow analysis

such as: ①, the data type of the operand stack and the instruction code sequence can work together without any type inconsistency

②, the jump instruction does not jump to the outside of the method body

③, a type conversion in a method body is valid, such as assigning a parent object to a subclass data type that is not legal

Turn off stackmaptable for byte-code verification optimized configuration:

-xx:-usesplitverifier

-xx:+failovertooldverifier

"4", Symbol reference validation

Matching checks for information other than the class itself

Objective: To ensure the normal execution of analytic actions

Time taken: Symbolic reference validation when virtual machines convert symbolic references to direct references in the parsing phase

Checksum content: ①, the fully qualified name described by a string in a symbol reference can find the corresponding class

②, specifies whether the class has a field descriptor that conforms to the method, and the methods and fields described by the simple name

③, the class in the symbol reference, the field, whether the accessibility of the method can be accessed by the current class

... ...

Possible errors: Java.lang.NoSuchFieldError, Java.lang.NoSuchMethodError, etc.

Three, prepare (connect)

Definition: Allocates memory in the method area for static variables of the class, sets the initial 0 value

    

Other:

public static final int value = 123; Prep stage: set to 123

public STAITC int value = 123; Prep stage: Set to 0, the putstatic instruction with value 123 is compiled and stored in the class constructor <clinit> () method, initialization stage: set to 123

java-class loading mechanism (2)-the process of class loading

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.