Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The principle of Java Parental appointment Mechanism

Shulou Source: shulou.com Published: 2022-06-02 05:07:59 09月22日 Update

This article mainly explains "the principle of Java parent appointment mechanism". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the principle of Java parent appointment mechanism".

Parental appointment mechanism

I opened my AndroidStudio, searched "ClassLoader", and then opened the ClassLoader class under the "java.lang" package. Then turn the code to the loadClass method:

Public Class loadClass (String name) throws ClassNotFoundException {

Return loadClass (name, false)

}

/ / -?

Protected Class loadClass (String name, boolean resolve)

Throws ClassNotFoundException

{

/ / First, check if the class has already been loaded

Class c = findLoadedClass (name)

If (c = = null) {

Try {

If (parent! = null) {

C = parent.loadClass (name, false)

} else {

C = findBootstrapClassOrNull (name)

}

} catch (ClassNotFoundException e) {

/ / ClassNotFoundException thrown if class not found

/ / from the non-null parent class loader

}

If (c = = null) {

/ / If still not found, then invoke findClass in order

/ / to find the class.

C = findClass (name)

}

}

Return c

}

In fact, this code has a good explanation of the parent delegation mechanism, in order to make it easier for you to understand, I have made a picture to describe how the above code is done:

It is easier to understand from the image above when a file like Hello.class is about to be loaded. Regardless of our custom classloader, we will first check in AppClassLoader to see if it has been loaded, and if so, there is no need to load it again. If not, the parent loader is taken and the loadClass method of the parent loader is called. The same person in the parent class first checks to see if it has been loaded, if not further up. Pay attention to this process, knowing that no loader chooses to load until it reaches Bootstrap classLoader. If the parent loader fails to load, it sinks to the child loader to load, down to the bottom, and if no loader can load, a ClassNotFoundException is thrown.

Why design this mechanism?

One advantage of this design is that if someone wants to replace the system-level class: String.java. Tamper with its implementation, but under this mechanism, the classes of these systems have already been loaded by Bootstrap classLoader, so they will not be loaded again, preventing the implantation of dangerous code to a certain extent.

At this point, I believe you have a deeper understanding of "the principle of Java parent appointment mechanism". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Mechanism parents delegation code principle method content system learning inspection design practical deeper dangerous above picture interest benefit practicality practicality simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology MariaDB Shulou Information macOS