3333import java .util .Set ;
3434import java .util .concurrent .CopyOnWriteArrayList ;
3535import java .util .concurrent .TimeUnit ;
36+ import java .util .function .BooleanSupplier ;
3637import net .bytebuddy .ByteBuddy ;
3738import net .bytebuddy .agent .builder .AgentBuilder ;
38- import net .bytebuddy .description .type .TypeDefinition ;
3939import net .bytebuddy .description .type .TypeDescription ;
4040import net .bytebuddy .dynamic .DynamicType ;
41+ import net .bytebuddy .dynamic .NexusAccessor ;
4142import net .bytebuddy .dynamic .VisibilityBridgeStrategy ;
4243import net .bytebuddy .dynamic .scaffold .InstrumentedType ;
4344import net .bytebuddy .dynamic .scaffold .MethodGraph ;
@@ -54,7 +55,7 @@ public class AgentInstaller {
5455 private static final List <Runnable > MBEAN_SERVER_BUILDER_CALLBACKS = new CopyOnWriteArrayList <>();
5556
5657 static {
57- addByteBuddyRawSetting ();
58+ enableByteBuddyRawTypes ();
5859 disableByteBuddyNexus ();
5960 // register weak map supplier as early as possible
6061 WeakMaps .registerAsSupplier ();
@@ -320,27 +321,39 @@ public static Set<InstrumenterModule.TargetSystem> getEnabledSystems() {
320321 return enabledSystems ;
321322 }
322323
323- private static void addByteBuddyRawSetting () {
324- final String savedPropertyValue = SystemProperties .get (TypeDefinition .RAW_TYPES_PROPERTY );
325- final boolean overridden = SystemProperties .set (TypeDefinition .RAW_TYPES_PROPERTY , "true" );
326- final boolean rawTypes = TypeDescription .AbstractBase .RAW_TYPES ;
327- if (!rawTypes && DEBUG ) {
328- log .debug ("Too late to enable {}" , TypeDefinition .RAW_TYPES_PROPERTY );
324+ private static void enableByteBuddyRawTypes () {
325+ temporaryOverride ("net.bytebuddy.raw" , "true" , AgentInstaller ::rawTypesEnabled );
326+ }
327+
328+ private static boolean rawTypesEnabled () {
329+ return TypeDescription .AbstractBase .RAW_TYPES ; // must avoid touching this before the override
330+ }
331+
332+ private static void disableByteBuddyNexus () {
333+ // disable byte-buddy's Nexus mechanism (we don't need it, and it triggers use of Unsafe)
334+ temporaryOverride ("net.bytebuddy.nexus.disabled" , "true" , AgentInstaller ::nexusDisabled );
335+ }
336+
337+ private static boolean nexusDisabled () {
338+ return !NexusAccessor .isAlive (); // must avoid touching this before the override
339+ }
340+
341+ /** Temporarily overrides a system property while checking it's had the intended side effect. */
342+ private static void temporaryOverride (String key , String value , BooleanSupplier sideEffect ) {
343+ final String savedPropertyValue = SystemProperties .get (key );
344+ final boolean overridden = SystemProperties .set (key , value );
345+ if (!sideEffect .getAsBoolean () && DEBUG ) {
346+ log .debug ("Too late to apply {}={}" , key , value );
329347 }
330348 if (overridden ) {
331349 if (savedPropertyValue == null ) {
332- SystemProperties .clear (TypeDefinition . RAW_TYPES_PROPERTY );
350+ SystemProperties .clear (key );
333351 } else {
334- SystemProperties .set (TypeDefinition . RAW_TYPES_PROPERTY , savedPropertyValue );
352+ SystemProperties .set (key , savedPropertyValue );
335353 }
336354 }
337355 }
338356
339- private static void disableByteBuddyNexus () {
340- // disable byte-buddy's Nexus mechanism (we don't need it, and it triggers use of Unsafe)
341- SystemProperties .set ("net.bytebuddy.nexus.disabled" , "true" );
342- }
343-
344357 private static AgentBuilder .RedefinitionStrategy .Listener redefinitionStrategyListener (
345358 final Set <InstrumenterModule .TargetSystem > enabledSystems ) {
346359 if (enabledSystems .contains (InstrumenterModule .TargetSystem .IAST )) {
0 commit comments