preface
Appclassloader today (github.com/frohoff/jdk…) “, found a knowledge blind area that he had not noticed before, thought for a while did not want to understand, finally realized: I am a paratrooper.
The body of the
They say parent delegate, delegate to the parent class loader, and if the parent class loader doesn’t load, load it yourself, and look at this code, it’s very easy to understand. This class
protectedClass<? > loadClass(String name,boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loadedClass<? > c = findLoadedClass(name);if (c == null) {
long t0 = System.nanoTime();
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.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); }}if (resolve) {
resolveClass(c);
}
returnc; }}Copy the code
We also know that loadClass is where the parent delegate logic lies
Take a look at the Appclassloader code
/** * Override loadClass so we can checkPackageAccess. */
publicClass<? > loadClass(String name,boolean resolve)
throws ClassNotFoundException
{
int i = name.lastIndexOf('. ');
if(i ! = -1) {
SecurityManager sm = System.getSecurityManager();
if(sm ! =null) {
sm.checkPackageAccess(name.substring(0, i)); }}if (ucp.knownToNotExist(name)) {
// The class of the given name is not found in the parent
// class loader as well as its local URLClassPath.
// Check if this class has already been defined dynamically;
// if so, return the loaded class; otherwise, skip the parent
// delegation and findClass.Class<? > c = findLoadedClass(name);if(c ! =null) {
if (resolve) {
resolveClass(c);
}
return c;
}
throw new ClassNotFoundException(name);
}
return (super.loadClass(name, resolve));
}
Copy the code
The AppClassLoader checks to see if it has loaded the class. If it has loaded the class, it will parse and return it. If it has not loaded the class, it will delegate the parent class loader to load it. If the parent class is not loaded, the AppclassLoader will load the parent class. If the parent class is not loaded, the AppclassLoader will load the parent class. But obviously if we write a class, PSVM, it will be loaded by appClassLoader, right? So when does the AppClassLoader load the class?
explain
Appclassloader loads the Extclassloader itself, but I can’t find when appClassLoader loads the Extclassloader. Be in a state of great displeasure. Debug when breakpoint found in appClassLoader return (super.loadClass(name, resolve)); At first, I didn’t react, I thought what was going on, then I realized, I called me a paratrooper.
The root is super in super.loadClass, which I treat as an extClassloader! But it’s actually a superclass method, so it goes all the way up, urlClassLoader, SecureClassLoader, loadClass all the way up to ClassLoader, So super.loadClass calls loadClass of the ClassLoader class! After that, the parent delegate logic begins, and eventually the URLCLASSLOADER FindClass(Name) actually loads the class itself.
alert
Superclass loading is not a superclass! Even though I thought I was pretty well aware of this, when I see super, I still think of it as a parent class loader!
conclusion
I am a paratrooper
A very small mistake, but maybe someone and I think impassable, so put on the network for search.