[Reserved] My Reading Notes About ClassLoader

zhaozj2021-02-12  161

(Sarsor Note: Recently, I have been very interested in the source code of the research open source project, some of which are used in ClassLoader, Method, etc.)

Source: Richardluo Learn ClassLoader1, what is the ClassLoader? Java program is not an executable file, it is required to load it into the JVM. ClassLoader works is to load the class in the JVM. Moreover, Java ClassLoader is written in Java language. This means that you can create your own ClassLoader ClassLoader's basic goal is to serve the request for classes. When the JVM needs to use classes, it requests this class based on the name, and then ClassLoader tries to return a class object that represents this class. Customized ClassLoader can be created by covering a different phase of this process.

2. Some important methods a) Method LoadClass ClassLoader.LoadClass () is the entry point of ClassLoader. The method is defined as follows: Class LoadClass (String Name, Boolean Resolve); Name JVM The name of the class required, such as foo or java.lang.Object. The resolve parameter tells the method whether you need to parse classes. Class analysis should be considered before preparing to execute the class. It doesn't always need to be parsed. If JVM only needs to know if this class exists or finds the superclass of this class, then no resolution. B) Method DefineClass DefineClass method is the main 诀窍 of ClassLoader. This method accepts arrays consisting of original bytes and converts it to Class objects. The original array contains data that is installed from a file system or network. DefineClass manages many of the complex, mysterious and reliance on the implementation of the JVM - it analyzes the bytecode to the runtime data structure, verification effectiveness, etc. Don't worry, you don't have to write it yourself. In fact, even if you want to do this, you can't overwrite it because it has been marked into final.

C) Methods FindsystemClass FindsystemClass method is loaded from the local file system. It looks for class files in the local file system. If there is, use defineClass to convert the original byte into a Class object to convert the file into classes. This is the default mechanism for JVM normally loaded when running a Java application. (ClassLoader changes in Java 2 provides details about the process of this process of Java 1.2.) For custom ClassLoader, only FindsystemClass is used after trying other methods to load classes. The reason is very simple: ClassLoader is responsible for performing special steps to carry out the laying class, not responsible for all classes. For example, even if ClassLoad is loaded from a remote Web site, you still need to load a large number of basic Java libraries on your local machine. These classes are not what we have cared, so JVM is to load them in the default manner: from the local file system. This is the use of FindsystemClass. D) The method ResolVeclass is as mentioned earlier, and can be loaded into classes completely (with parsing) without completely (without parsing). When writing our own loadClass, resolveclass can be called, depending on the value of the LoadClass's Resolve parameter. E) Method FindloadedClass FindloadedClass acts as a cache: When the loadClass is loaded, it calls this method to see if the ClassLoad has been loaded, which avoids the trouble of reloading existing classes. This method should be called first.

3, how to assemble these methods 1) Call FindLoadedClass to see if there is an installed class. 2) If not, then use the special magical way to get the original byte. 3) If there is already original byte, call DefineClass to convert them into a Class object. 4) If there is no original byte, then call FindSystemClass to see if you get the class from the local file system. 5) If the resolve parameter is true, then resolveclass parses the Class object. 6) If there is still no class, return to ClassNotFoundException.

4, Changed between ClassLoader in Java 2 1) LoadClass's default implementation Customized LoadClass method generally attempts to load the requested class, if you write a number of classes, you will find the same one, complicated Write variables on methods. The implementation of LoadClass in Java 1.2 embeds the general method of most lookup classes, and allows you to customize it by overwriting the FindClass method, and Findclass will call LoadClass when appropriate. The advantage of this way is that you may not have to override loadClass; as long as you override FindClass, it reduces the workload.

2) New method: The default implementation of FindClass LoadClass calls this new method. The use of FindClass contains all special code of your ClassLoader without having to copy other code (for example, when a special method fails, calling the system classloader).

转载请注明原文地址:https://www.9cbs.com/read-7264.html

New Post(0)