Class ClassReloadingStrategy
- java.lang.Object
-
- net.bytebuddy.dynamic.loading.ClassReloadingStrategy
-
- All Implemented Interfaces:
ClassLoadingStrategy<java.lang.ClassLoader>
@Enhance public class ClassReloadingStrategy extends java.lang.Object implements ClassLoadingStrategy<java.lang.ClassLoader>
The class reloading strategy allows to redefine loaded
Class
es. Note that this strategy will always attempt to load an existing class prior to its redefinition, even if this class is not yet loaded.Note: In order to redefine any type, neither its name or its modifiers must be changed. Furthermore, no fields or methods must be removed or added. This makes this strategy generally incompatible to applying it to a rebased class definition as by
ByteBuddy.rebase(Class)
which copies the original method implementations to additional methods. Furthermore, even theByteBuddy.redefine(Class)
adds a method if the original class contains an explicit class initializer. For these reasons, it is not recommended to use thisClassLoadingStrategy
with arbitrary classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interface
ClassReloadingStrategy.BootstrapInjection
A strategy to apply for injecting classes into the bootstrap class loader.protected static interface
ClassReloadingStrategy.Dispatcher
A dispatcher to interact with the instrumentation API.static class
ClassReloadingStrategy.Strategy
A strategy which performs the actual redefinition of aClass
.-
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
-
-
Field Summary
Fields Modifier and Type Field Description private ClassReloadingStrategy.BootstrapInjection
bootstrapInjection
The strategy to apply for injecting classes into the bootstrap class loader.protected static ClassReloadingStrategy.Dispatcher
DISPATCHER
A dispatcher to use with some methods of theInstrumentation
API.private static java.lang.String
INSTALLER_TYPE
The name of the Byte Buddynet.bytebuddy.agent.Installer
class.private java.lang.instrument.Instrumentation
instrumentation
This instance's instrumentation.private static java.lang.String
INSTRUMENTATION_GETTER
The name of thenet.bytebuddy.agent.Installer
getter for reading an installedInstrumentation
.private java.util.Map<java.lang.String,java.lang.Class<?>>
preregisteredTypes
The preregistered types of this instance.private static java.lang.Object
STATIC_MEMBER
Indicator for access to a static member via reflection to make the code more readable.private ClassReloadingStrategy.Strategy
strategy
An strategy which performs the actual redefinition of aClass
.-
Fields inherited from interface net.bytebuddy.dynamic.loading.ClassLoadingStrategy
BOOTSTRAP_LOADER, NO_PROTECTION_DOMAIN
-
-
Constructor Summary
Constructors Modifier Constructor Description ClassReloadingStrategy(java.lang.instrument.Instrumentation instrumentation, ClassReloadingStrategy.Strategy strategy)
Creates a class reloading strategy for the given instrumentation using an explicit transformation strategy which is represented by anClassReloadingStrategy.Strategy
.protected
ClassReloadingStrategy(java.lang.instrument.Instrumentation instrumentation, ClassReloadingStrategy.Strategy strategy, ClassReloadingStrategy.BootstrapInjection bootstrapInjection, java.util.Map<java.lang.String,java.lang.Class<?>> preregisteredTypes)
Creates a new class reloading strategy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ClassReloadingStrategy
enableBootstrapInjection(java.io.File folder)
Enables bootstrap injection for this class reloading strategy.static ClassReloadingStrategy
fromInstalledAgent()
Obtains aClassReloadingStrategy
from an installed Byte Buddy agent.static ClassReloadingStrategy
fromInstalledAgent(ClassReloadingStrategy.Strategy strategy)
Obtains aClassReloadingStrategy
from an installed Byte Buddy agent.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.static ClassReloadingStrategy
of(java.lang.instrument.Instrumentation instrumentation)
Creates a class reloading strategy for the given instrumentation.ClassReloadingStrategy
preregistered(java.lang.Class<?>... type)
Registers a type to be explicitly available without explicit lookup.ClassReloadingStrategy
reset(java.lang.Class<?>... type)
Resets all classes to their original definition while using the first type's class loader as a class file locator.ClassReloadingStrategy
reset(ClassFileLocator classFileLocator, java.lang.Class<?>... type)
Resets all classes to their original definition.
-
-
-
Field Detail
-
INSTALLER_TYPE
private static final java.lang.String INSTALLER_TYPE
The name of the Byte Buddynet.bytebuddy.agent.Installer
class.- See Also:
- Constant Field Values
-
INSTRUMENTATION_GETTER
private static final java.lang.String INSTRUMENTATION_GETTER
The name of thenet.bytebuddy.agent.Installer
getter for reading an installedInstrumentation
.- See Also:
- Constant Field Values
-
STATIC_MEMBER
private static final java.lang.Object STATIC_MEMBER
Indicator for access to a static member via reflection to make the code more readable.
-
DISPATCHER
protected static final ClassReloadingStrategy.Dispatcher DISPATCHER
A dispatcher to use with some methods of theInstrumentation
API.
-
instrumentation
private final java.lang.instrument.Instrumentation instrumentation
This instance's instrumentation.
-
strategy
private final ClassReloadingStrategy.Strategy strategy
An strategy which performs the actual redefinition of aClass
.
-
bootstrapInjection
private final ClassReloadingStrategy.BootstrapInjection bootstrapInjection
The strategy to apply for injecting classes into the bootstrap class loader.
-
preregisteredTypes
private final java.util.Map<java.lang.String,java.lang.Class<?>> preregisteredTypes
The preregistered types of this instance.
-
-
Constructor Detail
-
ClassReloadingStrategy
public ClassReloadingStrategy(java.lang.instrument.Instrumentation instrumentation, ClassReloadingStrategy.Strategy strategy)
Creates a class reloading strategy for the given instrumentation using an explicit transformation strategy which is represented by anClassReloadingStrategy.Strategy
. The given instrumentation must support the strategy's transformation type.- Parameters:
instrumentation
- The instrumentation to be used by this reloading strategy.strategy
- A strategy which performs the actual redefinition of aClass
.
-
ClassReloadingStrategy
protected ClassReloadingStrategy(java.lang.instrument.Instrumentation instrumentation, ClassReloadingStrategy.Strategy strategy, ClassReloadingStrategy.BootstrapInjection bootstrapInjection, java.util.Map<java.lang.String,java.lang.Class<?>> preregisteredTypes)
Creates a new class reloading strategy.- Parameters:
instrumentation
- The instrumentation to be used by this reloading strategy.strategy
- An strategy which performs the actual redefinition of aClass
.bootstrapInjection
- The bootstrap class loader injection strategy to use.preregisteredTypes
- The preregistered types of this instance.
-
-
Method Detail
-
of
public static ClassReloadingStrategy of(java.lang.instrument.Instrumentation instrumentation)
Creates a class reloading strategy for the given instrumentation. The given instrumentation must either supportInstrumentation.isRedefineClassesSupported()
orInstrumentation.isRetransformClassesSupported()
. If both modes are supported, classes will be transformed using a class retransformation.- Parameters:
instrumentation
- The instrumentation to be used by this reloading strategy.- Returns:
- A suitable class reloading strategy.
-
fromInstalledAgent
public static ClassReloadingStrategy fromInstalledAgent()
Obtains a
ClassReloadingStrategy
from an installed Byte Buddy agent. This agent must be installed either by adding thebyte-buddy-agent.jar
when starting up the JVM byjava -javaagent:byte-buddy-agent.jar -jar app.jar
ByteBuddyAgent
within thebyte-buddy-agent
module. The strategy is determined by the agent's support for redefinition where are retransformation is preferred over a redefinition.- Returns:
- A class reloading strategy which uses the Byte Buddy agent's
Instrumentation
.
-
fromInstalledAgent
public static ClassReloadingStrategy fromInstalledAgent(ClassReloadingStrategy.Strategy strategy)
Obtains a
ClassReloadingStrategy
from an installed Byte Buddy agent. This agent must be installed either by adding thebyte-buddy-agent.jar
when starting up the JVM byjava -javaagent:byte-buddy-agent.jar -jar app.jar
ByteBuddyAgent
within thebyte-buddy-agent
module.- Parameters:
strategy
- The strategy to use.- Returns:
- A class reloading strategy which uses the Byte Buddy agent's
Instrumentation
.
-
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.
-
reset
public ClassReloadingStrategy reset(java.lang.Class<?>... type) throws java.io.IOException
Resets all classes to their original definition while using the first type's class loader as a class file locator.- Parameters:
type
- The types to reset.- Returns:
- This class reloading strategy.
- Throws:
java.io.IOException
- If a class file locator causes an IO exception.
-
reset
public ClassReloadingStrategy reset(ClassFileLocator classFileLocator, java.lang.Class<?>... type) throws java.io.IOException
Resets all classes to their original definition.- Parameters:
classFileLocator
- The class file locator to use.type
- The types to reset.- Returns:
- This class reloading strategy.
- Throws:
java.io.IOException
- If a class file locator causes an IO exception.
-
enableBootstrapInjection
public ClassReloadingStrategy enableBootstrapInjection(java.io.File folder)
Enables bootstrap injection for this class reloading strategy.- Parameters:
folder
- The folder to save jar files in that are appended to the bootstrap class path.- Returns:
- A class reloading strategy with bootstrap injection enabled.
-
preregistered
public ClassReloadingStrategy preregistered(java.lang.Class<?>... type)
Registers a type to be explicitly available without explicit lookup.- Parameters:
type
- The loaded types that are explicitly available.- Returns:
- This class reloading strategy with the given types being explicitly available.
-
-