Class ClassLoaderReflectionToolkit
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic URL
_findResource
(ClassLoader cl, String name) Find the resource with the given name.static Enumeration<URL>
_findResources
(ClassLoader cl, String name) Return an enumeration ofURL
objects representing all the resources with the given name.static Class<?>
loadClass
(ClassLoader cl, String name) Load the class with the specified binary name.
-
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:-
Invoke
ClassLoader.findLoadedClass(String)
to check if the class has already been loaded. -
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
- TheClassLoader
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
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, aURL
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 ofURL
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 aURL
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:
-