@HashCodeAndEqualsPlugin.Enhance public class ClassReloadingStrategy extends Object implements ClassLoadingStrategy<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 the ByteBuddy.redefine(Class)
adds a method if the original class contains an explicit class initializer. For these reasons, it is not
recommended to use this ClassLoadingStrategy
with arbitrary classes.
Modifier and Type | Class and 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 a
Class . |
ClassLoadingStrategy.Configurable<S extends ClassLoader>, ClassLoadingStrategy.Default, ClassLoadingStrategy.ForBootstrapInjection, ClassLoadingStrategy.ForJnaInjection, ClassLoadingStrategy.ForUnsafeInjection, ClassLoadingStrategy.UsingLookup
Modifier and Type | Field and Description |
---|---|
protected static ClassReloadingStrategy.Dispatcher |
DISPATCHER
A dispatcher to use with some methods of the
Instrumentation API. |
BOOTSTRAP_LOADER, NO_PROTECTION_DOMAIN
Modifier | Constructor and Description |
---|---|
|
ClassReloadingStrategy(Instrumentation instrumentation,
ClassReloadingStrategy.Strategy strategy)
Creates a class reloading strategy for the given instrumentation using an explicit transformation strategy which
is represented by an
ClassReloadingStrategy.Strategy . |
protected |
ClassReloadingStrategy(Instrumentation instrumentation,
ClassReloadingStrategy.Strategy strategy,
ClassReloadingStrategy.BootstrapInjection bootstrapInjection,
Map<String,Class<?>> preregisteredTypes)
Creates a new class reloading strategy.
|
Modifier and Type | Method and Description |
---|---|
ClassReloadingStrategy |
enableBootstrapInjection(File folder)
Enables bootstrap injection for this class reloading strategy.
|
static ClassReloadingStrategy |
fromInstalledAgent()
Obtains a
ClassReloadingStrategy from an installed Byte Buddy agent. |
static ClassReloadingStrategy |
fromInstalledAgent(ClassReloadingStrategy.Strategy strategy)
Obtains a
ClassReloadingStrategy from an installed Byte Buddy agent. |
Map<TypeDescription,Class<?>> |
load(ClassLoader classLoader,
Map<TypeDescription,byte[]> types)
Loads a given collection of classes given their binary representation.
|
static ClassReloadingStrategy |
of(Instrumentation instrumentation)
Creates a class reloading strategy for the given instrumentation.
|
ClassReloadingStrategy |
preregistered(Class<?>... type)
Registers a type to be explicitly available without explicit lookup.
|
ClassReloadingStrategy |
reset(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,
Class<?>... type)
Resets all classes to their original definition.
|
protected static final ClassReloadingStrategy.Dispatcher DISPATCHER
Instrumentation
API.public ClassReloadingStrategy(Instrumentation instrumentation, ClassReloadingStrategy.Strategy strategy)
ClassReloadingStrategy.Strategy
. The given instrumentation
must support the strategy's transformation type.instrumentation
- The instrumentation to be used by this reloading strategy.strategy
- A strategy which performs the actual redefinition of a Class
.protected ClassReloadingStrategy(Instrumentation instrumentation, ClassReloadingStrategy.Strategy strategy, ClassReloadingStrategy.BootstrapInjection bootstrapInjection, Map<String,Class<?>> preregisteredTypes)
instrumentation
- The instrumentation to be used by this reloading strategy.strategy
- An strategy which performs the actual redefinition of a Class
.bootstrapInjection
- The bootstrap class loader injection strategy to use.preregisteredTypes
- The preregistered types of this instance.public static ClassReloadingStrategy of(Instrumentation instrumentation)
Instrumentation.isRedefineClassesSupported()
or
Instrumentation.isRetransformClassesSupported()
. If both modes are supported,
classes will be transformed using a class retransformation.instrumentation
- The instrumentation to be used by this reloading strategy.public static ClassReloadingStrategy fromInstalledAgent()
Obtains a ClassReloadingStrategy
from an installed Byte Buddy agent. This
agent must be installed either by adding the byte-buddy-agent.jar
when starting up the JVM by
java -javaagent:byte-buddy-agent.jar -jar app.jar
ByteBuddyAgent
within the byte-buddy-agent
module. The strategy is determined by the agent's support
for redefinition where are retransformation is preferred over a redefinition.Instrumentation
.public static ClassReloadingStrategy fromInstalledAgent(ClassReloadingStrategy.Strategy strategy)
Obtains a ClassReloadingStrategy
from an installed Byte Buddy agent. This
agent must be installed either by adding the byte-buddy-agent.jar
when starting up the JVM by
java -javaagent:byte-buddy-agent.jar -jar app.jar
ByteBuddyAgent
within the byte-buddy-agent
module.strategy
- The strategy to use.Instrumentation
.public Map<TypeDescription,Class<?>> load(@MaybeNull ClassLoader classLoader, Map<TypeDescription,byte[]> types)
load
in interface ClassLoadingStrategy<ClassLoader>
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.public ClassReloadingStrategy reset(Class<?>... type) throws IOException
type
- The types to reset.IOException
- If a class file locator causes an IO exception.public ClassReloadingStrategy reset(ClassFileLocator classFileLocator, Class<?>... type) throws IOException
classFileLocator
- The class file locator to use.type
- The types to reset.IOException
- If a class file locator causes an IO exception.public ClassReloadingStrategy enableBootstrapInjection(File folder)
folder
- The folder to save jar files in that are appended to the bootstrap class path.public ClassReloadingStrategy preregistered(Class<?>... type)
type
- The loaded types that are explicitly available.Copyright © 2014–2025. All rights reserved.