Class Loading --- (class load mechanism, the story of developers have to know) - Part I

xiaoxiao2021-03-06  41

Maybe you think Class Load is a high-level topic, no matter what, you still have to know it as a developer.

This article is based on the latest JDK5, and then the contents of the v. I hope you can learn more about the language you use.

Understand ClassLoader

If you define a ib.test.Object.

You write this in the program:

Import Ort.Test.Object

Object o = new string ();

Maybe you are pleasing to write no problem, but in fact you are wrong.

This is incorrect, the report

ClassCastException,

A Class is identified in the JVM. It is determined by it from which Package and class name are determined. That is, it has a namespace.

Org.test.Object is not java.lang.object

In Java, each class is an instance of java.lang.class. That is to say.

Java.lang.class klass = myclass.class;

Myclass myclass = new myclass () is equivalent to myclass.newinstance ();

In JVM. All obtained

Java.lang.classloader. (or it adds yourself to your own yourself), we are trying to get Java_Home / JRE / RT.ja when starting. But we found that JDK did not have any documentation to introduce Bootstrap.jar. In fact, Bootstrap Class Loader is outside JDK, which is different from JVM. (This article is not introduced)

Java.net.urlclassloader java.security.secureclassloader java.rmi.server.rmiclassLoader Sun.Applet.AppletClassLoader

Class Loaders working principle

In addition to bootstrap, all ClassLoader has a parent class logader, they are all instanceof

Java.lang.classloader

Let's take a look at the working principle in JDK1.5.

If there is a method loadingClass

protected synchronized Class loadClass (String name, boolean resolve) throws ClassNotFoundException {// First check if the class is already loaded Class c = findLoadedClass (name) ; if (c == null) {try {if (parent =! NULL) {c = parent.loadClass (name, false);} else {c = findbootstrapClass0 (Name);}} catch (classnotfoundexcection e) {// if still not found, then invoke // Findclass to find the class. c = FindClass (Name);}}}} {resolveclass (c);} return c;}

Then there are two ways to set it in the form of a parent.

Public class myclassloader extends classloader {public myclassloader () {super (myclassloader.class.getClassLoader ());}} or

Public class myclassloader extends classloader {public myclassloader () {super (getclass (). getClassLoader ());}}

Here is the first choice.

Because getClass () is the method within the constructor, there must be a constructor code existence, but if not, then find the parent class ClassLoader, I have been looking for, until finding FindbootstrapClass, if it does not exist At that time, the FindClass () method was executed. . At that time, I will report a classNotFoundException.

Let's take a look at Findclass ()

Protected class Findclass (String Name)

Throws classnotfoundexception {

Throw New ClassNotFoundException (NAME);

}

In the FindClass () method, Class Loader wants to get the domain code (which is the .class file), but not necessarily .class file, these bytecodes can come from local or system, network (borrow You can understand this COBRA, RMI) or use

BCEL (Apache is based on the byte code to get a engine library) ... and more. One, bytecode found. At that time, I started executing the defineClass () method. At that time, a class will be defined. If there are two ClassLoader instantiate two identical code, defineClass () defines two classes. Please see (Java Language Specification)

The figure below shows how a mymainclass.class is loaded. (Load the same target.class by multiple ClassLoader), --- and we think that there is a difference, it is first due to BootStarpClassLoader loading.

According to the above, since the two classloader () loaded into target.class, the defineClass () is defined by two Class products.

So it is easy to conclude Target Target3 = (target) target2; it is not possible to perform.

Please see

INSIDE CLASS Loaders (Andreas Schaefer)

Author Blog:

http://blog.9cbs.net/totodo/

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

New Post(0)