Package hudson.util

Class ExistenceCheckingClassLoader


@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public final class ExistenceCheckingClassLoader extends DelegatingClassLoader
A ClassLoader that verifies the existence of a .class resource before attempting to load the class. Intended to sit in front of servlet container loaders we do not control.

This implementation overrides loadClass(String, boolean) and uses ClassLoader.getResource(String) to check whether the corresponding .class file is available in the classpath. If the resource is not found, a ClassNotFoundException is thrown immediately.

Parallel-capable parent loaders retain a per-class-name lock object for every load attempt, including misses. By checking getResource(name + ".class") first and throwing ClassNotFoundException on absence, we avoid calling loadClass on misses, thus preventing the parent from populating its lock map for nonexistent classes.

This class is only needed in PluginManager.UberClassLoader. It is unnecessary for plugin ClassLoaders (because URLClassLoader2 mitigates lock retention via ClassLoader.getClassLoadingLock(java.lang.String)) and redundant for delegators (because DelegatingClassLoader already avoids base locking).

Author:
Dmytro Ukhlov
See Also:
  • Constructor Details

    • ExistenceCheckingClassLoader

      public ExistenceCheckingClassLoader(String name, ClassLoader parent)
    • ExistenceCheckingClassLoader

      public ExistenceCheckingClassLoader(ClassLoader parent)
  • Method Details

    • loadClass

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Short-circuits misses by checking for the .class resource prior to delegation. Successful loads behave exactly as the parent would; misses do not touch the parent's per-name lock map.
      Overrides:
      loadClass in class DelegatingClassLoader
      Parameters:
      name - The binary name of the class
      resolve - If true then resolve the class
      Returns:
      The resulting Class object
      Throws:
      ClassNotFoundException - If the class could not be found