Package com.tngtech.jgiven.impl
Class ByteBuddyStageClassCreator
java.lang.Object
com.tngtech.jgiven.impl.ByteBuddyStageClassCreator
- All Implemented Interfaces:
StageClassCreator
Creates JGiven stage subclasses at runtime using ByteBuddy.
The class loading strategy is chosen in three tiers, in order of preference:
- Reflection-based injection (
ClassLoadingStrategy.Default.INJECTION) — used whenClassInjector.UsingReflection.isAvailable()returns true. This is the case when the class to be instrumented is not barred from injection via the Jave module system. (Mostly the case up to Java 21) The generated subclass is injected into the same classloader as the stage class viasun.misc.Unsafe/defineClassreflection, which preserves package membership. - Lookup-based injection (
ClassLoadingStrategy.UsingLookup) — used when reflective injection is unavailable butClassInjector.UsingLookup.isAvailable()returns true and aMethodHandles.LookupwithPACKAGE/MODULEprivileges over the stage class's package can be obtained viaMethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup). The generated subclass is again injected into the stage class's classloader, preserving package membership. This tier is reached on Java 9+ runtimes that have not openedjava.base/java.langto reflection. IfprivateLookupInfails (typically because the user's JPMS module does not--add-opensits package to JGiven), a singleWARN-level log message is emitted and the strategy falls through toWRAPPER. - Wrapping (
ClassLoadingStrategy.Default.WRAPPER) — fallback when neither injector is available, or when the lookup tier cannot acquire a usableLookup. The generated subclass is placed in a separate child classloader. Package-private stages cannot be instrumented underWRAPPER; the user must either--add-openstheir stage class's package or move the stage class to the unnamed module / classpath.
Subclasses may override getClassLoadingStrategy(Class) to
take a different decision.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Class<? extends T> createStageClass(Class<T> stageClass) protected ClassLoadergetClassLoader(Class<?> stageClass) protected net.bytebuddy.dynamic.loading.ClassLoadingStrategy<ClassLoader> getClassLoadingStrategy(Class<?> stageClass)
-
Field Details
-
INTERCEPTOR_FIELD_NAME
- See Also:
-
SETTER_NAME
- See Also:
-
-
Constructor Details
-
ByteBuddyStageClassCreator
public ByteBuddyStageClassCreator()
-
-
Method Details
-
createStageClass
- Specified by:
createStageClassin interfaceStageClassCreator
-
getClassLoadingStrategy
protected net.bytebuddy.dynamic.loading.ClassLoadingStrategy<ClassLoader> getClassLoadingStrategy(Class<?> stageClass) -
getClassLoader
-