[Original] What is ClassLoader? What is it?

zhaozj2021-02-12  128

(Since it is written while watching, some content is based on the existing knowledge, not necessarily correct, please correct, thank you)

What is it?

It is said that ClassLoader has to say class first. A class instance represents a class, interface, original data type, even Void, represents its Class instance in Java. We can see this, and Reference is a record regarding some of its corresponding objects, and a class instance records information about the class or interface that it represents. Each Class instance contains a reference to a ClassLoader subclass object. Writing here, now what is a probably understanding of ClassLoader.

Class Loader is used to load classes, and the type of load is what JVM is doing. We don't have to manage it, but why do Java provide ClassLoader this class? Due to the source code of the open source project due to the graduation design, recently, discovered that the code, ClassLoader used very much, so I decided to understand this problem, and I have written this article.

What is it?

First answer the question just now, why do ClassLoader is available in Java. I think I am very right in an article in my blog collection. To class, load certain resources as classes, and is currently implemented. In addition, J2EE is also good, many open source projects are also, more and more configuration files or descriptors, deployment. In fact, these configuration information described through the XML document will eventually become a Java class, which are the credit of ClassLoader. Here is the use of the subclass of the ClassLoader we define. As for JVM, it doesn't have to say it.

--how to use

ClassLoader uses a commission pattern to find classes and resources. Every ClassLoader instance has a child's ClassLoader and it is related to it. When there is a request to find a lookup class, it will first entrust the ClassLoader to find it. If you can't find it, you will find yourself (how to find it later. ), Sometimes when the father is NULL, the JVM built-in class (called: bootstrap class loader) will act as a parent class. Below is an implementation of the loadClass () method in ClassLoader.

1: protected synchronized class loadclass (String name, Boolean 2: Resolve) throws classnotfoundexception {3: // First, check this class has been loaded. 4: Class C = FindloadedClass (Name); 5: IF (c == null) {6: try {7: if (parent! = Null) {8: c = parent.loadclass (name, false); 9:} Else {10: c = findbootstrapClass (name); 11:} 12:} catch (classnotfoundexception e) {13: // If there is still not found, then call FindClass to find this class. 14: c = findclass (name); / / SARSOR's NOTE: FINDCLASS () May Throw

// ClassNotFoundException why not try-catch block 11:} 16:} 17: if (resolve) {18: resolveclass (c); 19:} 20: return c;} This is followed by the above-mentioned delegation mode Find, all methods can not find the class after Class, and then use FindClass (Name), so we will define the subclass of ClassLoader to override FindClass (), as for how to override an article in my blog collection. Specific instances, findings, and instances of finding resources, you will not repeat it here. Just pay special attention to the defineClass () method, which converts the byte arrays read from the class file to the instance of Class.

In fact, write here, how to use it or not very much, I will prepare the source code in the open source project, and share it with you. In addition, there are some usages about the Method class.

(Reading notes, writing while looking at, endless

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

New Post(0)