如何控制流到findClass

时间:2010-08-23 03:39:31

标签: java delegation classloader

在用于加载类的父委托模型中,我知道在父类上调用loadclass(),一直到类加载器层次结构的顶部(假设未加载类)。此时调用最顶层的父类加载器的findClass。  如果找不到这个类,控件如何转移到下一个类加载器的findClass方法?

2 个答案:

答案 0 :(得分:12)

findClass(String)将由类加载器的loadClass(String)方法调用。这是默认的 实现抛出ClassNotFoundException,并且打算被类加载器覆盖。

loadClass(String)方法将按顺序调用以下方法

  • 首先,它会尝试查找该类是否已加载:findLoadedClass(String)
  • 如果没有找到,则会调用父级“classloaders 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),则返回该类。

答案 1 :(得分:3)

以上说明全面而优秀。 理解委托类加载原理的一种更直接的方法是读取源代码。无论如何,源代码根本不复杂。

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/ClassLoader.java#400

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     }