How a control thread finds Class of

In the parent delegation model for loading classes, I know that loadclass () is called on the parent element, right up to the top of the hierarchy of the loader class (assuming the class is not loaded). At this point, the top-most parent classloader findClass is called. If this class is not found, how is the control passed to the next findClass method of the loader class?

+3
source share
2 answers

findClass (String) will be called by the loadClass (String) method of the class loader. By default, the implementation throws a ClassNotFoundException and is intended to override class loaders.

The loadClass (String) method will call the following methods in this order

  • First he tries to find if the class is already loaded: findLoadedClass(String)
  • Then, if it is not found, it calls the method of the parent class_classes loadClass(String).
  • If not found, it will call the method findClass(String)(user load)

Thus, all a custom classloader should do is to override the method findClass(String)for loading classes in its own way. This will ensure proper delegation when loading classes. Check the links (javadoc), explain what steps are being taken and how it is called findClass(String)fromloadClass(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

, (eigther by findClass findLoadedClass), .

+10

. - . .

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     }
+3

Source: https://habr.com/ru/post/1761043/


All Articles