Top Description Fields Constructors Methods
org.springframework.instrument.classloading

public Class ShadowingClassLoader

extends DecoratingClassLoader
Class Inheritance
Known Direct Subclasses
org.springframework.instrument.classloading.ResourceOverridingShadowingClassLoader
Imports
java.io.IOException, .InputStream, java.lang.instrument.ClassFileTransformer, .IllegalClassFormatException, java.net.URL, java.util.ArrayList, .Enumeration, .HashMap, .List, .Map, org.springframework.core.DecoratingClassLoader, org.springframework.lang.Nullable, org.springframework.util.Assert, .FileCopyUtils, .StringUtils

ClassLoader decorator that shadows an enclosing ClassLoader, applying registered transformers to all affected classes.
Authors
Rob Harrop, Juergen Hoeller, Costin Leau
Since
2.0
See Also
addTransformer, org.springframework.core.OverridingClassLoader

Field Summary

Modifier and TypeField and Description
private final Map<String, Class<?>>
private final List<ClassFileTransformer>
public static final String[]
DEFAULT_EXCLUDED_PACKAGES

Packages that are excluded by default.

private final ClassLoader

Constructor Summary

AccessConstructor and Description
public
ShadowingClassLoader(ClassLoader
the ClassLoader to decorate
enclosingClassLoader
)

Create a new ShadowingClassLoader, decorating the given ClassLoader, applying DEFAULT_EXCLUDED_PACKAGES.

public
ShadowingClassLoader(ClassLoader
the ClassLoader to decorate
enclosingClassLoader
,
boolean
whether to apply DEFAULT_EXCLUDED_PACKAGES
defaultExcludes
)

Create a new ShadowingClassLoader, decorating the given ClassLoader.

Method Summary

Modifier and TypeMethod and Description
public void
addTransformer(ClassFileTransformer
the ClassFileTransformer
transformer
)

Add the given ClassFileTransformer to the list of transformers that this ClassLoader will apply.

private byte[]
applyTransformers(String name, byte[] bytes)

public void
copyTransformers(ShadowingClassLoader
the ClassLoader to copy from
other
)

Copy all ClassFileTransformers from the given ClassLoader to the list of transformers that this ClassLoader will apply.

private Class<?>
public URL
getResource(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResource.

Finds the resource with the given name.

public InputStream
getResourceAsStream(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResourceAsStream.

Returns an input stream for reading the specified resource.

public Enumeration<URL>
getResources(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResources.

Finds all the resources with the given name.

protected boolean

Returns:

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

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

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

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.

private boolean

Returns:

whether the specified class should be shadowed
shouldShadow
(String
the name of the class
className
)

Determine whether the given class should be excluded from shadowing.

Inherited from org.springframework.core.DecoratingClassLoader:
excludeClassexcludePackageisExcluded

Field Detail

classCacheback to summary
private final Map<String, Class<?>> classCache
classFileTransformersback to summary
private final List<ClassFileTransformer> classFileTransformers
DEFAULT_EXCLUDED_PACKAGESback to summary
public static final String[] DEFAULT_EXCLUDED_PACKAGES

Packages that are excluded by default.

enclosingClassLoaderback to summary
private final ClassLoader enclosingClassLoader

Constructor Detail

ShadowingClassLoaderback to summary
public ShadowingClassLoader(ClassLoader enclosingClassLoader)

Create a new ShadowingClassLoader, decorating the given ClassLoader, applying DEFAULT_EXCLUDED_PACKAGES.

Parameters
enclosingClassLoader:ClassLoader

the ClassLoader to decorate

See Also
ShadowingClassLoader(ClassLoader, boolean)
ShadowingClassLoaderback to summary
public ShadowingClassLoader(ClassLoader enclosingClassLoader, boolean defaultExcludes)

Create a new ShadowingClassLoader, decorating the given ClassLoader.

Parameters
enclosingClassLoader:ClassLoader

the ClassLoader to decorate

defaultExcludes:boolean

whether to apply DEFAULT_EXCLUDED_PACKAGES

Since
4.3.8

Method Detail

addTransformerback to summary
public void addTransformer(ClassFileTransformer transformer)

Add the given ClassFileTransformer to the list of transformers that this ClassLoader will apply.

Parameters
transformer:ClassFileTransformer

the ClassFileTransformer

applyTransformersback to summary
private byte[] applyTransformers(String name, byte[] bytes)
copyTransformersback to summary
public void copyTransformers(ShadowingClassLoader other)

Copy all ClassFileTransformers from the given ClassLoader to the list of transformers that this ClassLoader will apply.

Parameters
other:ShadowingClassLoader

the ClassLoader to copy from

doLoadClassback to summary
private Class<?> doLoadClass(String name) throws ClassNotFoundException
getResourceback to summary
public URL getResource(String name)

Overrides java.lang.ClassLoader.getResource.

Doc from java.lang.ClassLoader.getResource.

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a '/'-separated path name that identifies the resource.

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).

Parameters
name:String

The resource name

Returns:URL

URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.

Annotations
@Override
getResourceAsStreamback to summary
public InputStream getResourceAsStream(String name)

Overrides java.lang.ClassLoader.getResourceAsStream.

Doc from java.lang.ClassLoader.getResourceAsStream.

Returns an input stream for reading the specified resource.

The search order is described in the documentation for getResource(String).

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally.

Parameters
name:String

The resource name

Returns:InputStream

An input stream for reading the resource; null if the resource could not be found, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.

Annotations
@Override
@Nullable
getResourcesback to summary
public Enumeration<URL> getResources(String name) throws IOException

Overrides java.lang.ClassLoader.getResources.

Doc from java.lang.ClassLoader.getResources.

Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a /-separated path name that identifies the resource.

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).

Parameters
name:String

The resource name

Returns:Enumeration<URL>

An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.

Annotations
@Override
Exceptions
IOException:
If I/O errors occur
isEligibleForShadowingback to summary
protected boolean isEligibleForShadowing(String className)

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

Parameters
className:String

the class name to check

Returns:boolean

whether the specified class is eligible

See Also
isExcluded
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
shouldShadowback to summary
private boolean shouldShadow(String className)

Determine whether the given class should be excluded from shadowing.

Parameters
className:String

the name of the class

Returns:boolean

whether the specified class should be shadowed