Chapter 5 ClassLoader source code parsing and classloader source code

Source: Internet
Author: User

Chapter 5 ClassLoader source code parsing and classloader source code

Note: before learning about ClassLoader, first understand the class loading mechanism in Chapter 4.

 

1. Functions of ClassLoader

  • The "loading" phase of the class loading process is completed by the class loader.

 

2. Class Loader Structure

Structure: BootstrapClassLoader (grandfather) --> ExtClassLoader (grandfather) --> AppClassLoader (also called SystemClassLoader) (DAD) --> Custom Class Loader (son)

Link: Look at the positions in the brackets. The two adjacent to each other are parent-child relationships, the former is parent, and the latter is child.

2.1. BootstrapClassLoader

  • Hereinafter referred to as boot
  • C ++
  • It is the parent class of ExtClassLoader, but the getParent () obtained through ExtClassLoader is null (in the class loader section: null refers to boot)
  • Mainly load: E: \ Java \ jdk1.6 \ jre \ lib \ *. jar (the most important thing is: rt. jar)

2.2. ExtClassLoader:

  • Hereinafter referred to as ext
  • Written in java, which is located in the sun. misc package. This package does not exist when you import the source code and needs to be re-written.
  • Mainly load: E: \ Java \ jdk1.6 \ jre \ lib \ ext \ *. jar (eg. dnsns. jar)

2.3. AppClassLoader:

  • Hereinafter referred to as app
  • Written in java, under the sun. misc package
  • Mainly load: jar under the class path

2.4. Custom class loaders:

  • Hereinafter referred to as custom
  • A self-compiled Class Loader must inherit the ClassLoader class or URLClassLoader, and at least rewrite the findClass (String name) method. To break the parent-parent delegation mechanism, you must override the loadClass method.
  • Mainly load: class files in the specified path

 

3. Overall responsibility mechanism

Concept: If ClassLoaderA needs to load class B, but B References class C, then ClassLoaderA needs to load C first, and then load B, "full" means, the Class Loader A that loads B will also load the class referenced by B.

 

4. Parental Delegation

This is also the whole process for the class loader to load a class.

Process: If I load A Class A from the class path,

1) The app first checks whether A has been loaded. If yes, the app returns the result directly;

2) If NO, go to ext to check whether A has been loaded. If yes, return directly;

3) If NO, go to boot to check whether A has been loaded. If yes, return directly;

4) if no, load the boot. If the class with the specified name is found under E: \ Java \ jdk1.6 \ jre \ lib \ *. jar, load and end;

5) if not found, boot loading fails;

6) ext starts loading. If the class with the specified name is found in E: \ Java \ jdk1.6 \ jre \ lib \ ext \ *. jar, the loading ends;

7) if not found, ext loading fails;

8) load the app. If the class with the specified name is found under the class path, load and end;

9) If no exception is found, the ClassNotFoundException is thrown.

Note:

  • In the above process (1) 2) 3) 4) 6) 8), determine whether to perform the "resolution" process (For details, refer to Chapter 4 class loading mechanism)
  • The class loading process only involves parent-parent delegation, and there is no downward query and loading. Assume that ext is in E: \ Java \ jdk1.6 \ jre \ lib \ ext \*. when a class is loaded under jar, the entire query and loading process is irrelevant to the app.
  • Assuming that A is successfully loaded, the class will be cached in the current class loader Instance Object C. The key is (A, C) (where A is the full Class Name of the class, C is the class loader object instance for loading A), and value is the corresponding java. lang. class Object
  • 1) 2) 3) search from the cache of the corresponding class loader Instance Object
  • The purpose of caching is to prevent the same class from being loaded twice.
  • Using (A, C) as the key is to isolate classes. Assume that A class loader B also loads A, and the key is (A, B ), then the two A are different. How does this happen?
    • Suppose there are two custom class loaders, custom1 and custom2. They are sibling and load A at the same time. This is possible.

