Top Description Inners Fields Constructors Methods
net.bytebuddy.dynamic.loading

public Class MultipleParentClassLoader

extends InjectionClassLoader
Class Inheritance
Imports
edu.umd.cs.findbugs.annotations.SuppressFBWarnings, net.bytebuddy.build.HashCodeAndEqualsPlugin, net.bytebuddy.matcher.ElementMatcher, net.bytebuddy.utility.nullability.MaybeNull, .UnknownNull, java.io.IOException, java.lang.reflect.Method, java.net.URL, java.util.*

This java.lang.ClassLoader is capable of loading classes from multiple parents. This class loader implicitly defines the bootstrap class loader to be its direct parent as it is required for all class loaders. This can be useful when creating a type that inherits a super type and interfaces that are defined by different, non-compatible class loaders.

Note

Instances of this class loader can have the same class loader as its parent multiple times, either directly or indirectly by multiple parents sharing a common parent class loader. By definition, this implies that the bootstrap class loader is #(direct parents) + 1 times a parent of this class loader. For the java.lang.ClassLoader#getResources(java.lang.String) method, this means that this class loader might return the same url multiple times by representing the same class loader multiple times.

Important Note

This class loader does not support the location of packages from its multiple parents. This breaks package equality when loading classes by either loading them directly via this class loader (e.g. by subclassing) or by loading classes with child class loaders of this class loader.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MultipleParentClassLoader.Builder

A builder to collect class loader and that creates a net.bytebuddy.dynamic.loading.MultipleParentClassLoader only if multiple or no java.lang.ClassLoaders are found in the process.

protected static class

Field Summary

Modifier and TypeField and Description
private final List<? extends ClassLoader>
parents

The parents of this class loader in their application order.

Constructor Summary

AccessConstructor and Description
public
MultipleParentClassLoader(List<? extends ClassLoader>
The parents of this class loader in their application order. This list must not contain null, i.e. the bootstrap class loader which is an implicit parent of any class loader.
parents
)

Creates a new class loader with multiple parents.

public
MultipleParentClassLoader(ClassLoader
An explicit parent in compliance with the class loader API. This explicit parent should only be set if the current platform does not allow creating a class loader that extends the bootstrap loader.
parent
,
List<? extends ClassLoader>
The parents of this class loader in their application order. This list must not contain null, i.e. the bootstrap class loader which is an implicit parent of any class loader.
parents
)

Creates a new class loader with multiple parents.

public
MultipleParentClassLoader(ClassLoader
An explicit parent in compliance with the class loader API. This explicit parent should only be set if the current platform does not allow creating a class loader that extends the bootstrap loader.
parent
,
List<? extends ClassLoader>
The parents of this class loader in their application order. This list must not contain null, i.e. the bootstrap class loader which is an implicit parent of any class loader.
parents
,
boolean
true if the class loader is sealed for injection of additional classes.
sealed
)

Creates a new class loader with multiple parents.

Method Summary

Modifier and TypeMethod and Description
protected Map<String, Class<?>>
doDefineClasses(Map<String, byte[]>
The types binary representations.
typeDefinitions
)

Implements abstract net.bytebuddy.dynamic.loading.InjectionClassLoader.doDefineClasses.

