Class RootLoader
- All Implemented Interfaces:
Closeable,AutoCloseable
Note: this is very against the normal behavior of classloaders. Normal is to first check parent and then look in the resources you gave this classloader.
It's possible to add urls to the classpath at runtime through addURL(URL).
Why using RootLoader? If you have to load classes with multiple classloaders and a classloader does know a class which depends on a class only a child of this loader does know, then you won't be able to load the class. To load the class the child is not allowed to redirect its search for the class to the parent first. That way the child can load the class. If the child does not have all classes to do this, this fails of course.
For example:
parentLoader (has classpath: a.jar;c.jar)
|
|
childLoader (has classpath: a.jar;b.jar;c.jar)
class C (from c.jar) extends B (from b.jar)
childLoader.find("C")
--> parentLoader does know C.class, try to load it --> to load C.class it has to load B.class --> parentLoader is unable to find B.class in a.jar or c.jar --> NoClassDefFoundException!if childLoader had tried to load the class by itself, there would be no problem. Changing childLoader to be a RootLoader instance will solve that problem.
-
Constructor Summary
ConstructorsConstructorDescriptionRootLoader(ClassLoader parent) Constructs aRootLoaderwithout classpath.RootLoader(URL[] urls, ClassLoader parent) Constructs aRootLoaderwith a parent loader and an array of URLs as its classpath.Constructs aRootLoaderwith aLoaderConfigurationobject which holds the classpath. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected Class<?>getResource(String name) getResources(String name) protected Class<?>Methods inherited from class java.net.URLClassLoader
close, definePackage, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstanceMethods inherited from class java.security.SecureClassLoader
defineClass, defineClassMethods 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, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Constructor Details
-
RootLoader
Constructs aRootLoaderwithout classpath.- Parameters:
parent- the parent Loader
-
RootLoader
Constructs aRootLoaderwith a parent loader and an array of URLs as its classpath. -
RootLoader
Constructs aRootLoaderwith aLoaderConfigurationobject which holds the classpath.
-
-
Method Details
-
loadClass
- Overrides:
loadClassin classClassLoader- Throws:
ClassNotFoundException
-
getResource
- Overrides:
getResourcein classClassLoader
-
getResources
Collapses entries that resolve to the same canonical jar file so callers such as
ServiceLoaderdon't see the same providers twice when the launcher's startup classpath and theloadentries ingroovy-starter.confreference the same jar (GROOVY-11978).- Overrides:
getResourcesin classClassLoader- Throws:
IOException
-
addURL
- Overrides:
addURLin classURLClassLoader
-
findClass
- Overrides:
findClassin classURLClassLoader- Throws:
ClassNotFoundException
-