Package jenkins

Class 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 Detail

      • ClassLoaderReflectionToolkit

        public ClassLoaderReflectionToolkit()
    • Method Detail

      • _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:
        ClassLoader.findResource(String)
      • _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:
        ClassLoader.findResources(String)