Enum ClassLoadingStrategy.Default
- java.lang.Object
-
- java.lang.Enum<ClassLoadingStrategy.Default>
-
- net.bytebuddy.dynamic.loading.ClassLoadingStrategy.Default
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<ClassLoadingStrategy.Default>
,ClassLoadingStrategy<java.lang.ClassLoader>
,ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
- Enclosing interface:
- ClassLoadingStrategy<T extends java.lang.ClassLoader>
public static enum ClassLoadingStrategy.Default extends java.lang.Enum<ClassLoadingStrategy.Default> implements ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
This class contains implementations of default class loading strategies.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
ClassLoadingStrategy.Default.InjectionDispatcher
A class loading strategy which applies a class loader injection while applying a givenProtectionDomain
on class injection.protected static class
ClassLoadingStrategy.Default.WrappingDispatcher
A class loading strategy which creates a wrapping class loader while applying a givenProtectionDomain
on class loading.-
Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.loading.ClassLoadingStrategy
ClassLoadingStrategy.Configurable<S extends java.lang.ClassLoader>, ClassLoadingStrategy.Default, ClassLoadingStrategy.ForBootstrapInjection, ClassLoadingStrategy.ForUnsafeInjection, ClassLoadingStrategy.UsingLookup
-
-
Enum Constant Summary
Enum Constants Enum Constant Description CHILD_FIRST
The child-first class loading strategy is a modified version of theWRAPPER
where the dynamic types are given priority over any types of a parent class loader with the same name.CHILD_FIRST_PERSISTENT
The strategy is identical toCHILD_FIRST
but exposes the byte arrays that represent a class byClassLoader.getResourceAsStream(String)
.INJECTION
This strategy does not create a new class loader but injects all classes into the givenClassLoader
by reflective access.WRAPPER
This strategy creates a newByteArrayClassLoader
with the given class loader as its parent.WRAPPER_PERSISTENT
The strategy is identical toWRAPPER
but exposes the byte arrays that represent a class byClassLoader.getResourceAsStream(String)
.
-
Field Summary
Fields Modifier and Type Field Description private static boolean
DEFAULT_FORBID_EXISTING
The default behavior when attempting to load a type that was already loaded.private ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
dispatcher
The dispatcher to be used when loading a class.-
Fields inherited from interface net.bytebuddy.dynamic.loading.ClassLoadingStrategy
BOOTSTRAP_LOADER, NO_PROTECTION_DOMAIN
-
-
Constructor Summary
Constructors Modifier Constructor Description private
Default(ClassLoadingStrategy.Configurable<java.lang.ClassLoader> dispatcher)
Creates a new default class loading strategy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
allowExistingTypes()
Determines if this class loading strategy should not throw an exception when attempting to load a class that was already loaded.java.util.Map<TypeDescription,java.lang.Class<?>>
load(java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types)
Loads a given collection of classes given their binary representation.ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
opened()
With an opened class loading strategy, it is assured that types can be added to the class loader, either by indirect injection using this strategy or by creating a class loader that explicitly supports injection.static ClassLoadingStrategy.Default
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.static ClassLoadingStrategy.Default[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
with(java.security.ProtectionDomain protectionDomain)
Overrides the implicitly set defaultProtectionDomain
with an explicit one.ClassLoadingStrategy.Configurable<java.lang.ClassLoader>
with(PackageDefinitionStrategy packageDefinitionStrategy)
Defines the supplied package definition strategy to be used for defining packages.
-
-
-
Enum Constant Detail
-
WRAPPER
public static final ClassLoadingStrategy.Default WRAPPER
This strategy creates a newByteArrayClassLoader
with the given class loader as its parent. The byte array class loader is aware of a any dynamically created type and can natively load the given classes. This allows to load classes with cyclic load-time dependencies since the byte array class loader is queried on each encountered unknown class. Due to the encapsulation of the classes that were loaded by a byte array class loader, this strategy will lead to the unloading of these classes once this class loader, its classes or any instances of these classes become unreachable.
-
WRAPPER_PERSISTENT
public static final ClassLoadingStrategy.Default WRAPPER_PERSISTENT
The strategy is identical toWRAPPER
but exposes the byte arrays that represent a class byClassLoader.getResourceAsStream(String)
. For this purpose, all class files are persisted as byte arrays withing the wrapping class loader.
-
CHILD_FIRST
public static final ClassLoadingStrategy.Default CHILD_FIRST
The child-first class loading strategy is a modified version of the
WRAPPER
where the dynamic types are given priority over any types of a parent class loader with the same name.Important: This does not replace a type of the same name, but it makes the type invisible by the reach of this class loader.
-
CHILD_FIRST_PERSISTENT
public static final ClassLoadingStrategy.Default CHILD_FIRST_PERSISTENT
The strategy is identical toCHILD_FIRST
but exposes the byte arrays that represent a class byClassLoader.getResourceAsStream(String)
. For this purpose, all class files are persisted as byte arrays withing the wrapping class loader.
-
INJECTION
public static final ClassLoadingStrategy.Default INJECTION
This strategy does not create a new class loader but injects all classes into the given
ClassLoader
by reflective access. This prevents the loading of classes with cyclic load-time dependencies but avoids the creation of an additional class loader. The advantage of this strategy is that the loaded classes will have package-private access to other classes within their package of the class loader into which they are injected what is not permitted when the wrapper class loader is used. This strategy is implemented using aClassInjector.UsingReflection
. Note that this strategy usually yields a better runtime performance.Important: Class injection requires access to JVM internal methods that are sealed by security managers and the Java Platform module system. Since Java 11, access to these methods is no longer feasible unless those packages are explicitly opened.
Note: This class loader does not define packages for injected classes by default. Therefore, calls to
Class.getPackage()
might returnnull
. Packages are only defined manually by a class loader prior to Java 9.
-
-
Field Detail
-
DEFAULT_FORBID_EXISTING
private static final boolean DEFAULT_FORBID_EXISTING
The default behavior when attempting to load a type that was already loaded.- See Also:
- Constant Field Values
-
dispatcher
private final ClassLoadingStrategy.Configurable<java.lang.ClassLoader> dispatcher
The dispatcher to be used when loading a class.
-
-
Constructor Detail
-
Default
private Default(ClassLoadingStrategy.Configurable<java.lang.ClassLoader> dispatcher)
Creates a new default class loading strategy.- Parameters:
dispatcher
- The dispatcher to be used when loading a class.
-
-
Method Detail
-
values
public static ClassLoadingStrategy.Default[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (ClassLoadingStrategy.Default c : ClassLoadingStrategy.Default.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static ClassLoadingStrategy.Default valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is null
-
load
public java.util.Map<TypeDescription,java.lang.Class<?>> load(java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types)
Loads a given collection of classes given their binary representation.- Specified by:
load
in interfaceClassLoadingStrategy<java.lang.ClassLoader>
- Parameters:
classLoader
- The class loader to used for loading the classes.types
- Byte array representations of the types to be loaded mapped by their descriptions, where an iteration order defines an order in which they are supposed to be loaded, if relevant.- Returns:
- A collection of the loaded classes which will be initialized in the iteration order of the returned collection.
-
with
public ClassLoadingStrategy.Configurable<java.lang.ClassLoader> with(java.security.ProtectionDomain protectionDomain)
Overrides the implicitly set defaultProtectionDomain
with an explicit one.- Specified by:
with
in interfaceClassLoadingStrategy.Configurable<java.lang.ClassLoader>
- Parameters:
protectionDomain
- The protection domain to apply ornull
if no protection domain is set.- Returns:
- This class loading strategy with an explicitly set
ProtectionDomain
.
-
with
public ClassLoadingStrategy.Configurable<java.lang.ClassLoader> with(PackageDefinitionStrategy packageDefinitionStrategy)
Defines the supplied package definition strategy to be used for defining packages.- Specified by:
with
in interfaceClassLoadingStrategy.Configurable<java.lang.ClassLoader>
- Parameters:
packageDefinitionStrategy
- The package definer to be used.- Returns:
- A version of this class loading strategy that applies the supplied package definition strategy.
-
allowExistingTypes
public ClassLoadingStrategy.Configurable<java.lang.ClassLoader> allowExistingTypes()
Determines if this class loading strategy should not throw an exception when attempting to load a class that was already loaded. In this case, the already loaded class is used instead of the generated class.- Specified by:
allowExistingTypes
in interfaceClassLoadingStrategy.Configurable<java.lang.ClassLoader>
- Returns:
- A version of this class loading strategy that does not throw an exception when a class is already loaded.
-
opened
public ClassLoadingStrategy.Configurable<java.lang.ClassLoader> opened()
With an opened class loading strategy, it is assured that types can be added to the class loader, either by indirect injection using this strategy or by creating a class loader that explicitly supports injection.- Specified by:
opened
in interfaceClassLoadingStrategy.Configurable<java.lang.ClassLoader>
- Returns:
- A version of this class loading strategy that opens for future injections into a class loader.
-
-