The first part: Java.lang.ClassLoader
The ClassLoader (class loader) is used to load Java classes into a Java virtual machine. In general, Java virtual machines use Javaclass is as follows: the Java source program (. java file) is converted to Java-byte code (. Class) after it has been compiled by the Java compiler .files). The ClassLoader is responsible for reading the Java byte code and converting it into an instance of the Java.lang.Class class. Each of these instances is used to represent a Javaclass. An object of the class can be created by using the Newinstance () method of this instance. The actual situation may be more complex, such as JavaThe byte code may have been dynamically generated by the tool or it may have been downloaded over the network. basically all ClassLoader are an instance of the Java.lang.ClassLoader class
constructor function
Public abstract class classloader private static native void registernatives (); static { Registernatives (); } private classloader (Void unused, classloader parent) { this.parent = parent; if (parallelloaders.isregistered (This.getClass ())) { parallellockmap = new ConcurrentHashMap<> (); Package2certs = new concurrenthashmap<> (); domains =   Collections.synchronizedset (new hashset<protectiondomain> ()); assertionlock = new object (); } else { // no finer-grained lock; lock on the classloader instance parallelLockMap = null; package2certs = new Hashtable< > (); domains = new Hashset<> (); assertionlock = this; } }
2.loadClass method, the method is the main method of the ClassLoader, the code is as follows:
protected class<?> loadclass (string name, boolean resolve) throws ClassNotFoundException { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1. Asynchronous protection against repeated loading of the same class synchronized (Getclassloadinglock (name)) { //2. First, check if the class has been loaded class<?> c = findloadedclass (name); if (c == null) { //2.1 if the class has not been loaded //2.1.1 system.nanotime () This method mainly returns the current value of a system timer, measured in nanoseconds. But cannot be used to calculate the currentTime, can only be calculated through the End-start time interval long t0 = system.nanotime (); try { if (parent != null) { //2.1.2 If there is a parent loader, that is, the parent loader is not the initial loader, the recursive parent loader checks to see if it has been loaded c = Parent.loadclass (Name, false); } else { //2.1.3 if there is no parent loader, that is, the parent loader is the initial loader, find out if the class is loaded, see method c = findbootstrapclassornull (name); } } catch (Classnotfoundexception e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader } if (c == null) { //2.1.4 if the class is still not there, run the Findclass method to load, which is a virtual method long t1 = system.nanotime (); c = findclass (name); // this is the defining class loader; record the stats Sun.misc.PerfCounter.getParentdelegationtime (). Addtime (t1 - t0); sun.misc.perfcounter.getfindclasstime (). Addelapsedtimefrom (t1); sun.misc.perfcounter.getfindclasses (). Increment (); } } if (Resolve) { //parsing class, Resolve Default to false resolveclass (c); } return c; } }
3.getClassLoadingLock (name) method
Protected Object Getclassloadinglock (String className) {Object lock = this; if (parallellockmap! = null) {Object newlock = new Object (); Lock = Parallellockmap.putifabsent (ClassName, Newlock); if (lock = = null) {lock = Newlock; }} return lock; }
4.findLoadedClass (name) method
Protected final class<?> Findloadedclass (String name) {if (!checkname (name)) return null; return FindLoadedClass0 (name); } Private native Final class<?> findLoadedClass0 (String name);
5.findBootstrapClassOrNull (name) method
Private class<?> Findbootstrapclassornull (String name) {if (!checkname (name)) return null; return Findbootstrapclass (name); }//Return NULL if not found private native class<?> Findbootstrapclass (String name);
The 6.findClass (name) method, which is not specifically implemented in ClassLoader, overrides the method for different scenarios depending on the situation.
Protected class<?> Findclass (String name) throws ClassNotFoundException {throw new ClassNotFoundException (NA ME); }
7.resolveClass (class<?> C) method
Protected final void Resolveclass (class<?> c) {resolveClass0 (c); } private native void ResolveClass0 (class<?> c);
8.defineClass method, the main is the byte code class file instance as a class instance. This method is not overwritten, and when we inherit ClassLoader, we rewrite the Findclass method to convert the relevant file into a class instance that the JVM can recognize. You must call DefineClass in the overridden Findclass to complete the conversion logic.
protected final class<?> defineclass (String Name, byte[] b, int off, int len) throws ClassFormatError { Return defineclass (name, b, off, len, null); }
protected final class<?> defineclass (String name, byte[] b, int off, int len, protectiondomain protectiondomain) throws ClassFormatError { protectiondomain = predefineclass (Name, protectionDomain); string source = defineclasssourcelocation ( Protectiondomain); class<?> c = DefineClass1 (Name, b, off, len, protectiondomain, source); postdefineclass (c, protectiondomain); return c; }
Related tests for the 9.ClassLoader class.
Java.lang.ClassLoader and Java.net.URLClassLoader Learning