在加载类的父委托模型中,我知道 loadclass() 是在父类上调用的,一直到类加载器层次结构的顶部(假设未加载类)。此时最顶层的父类加载器的 findClass 被调用。如果没有找到这个类,如何将控制权转移到下一个类加载器的 findClass 方法?
问问题
5863 次
2 回答
14
findClass(String)将由类加载器的loadClass (String)方法调用。它的默认实现抛出一个 ClassNotFoundException 并且旨在被类加载器覆盖。
loadClass(String) 方法将按顺序调用以下方法
- 首先,它尝试查找该类是否已加载:
findLoadedClass(String)
- 然后如果没有找到它调用父类加载器
loadClass(String)
方法。 - 如果没有找到,就会调用
findClass(String)
方法(自定义加载)
因此,自定义类加载器所要做的就是覆盖findClass(String)
以自定义方式加载类的方法。这将确保类加载中的正确委派。检查链接(javadoc),它解释了采取了哪些步骤以及如何findClass(String)
调用loadClass(String)
因此类加载按以下顺序进行(示例)ClassLoader A 与父 B (仅解释 findClass 和 loadClass)
A.loadClass()
|
(not-found?) (by findLoadedClass)
|
B.loadClass()
|
(not found?) (by findLoadedClass)
|
systemclassloader.loadClass() (Bs parent, also can be
| called classpath classloader)
|
(not found?) (by findLoadedClass)
|
bootstrap classloader.loadClass() (the bootstrap classloader,
| this has no parent)
|
(not found?)
|
systemclassloader.findClass() (on system classloader,
| will try to "find" class in "classpath")
|
(not found?)
|
B.findClass()
|
(not found?)
|
A.findClass()
|
(not found?)
|
ClassNotFoundException
在任何给定时间,如果找到类(通过 findClass 或 findLoadedClass),则返回该类。
于 2010-08-23T04:10:12.883 回答
3
上面的解释是全面而优秀的。了解委托类加载原理的更直接的方法是阅读源代码。无论如何,源代码一点也不复杂。
400 protected Class<?> loadClass(String name, boolean resolve)
401 throws ClassNotFoundException
402 {
403 synchronized (getClassLoadingLock(name)) {
404 // First, check if the class has already been loaded
405 Class c = findLoadedClass(name);
406 if (c == null) {
407 long t0 = System.nanoTime();
408 try {
409 if (parent != null) {
410 c = parent.loadClass(name, false);
411 } else {
412 c = findBootstrapClassOrNull(name);
413 }
414 } catch (ClassNotFoundException e) {
415 // ClassNotFoundException thrown if class not found
416 // from the non-null parent class loader
417 }
418
419 if (c == null) {
420 // If still not found, then invoke findClass in order
421 // to find the class.
422 long t1 = System.nanoTime();
423 c = findClass(name);
424
425 // this is the defining class loader; record the stats
426 sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
427 sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
428 sun.misc.PerfCounter.getFindClasses().increment();
429 }
430 }
431 if (resolve) {
432 resolveClass(c);
433 }
434 return c;
435 }
436 }
于 2012-11-08T06:09:33.887 回答