public class OverridingClassLoader extends DecoratingClassLoader
ClassLoader
that does not always delegate to the
parent loader, as normal class loaders do. This enables, for example,
instrumentation to be forced in the overriding ClassLoader, or a
"throwaway" class loading behavior, where selected classes are
temporarily loaded in the overriding ClassLoader, in order to load
an instrumented version of the class in the parent ClassLoader later on.Modifier and Type | Field and Description |
---|---|
static String[] |
DEFAULT_EXCLUDED_PACKAGES
Packages that are excluded by default
|
Constructor and Description |
---|
OverridingClassLoader(ClassLoader parent)
Create a new OverridingClassLoader for the given class loader.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
isEligibleForOverriding(String className)
Determine whether the specified class is eligible for overriding
by this class loader.
|
protected byte[] |
loadBytesForClass(String name)
Load the defining bytes for the given class,
to be turned into a Class object through a
ClassLoader.defineClass(byte[], int, int) call. |
protected Class<?> |
loadClass(String name,
boolean resolve) |
protected Class<?> |
loadClassForOverriding(String name)
Load the specified class for overriding purposes in this ClassLoader.
|
protected InputStream |
openStreamForClass(String name)
Open an InputStream for the specified class.
|
protected byte[] |
transformIfNecessary(String name,
byte[] bytes)
Transformation hook to be implemented by subclasses.
|
excludeClass, excludePackage, isExcluded
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public static final String[] DEFAULT_EXCLUDED_PACKAGES
public OverridingClassLoader(ClassLoader parent)
parent
- the ClassLoader to build an overriding ClassLoader forprotected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
loadClass
in class ClassLoader
ClassNotFoundException
protected boolean isEligibleForOverriding(String className)
className
- the class name to checkDecoratingClassLoader.isExcluded(java.lang.String)
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException
The default implementation delegates to ClassLoader.findLoadedClass(java.lang.String)
,
loadBytesForClass(java.lang.String)
and ClassLoader.defineClass(byte[], int, int)
.
name
- the name of the classnull
if no class defined for that nameClassNotFoundException
- if the class for the given name couldn't be loadedprotected byte[] loadBytesForClass(String name) throws ClassNotFoundException
ClassLoader.defineClass(byte[], int, int)
call.
The default implementation delegates to openStreamForClass(java.lang.String)
and transformIfNecessary(java.lang.String, byte[])
.
name
- the name of the classnull
if no class defined for that nameClassNotFoundException
- if the class for the given name couldn't be loadedprotected InputStream openStreamForClass(String name)
The default implementation loads a standard class file through
the parent ClassLoader's getResourceAsStream
method.
name
- the name of the classprotected byte[] transformIfNecessary(String name, byte[] bytes)
The default implementation simply returns the given bytes as-is.
name
- the fully-qualified name of the class being transformedbytes
- the raw bytes of the classnull
;
same as the input bytes if the transformation produced no changes)