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 String[] |
DEFAULT_EXCLUDED_PACKAGES
Packages that are excluded by default.
|
Constructor and Description |
---|
OverridingClassLoader(ClassLoader parent)
Create a new OverridingClassLoader for the given ClassLoader.
|
OverridingClassLoader(ClassLoader parent,
ClassLoader overrideDelegate)
Create a new OverridingClassLoader for the given ClassLoader.
|
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. |
Class<?> |
loadClass(String name) |
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, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public static final String[] DEFAULT_EXCLUDED_PACKAGES
public OverridingClassLoader(@Nullable ClassLoader parent)
parent
- the ClassLoader to build an overriding ClassLoader forpublic OverridingClassLoader(@Nullable ClassLoader parent, @Nullable ClassLoader overrideDelegate)
parent
- the ClassLoader to build an overriding ClassLoader foroverrideDelegate
- the ClassLoader to delegate to for overridingpublic Class<?> loadClass(String name) throws ClassNotFoundException
loadClass
in class ClassLoader
ClassNotFoundException
protected 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)
@Nullable 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 loaded@Nullable protected 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 loaded@Nullable protected 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)