Summary:

  • Check whether the class with the specified name has been loaded from the bottom up; load the class from the top down. (After any step is successful, the class loading process will be aborted)
  • Advantages of parental delegation: assume that you have compiled a java. lang. object class, which is put under the class path after compilation. At this time, there are two Object classes in the system, one being rt. jar, one is under the class path. During the class loading process, when the Object class is to be loaded according to the full class name, the boot will load rt according to the parent-parent delegation. the Object class under jar. This is the end of the method, that is, the Object class under the class path is not loaded. This ensures that classes in the system are not chaotic.

 

5. Source Code

1/*** load the class according to the specified binary name. 3 * Step 3: 4 * If I load A Class A and 5*1 from the class path, the app will first check whether A (findLoadedClass (name) has been loaded )), if yes, return directly; 6*2) If NO, go to ext to check whether A (parent. loadClass (name, false). If yes, return directly. 7 * findBootstrapClassOrNull (name) 3) 4) 5) are all methods 8*3. If no, check whether A has been loaded in boot. If yes, return directly; 9*4) If no, load the boot file. If it is in E: \ Java \ jdk1.6 \ jre \ lib \*. if a class with the specified name is found in jar, load the class and end it. 10*5) If no, boot fails to be loaded. 11 * findClass (name) 6) 7) 8) 9) these are all Methods 12 * The defineClass method is called in findClass. This method will generate the java of the current class. l Ang. class Object 13*6) ext starts to load. If it is in E: \ Java \ jdk1.6 \ jre \ lib \ ext \*. if a class with the specified name is found in jar, load and end; 14*7) if not found, ext loading fails; 15*8) app loading, if a class with the specified name is found in the class path, the class is loaded and ended. 16*9) If no class is found, the exception ClassNotFoundException17 is thrown. * Note: In the above process, 1) 2) 3) 4) 6) 8) back, you must determine whether to perform the "resolution" process 18 */19 protected synchronized Class <?> LoadClass (String name, boolean resolve) 20 throws ClassNotFoundException {21 Class c = findLoadedClass (name ); // check whether the class to be loaded has been loaded 22 if (c = null) {// No 23 try {24 if (parent! = Null) {25 // if the parent loader is not boot, call loadClass (name, false) recursively 26 c = parent. loadClass (name, false); 27} else {// The parent loader is boot28/* 29 * returns a class loaded by boot; 3) 30 * if not, in E: \ Java \ jdk1.6 \ jre \ lib \*. jar search 4) 31 * If the class is not found in the bootstrap class loader search range, return null 5) 32 */33 c = findBootstrapClassOrNull (name ); 34} 35} catch (ClassNotFoundException e) {36 // The parent classloader cannot complete the loading request 37} 38 if (c = null) {39 // if the parent class loader is not found, call findClass (name) of itself (this includes ext and app) to find the class 40 c = findClass (name ); 41} 42} 43 if (resolve) {44 resolveClass (c); 45} 46 return c; 47}View Code

Note:

  • Most of the Methods referenced in this section of code are actually native methods.
  • The findClass method defines the Class as follows:/*** find the Class of the specified binary name * This Class should be overwritten by the Implementation Class of ClassLoader */protected Class <?> FindClass (String name) throws ClassNotFoundException {throw new ClassNotFoundException (name );}View Code
  • For findClass, you can view URLClassLoader. findClass (final String name). The defineClass method is referenced, in which the binary byte stream is converted to a java. lang. Class Object.

 

Appendix: Recursion

Recursion is based on stack implementation.

The above Code cannot be seen clearly if it is unclear the meaning of recursion.

Explanation:

  • The app_loadClass () method is executed to ext_loadClass (). At this time, the remaining findClass () in app_loadClass () will be pressed down in the stack;
  • Execute ext_loadClass (). When findBootstrapClassOrNull (name) is executed, the remaining findClass () in ext_loadClass () will also be pressed down from the top of the stack. In this case, ext_loadClass () _ findClass () only above app_loadClass () _ findClass;
  • Then execute findBootstrapClassOrNull (name). After the boot Check is completed and the load is not successful, the boot method leaves the top of the stack;
  • Then execute the ext_loadClass () _ findClass () at the top of the stack ()
  • Then execute the app_loadClass () _ findClass () at the top of the stack ()

In this way, the dual-parent delegation mechanism is completed.

Recursion is too annoying. Try not to use it in actual development!

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.