Package jenkins

Class ClassLoaderReflectionToolkit

java.lang.Object
jenkins.ClassLoaderReflectionToolkit

@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @RestrictedSince("2.324") public class ClassLoaderReflectionToolkit extends Object
Reflective access to various ClassLoader methods which are otherwise protected.

Initially tries to access methods using known classloaders in use that expose the methods to prevent illegal reflective access errors on Java 11+ Then falls back to accessing the ClassLoader methods.

All reflection method initialisation is delayed until first use so that we don't access the methods if we don't need to.

Note: Currently there is no known production use-case for the fallback case of accessing these methods via reflection: the JenkinsRule tests use a different classloader, but once that is made consistent with production Jenkins we can re-evaluate the fallback code.

  • Constructor Details

    • ClassLoaderReflectionToolkit

      public ClassLoaderReflectionToolkit()
  • Method Details

    • loadClass

      @NonNull public static Class<?> loadClass(ClassLoader cl, String name) throws ClassNotFoundException
      Load the class with the specified binary name. This method searches for classes in the following order:
      1. Invoke ClassLoader.findLoadedClass(String) to check if the class has already been loaded.

      2. Invoke ClassLoader.findClass(String) to find the class.

      This method synchronizes on the result of ClassLoader.getClassLoadingLock(String) during the entire class loading process.

      Parameters:
      cl - The ClassLoader to use.
      name - The binary name of the class.
      Returns:
      The resulting Class object.
      Throws:
      ClassNotFoundException - If the class could not be found.
      Since:
      2.321
      See Also:
    • _findResource

      @CheckForNull public static URL _findResource(ClassLoader cl, String name)
      Find the resource with the given name.
      Parameters:
      name - The resource name.
      Returns:
      URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.
      Since:
      1.553
      See Also:
    • _findResources

      @NonNull public static Enumeration<URL> _findResources(ClassLoader cl, String name) throws IOException
      Return an enumeration of URL objects representing all the resources with the given name.
      Parameters:
      name - The resource name.
      Returns:
      An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, which are in a package that is not opened unconditionally, or for which access to the resource is denied by the security manager, are not returned in the enumeration.
      Throws:
      IOException - If I/O errors occur.
      Since:
      1.553
      See Also: