Groovy Documentation

org.codehaus.groovy.tools
[Java] Class RootLoader

java.lang.Object
  java.lang.ClassLoader
      java.security.SecureClassLoader
          java.net.URLClassLoader
              org.codehaus.groovy.tools.RootLoader

public class RootLoader
extends java.net.URLClassLoader

This ClassLoader should be used as root of class loaders. Any RootLoader does have it's 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

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 it's 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.
Authors:
Jochen Theodorou


Constructor Summary
RootLoader(java.net.URL[] urls, java.lang.ClassLoader parent)

constructs a new RootLoader with a parent loader and an array of URLs as classpath

RootLoader(LoaderConfiguration lc)

constructs a new RootLoader with a LoaderConfiguration object which holds the classpath

 
Method Summary
void addURL(java.net.URL url)

adds an url to the classpath of this classloader

protected java.lang.Class findClass(java.lang.String name)

java.net.URL getResource(java.lang.String name)

returns the URL of a resource, or null if it is not found

protected java.lang.Class loadClass(java.lang.String name, boolean resolve)

loads a class using the name of the class

 
Methods inherited from class java.net.URLClassLoader
java.net.URLClassLoader#getResourceAsStream(java.lang.String), java.net.URLClassLoader#newInstance([Ljava.net.URL;, java.lang.ClassLoader), java.net.URLClassLoader#newInstance([Ljava.net.URL;), java.net.URLClassLoader#findResource(java.lang.String), java.net.URLClassLoader#findResources(java.lang.String), java.net.URLClassLoader#close(), java.net.URLClassLoader#getURLs(), java.net.URLClassLoader#loadClass(java.lang.String), java.net.URLClassLoader#getSystemClassLoader(), java.net.URLClassLoader#getResource(java.lang.String), java.net.URLClassLoader#getSystemResource(java.lang.String), java.net.URLClassLoader#getSystemResourceAsStream(java.lang.String), java.net.URLClassLoader#clearAssertionStatus(), java.net.URLClassLoader#getParent(), java.net.URLClassLoader#getResources(java.lang.String), java.net.URLClassLoader#getSystemResources(java.lang.String), java.net.URLClassLoader#setClassAssertionStatus(java.lang.String, boolean), java.net.URLClassLoader#setDefaultAssertionStatus(boolean), java.net.URLClassLoader#setPackageAssertionStatus(java.lang.String, boolean), java.net.URLClassLoader#wait(long, int), java.net.URLClassLoader#wait(long), java.net.URLClassLoader#wait(), java.net.URLClassLoader#equals(java.lang.Object), java.net.URLClassLoader#toString(), java.net.URLClassLoader#hashCode(), java.net.URLClassLoader#getClass(), java.net.URLClassLoader#notify(), java.net.URLClassLoader#notifyAll()
 
Methods inherited from class java.security.SecureClassLoader
java.security.SecureClassLoader#loadClass(java.lang.String), java.security.SecureClassLoader#getSystemClassLoader(), java.security.SecureClassLoader#getResource(java.lang.String), java.security.SecureClassLoader#getResourceAsStream(java.lang.String), java.security.SecureClassLoader#getSystemResource(java.lang.String), java.security.SecureClassLoader#getSystemResourceAsStream(java.lang.String), java.security.SecureClassLoader#clearAssertionStatus(), java.security.SecureClassLoader#getParent(), java.security.SecureClassLoader#getResources(java.lang.String), java.security.SecureClassLoader#getSystemResources(java.lang.String), java.security.SecureClassLoader#setClassAssertionStatus(java.lang.String, boolean), java.security.SecureClassLoader#setDefaultAssertionStatus(boolean), java.security.SecureClassLoader#setPackageAssertionStatus(java.lang.String, boolean), java.security.SecureClassLoader#wait(long, int), java.security.SecureClassLoader#wait(long), java.security.SecureClassLoader#wait(), java.security.SecureClassLoader#equals(java.lang.Object), java.security.SecureClassLoader#toString(), java.security.SecureClassLoader#hashCode(), java.security.SecureClassLoader#getClass(), java.security.SecureClassLoader#notify(), java.security.SecureClassLoader#notifyAll()
 

Constructor Detail

RootLoader

public RootLoader(java.net.URL[] urls, java.lang.ClassLoader parent)
constructs a new RootLoader with a parent loader and an array of URLs as classpath


RootLoader

public RootLoader(LoaderConfiguration lc)
constructs a new RootLoader with a LoaderConfiguration object which holds the classpath


 
Method Detail

addURL

public void addURL(java.net.URL url)
adds an url to the classpath of this classloader


findClass

protected java.lang.Class findClass(java.lang.String name)


getResource

public java.net.URL getResource(java.lang.String name)
returns the URL of a resource, or null if it is not found


loadClass

protected java.lang.Class loadClass(java.lang.String name, boolean resolve)
loads a class using the name of the class


 

Groovy Documentation