Package io.permazen.util
Class ApplicationClassLoader
java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
io.permazen.util.ApplicationClassLoader
- All Implemented Interfaces:
Closeable
,AutoCloseable
An
URLClassLoader
whose classpath can be modified at runtime.
This is intended for use by applications that want to load classes using the usual system or context class loader, but need
the ability to add URL
s to the classpath that is searched. In JDK 9 and later, the system class loader can no longer
be directly modified (this previously used an ugly reflection hack anyway). This class effectively restores that capability,
by overriding addURL()
and making it public.
This class also provides a way to lookup existing instances by parent; see getInstance()
. These instances
are cached using weak references to avoid a memory leak.
Instances work together to behave like a singleton; an URL
added to any instance by addURL()
is
automagically added to all existing and future instances.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Append the specified file to the search classpath.void
static ApplicationClassLoader
Obtain the unique instance having the current thread's context class loader as its parent, creating it on demand if needed.static ApplicationClassLoader
getInstance
(ClassLoader parent) Obtain the unique instance having the givenClassLoader
as its parent, creating it on demand if needed.Methods inherited from class java.net.URLClassLoader
close, definePackage, findClass, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstance
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Method Details
-
addFile
Append the specified file to the search classpath.- Parameters:
file
- classpath component- Throws:
IllegalArgumentException
- iffile
is null
-
addURL
- Overrides:
addURL
in classURLClassLoader
- Parameters:
url
- the URL to be added to the search path of URLs- Throws:
IllegalArgumentException
- ifurl
cannot be converted into anURI
IllegalArgumentException
- ifurl
is null
-
getInstance
Obtain the unique instance having the current thread's context class loader as its parent, creating it on demand if needed. -
getInstance
Obtain the unique instance having the givenClassLoader
as its parent, creating it on demand if needed.- Parameters:
parent
- parent class loader, or null for the boot class loader- Returns:
- corresponding
ApplicationClassLoader
, orparent
ifparent
is itself already anApplicationClassLoader
-