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 application classes are temporarily loaded in the overriding
ClassLoader
for introspection purposes before eventually loading an
instrumented version of the class in the given parent ClassLoader
.Modifier and Type | Field and Description |
---|---|
static java.lang.String[] |
DEFAULT_EXCLUDED_PACKAGES
Packages that are excluded by default.
|
Constructor and Description |
---|
OverridingClassLoader(java.lang.ClassLoader parent)
Create a new OverridingClassLoader for the given ClassLoader.
|
OverridingClassLoader(java.lang.ClassLoader parent,
java.lang.ClassLoader overrideDelegate)
Create a new OverridingClassLoader for the given ClassLoader.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
isEligibleForOverriding(java.lang.String className)
Determine whether the specified class is eligible for overriding
by this class loader.
|
protected byte[] |
loadBytesForClass(java.lang.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. |
java.lang.Class<?> |
loadClass(java.lang.String name) |
protected java.lang.Class<?> |
loadClass(java.lang.String name,
boolean resolve) |
protected java.lang.Class<?> |
loadClassForOverriding(java.lang.String name)
Load the specified class for overriding purposes in this ClassLoader.
|
protected java.io.InputStream |
openStreamForClass(java.lang.String name)
Open an InputStream for the specified class.
|
protected byte[] |
transformIfNecessary(java.lang.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, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public static final java.lang.String[] DEFAULT_EXCLUDED_PACKAGES
public OverridingClassLoader(@Nullable java.lang.ClassLoader parent)
parent
- the ClassLoader to build an overriding ClassLoader forpublic OverridingClassLoader(@Nullable java.lang.ClassLoader parent, @Nullable java.lang.ClassLoader overrideDelegate)
parent
- the ClassLoader to build an overriding ClassLoader foroverrideDelegate
- the ClassLoader to delegate to for overridingpublic java.lang.Class<?> loadClass(java.lang.String name) throws java.lang.ClassNotFoundException
loadClass
in class java.lang.ClassLoader
java.lang.ClassNotFoundException
protected java.lang.Class<?> loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
loadClass
in class java.lang.ClassLoader
java.lang.ClassNotFoundException
protected boolean isEligibleForOverriding(java.lang.String className)
className
- the class name to checkDecoratingClassLoader.isExcluded(java.lang.String)
@Nullable protected java.lang.Class<?> loadClassForOverriding(java.lang.String name) throws java.lang.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 namejava.lang.ClassNotFoundException
- if the class for the given name couldn't be loaded@Nullable protected byte[] loadBytesForClass(java.lang.String name) throws java.lang.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 namejava.lang.ClassNotFoundException
- if the class for the given name couldn't be loaded@Nullable protected java.io.InputStream openStreamForClass(java.lang.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(java.lang.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)