Top Description Fields Constructors Methods
org.codehaus.groovy.tools

public Class RootLoader

extends URLClassLoader
Class Inheritance
Imports
java.io.File, java.net.MalformedURLException, .URL, .URLClassLoader, java.util.HashMap, .Map, .Optional

This ClassLoader should be used as root of class loaders. Any RootLoader does have its own classpath. When searching for a class or resource this classpath will be used. Parent Classloaders are ignored first. If a class or resource can't be found in the classpath of the RootLoader, then parent is checked.

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.

Field Summary

Modifier and TypeField and Description
private final Map<String, Class<?>>
private static final String

Constructor Summary

AccessConstructor and Description
public
RootLoader(final ClassLoader
the parent Loader
parent
)

Constructs a RootLoader without classpath.

public
RootLoader(final URL[] urls, final ClassLoader parent)

Constructs a RootLoader with a parent loader and an array of URLs as its classpath.

public
RootLoader(final LoaderConfiguration lc)

Constructs a RootLoader with a LoaderConfiguration object which holds the classpath.

Method Summary

Modifier and TypeMethod and Description
public void
addURL(final URL
the URL to be added to the search path of URLs
url
)

Overrides java.net.URLClassLoader.addURL.

Appends the specified URL to the list of URLs to search for classes and resources.
protected Class<?>
findClass(final String
the name of the class
name
)

Overrides java.net.URLClassLoader.findClass.

Finds and loads the class with the specified name from the URL search path.
public URL
getResource(final String
The resource name
name
)

Overrides java.lang.ClassLoader.getResource.

Finds the resource with the given name.
protected synchronized Class<?>
loadClass(final String
The binary name of the class
name
,
final boolean
If true then resolve the class
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.
Inherited from java.net.URLClassLoader:
closedefinePackagefindResourcefindResourcesgetPermissionsgetResourceAsStreamgetURLsnewInstancenewInstance

Field Detail

customClassesback to summary
private final Map<String, Class<?>> customClasses
ORG_W3C_DOM_NODEback to summary
private static final String ORG_W3C_DOM_NODE

Constructor Detail

RootLoaderback to summary
public RootLoader(final ClassLoader parent)

Constructs a RootLoader without classpath.

Parameters
parent:ClassLoader

the parent Loader

RootLoaderback to summary
public RootLoader(final URL[] urls, final ClassLoader parent)

Constructs a RootLoader with a parent loader and an array of URLs as its classpath.

RootLoaderback to summary
public RootLoader(final LoaderConfiguration lc)

Constructs a RootLoader with a LoaderConfiguration object which holds the classpath.

Method Detail

addURLback to summary
public void addURL(final URL url)

Overrides java.net.URLClassLoader.addURL.

Doc from java.net.URLClassLoader.addURL.

Appends the specified URL to the list of URLs to search for classes and resources.

If the URL specified is null or is already in the list of URLs, or if this loader is closed, then invoking this method has no effect.

Parameters
url:URL

the URL to be added to the search path of URLs

Annotations
@Override
findClassback to summary
protected Class<?> findClass(final String name) throws ClassNotFoundException

Overrides java.net.URLClassLoader.findClass.

Doc from java.net.URLClassLoader.findClass.

Finds and loads the class with the specified name from the URL search path. Any URLs referring to JAR files are loaded and opened as needed until the class is found.

Parameters
name:String

the name of the class

Returns:Class<?>

the resulting class

Annotations
@Override
Exceptions
ClassNotFoundException:
if the class could not be found, or if the loader is closed.
getResourceback to summary
public URL getResource(final String name)

Overrides java.lang.ClassLoader.getResource.

Doc from java.lang.ClassLoader.getResource.

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a '/'-separated path name that identifies the resource.

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).

Parameters
name:String

The resource name

Returns:URL

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.

Annotations
@Override
loadClassback to summary
protected synchronized Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException

Overrides java.lang.ClassLoader.loadClass.

Doc from java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name. The default implementation of this method searches for classes in the following order:

  1. Invoke findLoadedClass(String) to check if the class has already been loaded.

  2. Invoke the loadClass method on the parent class loader. If the parent is null the class loader built into the virtual machine is used, instead.

  3. Invoke the findClass(String) method to find the class.

If the class was found using the above steps, and the resolve flag is true, this method will then invoke the resolveClass(Class) method on the resulting Class object.

Subclasses of ClassLoader are encouraged to override findClass(String), rather than this method.

Unless overridden, this method synchronizes on the result of getClassLoadingLock method during the entire class loading process.

Parameters
name:String

The binary name of the class

resolve:boolean

If true then resolve the class

Returns:Class<?>

The resulting Class object

Annotations
@Override
Exceptions
ClassNotFoundException:
If the class could not be found