Top Description Fields Constructors Methods
org.springframework.core

public Class OverridingClassLoader

extends DecoratingClassLoader
Class Inheritance
Imports
java.io.IOException, .InputStream, org.springframework.lang.Nullable, org.springframework.util.FileCopyUtils

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.
Authors
Rod Johnson, Juergen Hoeller
Since
2.0.1

Field Summary

Modifier and TypeField and Description
private static final String
public static final String[]
DEFAULT_EXCLUDED_PACKAGES

Packages that are excluded by default.

private final ClassLoader

Constructor Summary

AccessConstructor and Description
public
OverridingClassLoader(ClassLoader
the ClassLoader to build an overriding ClassLoader for
parent
)

Create a new OverridingClassLoader for the given ClassLoader.

public
OverridingClassLoader(ClassLoader
the ClassLoader to build an overriding ClassLoader for
parent
,
ClassLoader
the ClassLoader to delegate to for overriding
overrideDelegate
)

Create a new OverridingClassLoader for the given ClassLoader.

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

whether the specified class is eligible
isEligibleForOverriding
(String
the class name to check
className
)

Determine whether the specified class is eligible for overriding by this class loader.

protected byte[]

Returns:

the byte content (with transformers already applied), or null if no class defined for that name
loadBytesForClass
(String
the name of the class
name
)

Load the defining bytes for the given class, to be turned into a Class object through a defineClass call.

public Class<?>
loadClass(String
The binary name of the class
name
)

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.
protected Class<?>
loadClass(String
The binary name of the class
name
,
boolean
If true then resolve the class
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.
protected Class<?>

Returns:

the Class object, or null if no class defined for that name
loadClassForOverriding
(String
the name of the class
name
)

Load the specified class for overriding purposes in this ClassLoader.

protected InputStream

Returns:

the InputStream containing the byte code for the specified class
openStreamForClass
(String
the name of the class
name
)

Open an InputStream for the specified class.

protected byte[]

Returns:

the transformed bytes (never null; same as the input bytes if the transformation produced no changes)
transformIfNecessary
(String
the fully-qualified name of the class being transformed
name
,
byte[]
the raw bytes of the class
bytes
)

Transformation hook to be implemented by subclasses.

Inherited from org.springframework.core.DecoratingClassLoader:
excludeClassexcludePackageisExcluded

Field Detail

CLASS_FILE_SUFFIXback to summary
private static final String CLASS_FILE_SUFFIX
DEFAULT_EXCLUDED_PACKAGESback to summary
public static final String[] DEFAULT_EXCLUDED_PACKAGES

Packages that are excluded by default.

overrideDelegateback to summary
private final ClassLoader overrideDelegate
Annotations
@Nullable

Constructor Detail

OverridingClassLoaderback to summary
public OverridingClassLoader(ClassLoader parent)

Create a new OverridingClassLoader for the given ClassLoader.

Parameters
parent:ClassLoader

the ClassLoader to build an overriding ClassLoader for

OverridingClassLoaderback to summary
public OverridingClassLoader(ClassLoader parent, ClassLoader overrideDelegate)

Create a new OverridingClassLoader for the given ClassLoader.

Parameters
parent:ClassLoader

the ClassLoader to build an overriding ClassLoader for

overrideDelegate:ClassLoader

the ClassLoader to delegate to for overriding

Since
4.3

Method Detail

isEligibleForOverridingback to summary
protected boolean isEligibleForOverriding(String className)

Determine whether the specified class is eligible for overriding by this class loader.

Parameters
className:String

the class name to check

Returns:boolean

whether the specified class is eligible

See Also
isExcluded
loadBytesForClassback to summary
protected byte[] loadBytesForClass(String name) throws ClassNotFoundException

Load the defining bytes for the given class, to be turned into a Class object through a defineClass call.

The default implementation delegates to openStreamForClass and transformIfNecessary.

Parameters
name:String

the name of the class

Returns:byte[]

the byte content (with transformers already applied), or null if no class defined for that name

Annotations
@Nullable
Exceptions
ClassNotFoundException:
if the class for the given name couldn't be loaded
loadClassback to summary
public Class<?> loadClass(String name) throws ClassNotFoundException

Overrides java.lang.ClassLoader.loadClass.

Doc from java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name. This method searches for classes in the same manner as the loadClass(String, boolean) method. It is invoked by the Java virtual machine to resolve class references. Invoking this method is equivalent to invoking loadClass(name, false).

Parameters
name:String

The binary name of the class

Returns:Class<?>

The resulting Class object

Annotations
@Override
Exceptions
ClassNotFoundException:
If the class was not found
loadClassback to summary
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException

Overrides java.lang.ClassLoader.loadClass.

Doc from java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name. The default implementation of this method searches for classes in the following order:

  1. Invoke findLoadedClass(String) to check if the class has already been loaded.

  2. Invoke the loadClass method on the parent class loader. If the parent is null the class loader built into the virtual machine is used, instead.

  3. Invoke the findClass(String) method to find the class.

If the class was found using the above steps, and the resolve flag is true, this method will then invoke the resolveClass(Class) method on the resulting Class object.

Subclasses of ClassLoader are encouraged to override findClass(String), rather than this method.

Unless overridden, this method synchronizes on the result of getClassLoadingLock method during the entire class loading process.

Parameters
name:String

The binary name of the class

resolve:boolean

If true then resolve the class

Returns:Class<?>

The resulting Class object

Annotations
@Override
Exceptions
ClassNotFoundException:
If the class could not be found
loadClassForOverridingback to summary
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException

Load the specified class for overriding purposes in this ClassLoader.

The default implementation delegates to findLoadedClass, loadBytesForClass and defineClass.

Parameters
name:String

the name of the class

Returns:Class<?>

the Class object, or null if no class defined for that name

Annotations
@Nullable
Exceptions
ClassNotFoundException:
if the class for the given name couldn't be loaded
openStreamForClassback to summary
protected InputStream openStreamForClass(String name)

Open an InputStream for the specified class.

The default implementation loads a standard class file through the parent ClassLoader's getResourceAsStream method.

Parameters
name:String

the name of the class

Returns:InputStream

the InputStream containing the byte code for the specified class

Annotations
@Nullable
transformIfNecessaryback to summary
protected byte[] transformIfNecessary(String name, byte[] bytes)

Transformation hook to be implemented by subclasses.

The default implementation simply returns the given bytes as-is.

Parameters
name:String

the fully-qualified name of the class being transformed

bytes:byte[]

the raw bytes of the class

Returns:byte[]

the transformed bytes (never null; same as the input bytes if the transformation produced no changes)