edu.rice.cs.plt.reflect
Class ShadowingClassLoader

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

public class ShadowingClassLoader
extends ClassLoader

A class loader that hides a set of classes and related resources. This allows classes with the same name (but perhaps a different implementation) to be cleanly loaded by a child loader.


Constructor Summary
ShadowingClassLoader(ClassLoader parent, boolean blackList, Iterable<? extends String> prefixes, boolean filterBootClasses)
           
 
Method Summary
static ShadowingClassLoader blackList(ClassLoader parent, String... excludePrefixes)
          Create a ShadowingClassLoader that will hide non-bootstrap classes matching the given prefixes.
 URL getResource(String name)
           
protected  Class<?> loadClass(String name, boolean resolve)
          If the given class is shadowed, a ClassNotFoundException will occur; otherwise, the method delegates to the parent class loader.
static ShadowingClassLoader whiteList(ClassLoader parent, String... includePrefixes)
          Create a ShadowingClassLoader that will hide all non-bootstrap classes except those matching the given prefixes.
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, 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

ShadowingClassLoader

public ShadowingClassLoader(ClassLoader parent,
                            boolean blackList,
                            Iterable<? extends String> prefixes,
                            boolean filterBootClasses)
Parameters:
parent - The parent loader
blackList - If true, classes matching prefixes are prevented from loading; otherwise, all classes except those matching prefixes are prevented from loading.
prefixes - A set of class name prefixes to which class names will be compared. Each prefix must be a package or class name (partial names, like "java.lang.Stri", will not match the full class name).
filterBootClasses - Whether classes and resources available to the bootstrap class loader should be hidden. If true, care must be taken to ensure that essential classes (like java.lang.Object) are available when needed.
Method Detail

blackList

public static ShadowingClassLoader blackList(ClassLoader parent,
                                             String... excludePrefixes)
Create a ShadowingClassLoader that will hide non-bootstrap classes matching the given prefixes.

Parameters:
parent - The parent loader
excludePrefixes - A set of class name prefixes to match. Each prefix must be a package or class name (partial names, like "java.lang.Stri", will not match the full class name).

whiteList

public static ShadowingClassLoader whiteList(ClassLoader parent,
                                             String... includePrefixes)
Create a ShadowingClassLoader that will hide all non-bootstrap classes except those matching the given prefixes.

Parameters:
parent - The parent loader
includePrefixes - A set of class name prefixes to match. Each prefix must be a package or class name (partial names, like "java.lang.Stri", will not match the full class name).

loadClass

protected Class<?> loadClass(String name,
                             boolean resolve)
                      throws ClassNotFoundException
If the given class is shadowed, a ClassNotFoundException will occur; otherwise, the method delegates to the parent class loader.

Overrides:
loadClass in class ClassLoader
Throws:
ClassNotFoundException

getResource

public URL getResource(String name)
Overrides:
getResource in class ClassLoader