Java class loading mechanism - class load (load)

The whole process of the Java virtual machine loads classes include: loading, validation, preparation, resolution, initialization . Validation, preparation, called to resolve the connection process. Today we are talking about loaded.
First of all clear "loading" process is a "class loader", do not be confused. Loading process, the virtual machine needs to do the following three things:

  1. Obtaining such a binary byte stream defined by the fully qualified name of a class;
  2. This byte stream represents the static structure into a data structure stored in the method area runtime;
  3. Generating in memory represents java.lang.Class object of this class, as a method for accessing the various data entry areas of this class.

This three requirements is not specific virtual machine, virtual machine implementation and flexibility specific applications are quite large. Take the first that he is only required to "get the definition of such a binary byte stream through a fully qualified name of the class," and does not specify a binary byte stream from Class file acquisition, did not say how to get from where to get. So the java virtual machine's design team set up in the loading phase of a very broad stage, many important java technology have been established in this arena, such as:

  • Obtained from the ZIP package, which is the basis jar, war format
  • Acquired from the network, this scenario is typical application Applet
  • Calculates and generates run-time, the most dynamic proxy technique is used in this scenario, in the java.lang.reflect.Proxy, is to use a binary ProxyGenerator.generateProxyClass proxy class is "* $ Proxy" interface generation for a particular form byte stream
  • Generated from other files, a typical scenario is JSP application, i.e. JSP file generated corresponding to the class Class
  • Read from the database, this scenario is relatively rare

With respect to the other stages of the class loading process, a non-load phase array class (to be exact, the loading phase acquisition operation is a binary stream of bytes, some loading stage is not available binary byte stream) is controllable developer strongest, since the loading stage of the bootstrap class loader may be used to provide a complete system, may be to complete a user-defined class loader, the developer can flow to acquire the control byte by defining its own class loader Information (i.e., override loadClass method of a class).

For an array class, the different situations. Array class by itself does not create a class loader, he was created directly from the java virtual machine. But the relationship between any natural array of classes and class loader is very close, because the array element type class (refers to an array type remove all dimensions) ultimately depend on the class loader to create an array class (below referred to as C) follows the creation process the following rules:

  • If the component type of the array (refers to an array type removing one dimension) is a reference type, then the recursive use of the present loading section defined to load the component type, the array will C class loader assembly of the type the class name space is identified (this is important, you must determine the uniqueness of a class with class loader)
  • If the component type is not an array reference type, Java virtual machine is associated with (e.g., int [] array) the array with the labeled C bootstrap class loader
  • Visibility array class with his consistent visibility component type, if the component type is not a reference type, the visibility of that array class will default to public

After loading stage is completed, the external virtual machine according to a binary stream of bytes required to store the virtual machine in the format area method, area method implemented data storage format defined by the virtual machine itself, the virtual machine specification does not in this predetermined area specific data structures. Then in memory instantiate an object of class java.lang.Class (and not clearly defined in the java heap for the HotSpot virtual machine concerned, Class object is rather special, though he is an object, but on the method area) this object external interface access method as a program area of these types of data.
Part of the load phase and the connection phase (e.g., a portion of the file format byte code verification operation) is carried out crossing, loading stage has not been completed, the connection phase may start, but the clamp in the loading stage of the operation, then any part of the contents of the connection phase, the start time of any of these two phases then maintain a fixed sequence.

Guess you like

Origin blog.csdn.net/huqianlei/article/details/90734570