Defines a group of types to be loaded by this class loader.
private static void
public URL
getResource(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResource.

Finds the resource with the given name.
public Enumeration<URL>
getResources(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResources.

Finds all the resources with the given 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.
Inherited from net.bytebuddy.dynamic.loading.InjectionClassLoader:
defineClassdefineClassesisSealedseal

Field Detail

parentsback to summary
private final List<? extends ClassLoader> parents

The parents of this class loader in their application order.

Constructor Detail

MultipleParentClassLoaderback to summary
public MultipleParentClassLoader(List<? extends ClassLoader> parents)

Creates a new class loader with multiple parents.

Parameters
parents:List<? extends ClassLoader>

The parents of this class loader in their application order. This list must not contain null, i.e. the bootstrap class loader which is an implicit parent of any class loader.

MultipleParentClassLoaderback to summary
public MultipleParentClassLoader(ClassLoader parent, List<? extends ClassLoader> parents)

Creates a new class loader with multiple parents.

Parameters
parent:ClassLoader

An explicit parent in compliance with the class loader API. This explicit parent should only be set if the current platform does not allow creating a class loader that extends the bootstrap loader.

parents:List<? extends ClassLoader>

The parents of this class loader in their application order. This list must not contain null, i.e. the bootstrap class loader which is an implicit parent of any class loader.

MultipleParentClassLoaderback to summary
public MultipleParentClassLoader(ClassLoader parent, List<? extends ClassLoader> parents, boolean sealed)

Creates a new class loader with multiple parents.

Parameters
parent:ClassLoader

An explicit parent in compliance with the class loader API. This explicit parent should only be set if the current platform does not allow creating a class loader that extends the bootstrap loader.

parents:List<? extends ClassLoader>

The parents of this class loader in their application order. This list must not contain null, i.e. the bootstrap class loader which is an implicit parent of any class loader.

sealed:boolean

true if the class loader is sealed for injection of additional classes.

Method Detail

doDefineClassesback to summary
protected Map<String, Class<?>> doDefineClasses(Map<String, byte[]> typeDefinitions)

Implements abstract net.bytebuddy.dynamic.loading.InjectionClassLoader.doDefineClasses.

Doc from net.bytebuddy.dynamic.loading.InjectionClassLoader.doDefineClasses.

Defines a group of types to be loaded by this class loader.

Parameters
typeDefinitions:Map<String, byte[]>

The types binary representations.

Returns:Map<String, Class<?>>

The mapping of defined classes or previously defined classes by their name.

Annotations
@Override
doRegisterAsParallelCapableback to summary
private static void doRegisterAsParallelCapable()

Hides net.bytebuddy.dynamic.loading.InjectionClassLoader.doRegisterAsParallelCapable.

Registers class loader as parallel capable if possible.

Annotations
@SuppressFBWarnings:DP_DO_INSIDE_DO_PRIVILEGED
justification:Must be invoked from targeting class loader type.
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.

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.

Exceptions
IOException:
If I/O errors occur
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

Exceptions
ClassNotFoundException:
If the class could not be found
net.bytebuddy.dynamic.loading back to summary

public Class MultipleParentClassLoader.Builder

extends Object
Class Inheritance
  • java.lang.Object
  • net.bytebuddy.dynamic.loading.MultipleParentClassLoader.Builder
Annotations
@Enhance

A builder to collect class loader and that creates a net.bytebuddy.dynamic.loading.MultipleParentClassLoader only if multiple or no java.lang.ClassLoaders are found in the process. If exactly a single class loader is found, this class loader is returned. All class loaders are applied in their collection order with the exception of the bootstrap class loader which is represented by null and which is an implicit parent of any class loader.

Field Summary

Modifier and TypeField and Description
private final List<? extends ClassLoader>
classLoaders

The class loaders that were collected.

private final boolean
sealed

true if the created class loader is sealed.

Constructor Summary

AccessConstructor and Description
public
Builder()

Creates a new builder without any class loaders.

public
Builder(boolean
true if the created class loader is sealed.
sealed
)

Creates a new builder without any class loaders.

private
Builder(List<? extends ClassLoader>
The class loaders that were collected until now.
classLoaders
,
boolean
true if the created class loader is sealed.
sealed
)

Creates a new builder.

Method Summary

Modifier and TypeMethod and Description
public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders of the provided types if they were not yet collected.
append
(Class<?>...
The types of which to collect the class loaders.
type
)

Appends the class loaders of the given types.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders.
append
(Collection<? extends Class<?>>
The types of which to collect the class loaders.
types
)

Appends the class loaders of the given types if those class loaders were not yet collected.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders.
append
(ClassLoader...
The class loaders to be collected.
classLoader
)

Appends the given class loaders if they were not yet collected.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders.
append
(List<? extends ClassLoader>
The class loaders to collected.
classLoaders
)

Appends the given class loaders if they were not yet appended.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders of the provided types if they were not yet collected.
appendMostSpecific
(Class<?>...
The types of which to collect the class loaders.
type
)

Appends the class loaders of the given types but filters any duplicates within the hierarchy of class loaders.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders.
appendMostSpecific
(Collection<? extends Class<?>>
The types of which to collect the class loaders.
types
)

Appends the class loaders of the given types but filters any duplicates within the hierarchy of class loaders.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders.
appendMostSpecific
(ClassLoader...
The class loaders to be collected.
classLoader
)

Appends the given class loaders but removes any class loaders that are the parent of any previously registered class loader.

public MultipleParentClassLoader.Builder

Returns:

A new builder instance with the additional class loaders.
appendMostSpecific
(List<? extends ClassLoader>
The class loaders to collected.
classLoaders
)

Appends the given class loaders but removes any class loaders that are the parent of any previously registered class loader.

public ClassLoader

Returns:

A suitable class loader.
build
()

Returns the only class loader that was appended if exactly one class loader was appended or a multiple parent class loader as a parent of all supplied class loader and with the bootstrap class loader as an implicit parent.

public ClassLoader

Returns:

A suitable class loader.
build
(ClassLoader
The class loader's contractual parent which is accessible via ClassLoader#getParent(). If this class loader is also included in the appended class loaders, it is not
parent
)

Returns the only class loader that was appended if exactly one class loader was appended or a multiple parent class loader as a parent of all supplied class loader and with the bootstrap class loader as an implicit parent.

private ClassLoader

Returns:

A multiple parent class loader that includes all collected class loaders and the explicit parent.
doBuild
(ClassLoader
The explicit parent class loader.
parent
)

Creates a multiple parent class loader with an explicit parent.

public MultipleParentClassLoader.Builder

Returns:

A builder that does not longer consider any appended class loaders that matched the provided matcher.
filter
(ElementMatcher<? super ClassLoader>
The matcher to be used for filtering.
matcher
)

Only retains all class loaders that match the given matcher.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

classLoadersback to summary
private final List<? extends ClassLoader> classLoaders

The class loaders that were collected.

sealedback to summary
private final boolean sealed

true if the created class loader is sealed.

Constructor Detail

Builderback to summary
public Builder()

Creates a new builder without any class loaders.

Builderback to summary
public Builder(boolean sealed)

Creates a new builder without any class loaders.

Parameters
sealed:boolean

true if the created class loader is sealed.

Builderback to summary
private Builder(List<? extends ClassLoader> classLoaders, boolean sealed)

Creates a new builder.

Parameters
classLoaders:List<? extends ClassLoader>

The class loaders that were collected until now.

sealed:boolean

true if the created class loader is sealed.

Method Detail

appendback to summary
public MultipleParentClassLoader.Builder append(Class<?>... type)

Appends the class loaders of the given types. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader.

Parameters
type:Class<?>[]

The types of which to collect the class loaders.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders of the provided types if they were not yet collected.

appendback to summary
public MultipleParentClassLoader.Builder append(Collection<? extends Class<?>> types)

Appends the class loaders of the given types if those class loaders were not yet collected. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader.

Parameters
types:Collection<? extends Class<?>>

The types of which to collect the class loaders.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders.

appendback to summary
public MultipleParentClassLoader.Builder append(ClassLoader... classLoader)

Appends the given class loaders if they were not yet collected. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader.

Parameters
classLoader:ClassLoader[]

The class loaders to be collected.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders.

appendback to summary
public MultipleParentClassLoader.Builder append(List<? extends ClassLoader> classLoaders)

Appends the given class loaders if they were not yet appended. The bootstrap class loader is never appended as it is an implicit parent of any class loader.

Parameters
classLoaders:List<? extends ClassLoader>

The class loaders to collected.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders.

appendMostSpecificback to summary
public MultipleParentClassLoader.Builder appendMostSpecific(Class<?>... type)

Appends the class loaders of the given types but filters any duplicates within the hierarchy of class loaders. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader. Class loaders are prepended to the list of class loaders.

Parameters
type:Class<?>[]

The types of which to collect the class loaders.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders of the provided types if they were not yet collected.

appendMostSpecificback to summary
public MultipleParentClassLoader.Builder appendMostSpecific(Collection<? extends Class<?>> types)

Appends the class loaders of the given types but filters any duplicates within the hierarchy of class loaders. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader. Class loaders are prepended to the list of class loaders.

Parameters
types:Collection<? extends Class<?>>

The types of which to collect the class loaders.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders.

appendMostSpecificback to summary
public MultipleParentClassLoader.Builder appendMostSpecific(ClassLoader... classLoader)

Appends the given class loaders but removes any class loaders that are the parent of any previously registered class loader. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader.

Parameters
classLoader:ClassLoader[]

The class loaders to be collected.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders.

appendMostSpecificback to summary
public MultipleParentClassLoader.Builder appendMostSpecific(List<? extends ClassLoader> classLoaders)

Appends the given class loaders but removes any class loaders that are the parent of any previously registered class loader. The bootstrap class loader is implicitly skipped as it is an implicit parent of any class loader.

Parameters
classLoaders:List<? extends ClassLoader>

The class loaders to collected.

Returns:MultipleParentClassLoader.Builder

A new builder instance with the additional class loaders.

buildback to summary
public ClassLoader build()

Returns the only class loader that was appended if exactly one class loader was appended or a multiple parent class loader as a parent of all supplied class loader and with the bootstrap class loader as an implicit parent. If no class loader was appended, a new class loader is created that declares no parents. If a class loader is created, its explicit parent is set to be the bootstrap class loader.

Important Note

Byte Buddy does not provide any access control for the creation of the class loader. It is the responsibility of the user of this builder to provide such privileges.

Returns:ClassLoader

A suitable class loader.

buildback to summary
public ClassLoader build(ClassLoader parent)

Returns the only class loader that was appended if exactly one class loader was appended or a multiple parent class loader as a parent of all supplied class loader and with the bootstrap class loader as an implicit parent. If no class loader was appended, or if only the supplied parent to this method was included, this class loader is returned,

Important Note

Byte Buddy does not provide any access control for the creation of the class loader. It is the responsibility of the user of this builder to provide such privileges.

Parameters
parent:ClassLoader

The class loader's contractual parent which is accessible via ClassLoader#getParent(). If this class loader is also included in the appended class loaders, it is not

Returns:ClassLoader

A suitable class loader.

doBuildback to summary
private ClassLoader doBuild(ClassLoader parent)

Creates a multiple parent class loader with an explicit parent.

Parameters
parent:ClassLoader

The explicit parent class loader.

Returns:ClassLoader

A multiple parent class loader that includes all collected class loaders and the explicit parent.

Annotations
@SuppressFBWarnings:DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED
justification:Assuring privilege is explicit user responsibility.
filterback to summary
public MultipleParentClassLoader.Builder filter(ElementMatcher<? super ClassLoader> matcher)

Only retains all class loaders that match the given matcher.

Parameters
matcher:ElementMatcher<? super ClassLoader>

The matcher to be used for filtering.

Returns:MultipleParentClassLoader.Builder

A builder that does not longer consider any appended class loaders that matched the provided matcher.

net.bytebuddy.dynamic.loading back to summary

protected Class MultipleParentClassLoader.CompoundEnumeration

extends Object
implements Enumeration<URL>
Class Inheritance
  • java.lang.Object
  • net.bytebuddy.dynamic.loading.MultipleParentClassLoader.CompoundEnumeration
All Implemented Interfaces
java.util.Enumeration

A compound URL enumeration.

Field Summary

Modifier and TypeField and Description
private Enumeration<URL>
current

The currently represented enumeration or null if no such enumeration is currently selected.

private final List<Enumeration<URL>>
enumerations

The remaining lists of enumerations.

private static final int
FIRST

Indicates the first index of a list.

Constructor Summary

AccessConstructor and Description
protected
CompoundEnumeration(List<Enumeration<URL>>
The enumerations to represent.
enumerations
)

Creates a compound enumeration.

Method Summary

Modifier and TypeMethod and Description
public boolean
hasMoreElements()

Implements java.util.Enumeration.hasMoreElements.

Tests if this enumeration contains more elements.
public URL
nextElement()

Implements java.util.Enumeration.nextElement.

Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentback to summary
private Enumeration<URL> current

The currently represented enumeration or null if no such enumeration is currently selected.

Annotations
@UnknownNull
enumerationsback to summary
private final List<Enumeration<URL>> enumerations

The remaining lists of enumerations.

FIRSTback to summary
private static final int FIRST

Indicates the first index of a list.

Constructor Detail

CompoundEnumerationback to summary
protected CompoundEnumeration(List<Enumeration<URL>> enumerations)

Creates a compound enumeration.

Parameters
enumerations:List<Enumeration<URL>>

The enumerations to represent.

Method Detail

hasMoreElementsback to summary
public boolean hasMoreElements()

Implements java.util.Enumeration.hasMoreElements.

Doc from java.util.Enumeration.hasMoreElements.

Tests if this enumeration contains more elements.

Returns:boolean

true if and only if this enumeration object contains at least one more element to provide; false otherwise.

nextElementback to summary
public URL nextElement()

Implements java.util.Enumeration.nextElement.

Doc from java.util.Enumeration.nextElement.

Returns the next element of this enumeration if this enumeration object has at least one more element to provide.

Returns:URL

the next element of this enumeration.

Annotations
@SuppressFBWarnings:UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR
justification:Null reference is avoided by element check.