edu.rice.cs.plt.reflect
Class PreemptingClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by edu.rice.cs.plt.reflect.AbstractClassLoader
          extended by edu.rice.cs.plt.reflect.PreemptingClassLoader

public class PreemptingClassLoader
extends AbstractClassLoader

A class loader that claims a set of classes available in its parent as its own. This allows classes that would otherwise be loaded by a grandparent or distant ancestor to be loaded in a context that gives them access to all classes available to the parent. For example, if a library must be loaded with a PathClassLoader, while an adapter for that library is available on the static class path (without any custom class loading), loading the adapter directly will lead to errors — the library will not be visible to it. When loaded instead by this class loader, the adapter can see both the application classes and the library classes.

The implementation is somewhat limited: the only way to access a class definition in an arbitrary class loader without actually defining that class is to invoke ClassLoader.getResource(java.lang.String) with the name of a .class file. To work correctly, then, all preempted classes must be available as class files via parent.getResource(classFileName). Most class loaders (especially those that simply load class files from some repository) follow the convention that a class will be loaded only if a resource with the appropriate class file name exists. But there is no guarantee that all class loaders (such as those that generate class definitions on the fly) will follow this convention.


Constructor Summary
PreemptingClassLoader(ClassLoader parent, Iterable<? extends String> prefixes)
           
PreemptingClassLoader(ClassLoader parent, String... prefixes)
           
 
Method Summary
protected  Class<?> loadClass(String name, boolean resolve)
          Load a class by following the standard search strategy specified by ClassLoader.loadClass(String, boolean), with one exception: if the name matches the set of prefixes to preemptively load, the corresponding class file will be loaded as a resource and defined here rather than allowing the parent to define the class.
 
Methods inherited from class edu.rice.cs.plt.reflect.AbstractClassLoader
definePackageForClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PreemptingClassLoader

public PreemptingClassLoader(ClassLoader parent,
                             String... prefixes)
Parameters:
parent - The source of all classes and resources to be loaded
prefixes - A set of class name prefixes for which all matching classes will be defined by this loader rather than a parent. Each prefix must be a package or class name (partial names, like "java.lang.Stri", will not match the full class name).

PreemptingClassLoader

public PreemptingClassLoader(ClassLoader parent,
                             Iterable<? extends String> prefixes)
Parameters:
parent - The source of all classes and resources to be loaded
prefixes - A set of class name prefixes for which all matching classes will be defined by this loader rather than a parent. Each prefix must be a package or class name (partial names, like "java.lang.Stri", will not match the full class name).
Method Detail

loadClass

protected Class<?> loadClass(String name,
                             boolean resolve)
                      throws ClassNotFoundException
Load a class by following the standard search strategy specified by ClassLoader.loadClass(String, boolean), with one exception: if the name matches the set of prefixes to preemptively load, the corresponding class file will be loaded as a resource and defined here rather than allowing the parent to define the class.

Overrides:
loadClass in class ClassLoader
Throws:
ClassNotFoundException