Package net.bytebuddy.dynamic.loading
Class ClassLoadingStrategy.UsingLookup
java.lang.Object
net.bytebuddy.dynamic.loading.ClassLoadingStrategy.UsingLookup
- All Implemented Interfaces:
ClassLoadingStrategy<ClassLoader>
- Enclosing interface:
- ClassLoadingStrategy<T extends ClassLoader>
@Enhance
public static class ClassLoadingStrategy.UsingLookup
extends Object
implements ClassLoadingStrategy<ClassLoader>
A class loading strategy that uses a
java.lang.invoke.MethodHandles$Lookup
instance for defining types.
A lookup instance can define types only in the same class loader and in the same package as the type within which
it was created. The supplied lookup must have package privileges, i.e. it must not be a public lookup.-
Nested Class Summary
Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.loading.ClassLoadingStrategy
ClassLoadingStrategy.Configurable<S extends ClassLoader>, ClassLoadingStrategy.Default, ClassLoadingStrategy.ForBootstrapInjection, ClassLoadingStrategy.ForJnaInjection, ClassLoadingStrategy.ForUnsafeInjection, ClassLoadingStrategy.UsingLookup
-
Field Summary
FieldsFields inherited from interface net.bytebuddy.dynamic.loading.ClassLoadingStrategy
BOOTSTRAP_LOADER, NO_PROTECTION_DOMAIN
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
UsingLookup
(ClassInjector classInjector) Creates a new class loading strategy that uses a lookup type. -
Method Summary
Modifier and TypeMethodDescriptionMap<TypeDescription,
Class<?>> load
(ClassLoader classLoader, Map<TypeDescription, byte[]> types) Loads a given collection of classes given their binary representation.static ClassLoadingStrategy<ClassLoader>
Creates a new class loading strategy that uses ajava.lang.invoke.MethodHandles$Lookup
instance.static ClassLoadingStrategy<ClassLoader>
withFallback
(Callable<?> lookup) Resolves a class loading strategy using a lookup if available on the current JVM.static ClassLoadingStrategy<ClassLoader>
withFallback
(Callable<?> lookup, boolean wrapper) Resolves a class loading strategy using a lookup if available on the current JVM.
-
Field Details
-
classInjector
The class injector to use.
-
-
Constructor Details
-
UsingLookup
Creates a new class loading strategy that uses a lookup type.- Parameters:
classInjector
- The class injector to use.
-
-
Method Details
-
of
Creates a new class loading strategy that uses ajava.lang.invoke.MethodHandles$Lookup
instance.- Parameters:
lookup
- The lookup instance to use for defining new types.- Returns:
- A suitable class loading strategy.
-
withFallback
Resolves a class loading strategy using a lookup if available on the current JVM. If the current JVM supports method handles lookups, a lookup instance will be used. Alternatively, unsafe class definition is used, if supported. If neither strategy is supported, an exception is thrown. A common use case would be calling this method as inClassLoadingStrategy.UsingLookup.withFallback(MethodHandles::lookup)
. Note that the invocation ofMethodHandles.lookup()
is call site sensitive such that it cannot be invoked within Byte Buddy what requires this external invocation.- Parameters:
lookup
- A resolver for a lookup instance if the current JVM allows for lookup-based class injection.- Returns:
- An appropriate class loading strategy for the current JVM that uses a method handles lookup if available.
-
withFallback
Resolves a class loading strategy using a lookup if available on the current JVM. If the current JVM supports method handles lookups, a lookup instance will be used. Alternatively, unsafe class definition is used, if supported. If neither strategy is supported, an exception is thrown. A common use case would be calling this method as inClassLoadingStrategy.UsingLookup.withFallback(MethodHandles::lookup)
. Note that the invocation ofMethodHandles.lookup()
is call site sensitive such that it cannot be invoked within Byte Buddy what requires this external invocation.- Parameters:
lookup
- A resolver for a lookup instance if the current JVM allows for lookup-based class injection.wrapper
-true
if aClassLoadingStrategy.Default.WRAPPER
strategy should be used as a fallback in case that no injection strategy is available.- Returns:
- An appropriate class loading strategy for the current JVM that uses a method handles lookup if available.
-
load
public Map<TypeDescription,Class<?>> load(@MaybeNull ClassLoader classLoader, Map<TypeDescription, byte[]> types) Loads a given collection of classes given their binary representation.- Specified by:
load
in interfaceClassLoadingStrategy<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.
-