Class Nexus


  • public class Nexus
    extends WeakReference<ClassLoader>

    This nexus is a global dispatcher for initializing classes with LoadedTypeInitializers. To do so, this class is to be loaded by the system class loader in an explicit manner. Any instrumented class is then injected a code block into its static type initializer that makes a call to this very same nexus which had the loaded type initializer registered before hand.

    Note: Availability of the Nexus class and its injection into the system class loader can be disabled entirely by setting the PROPERTY system property to false.

    Important: The nexus must never be accessed directly but only by the NexusAccessor which makes sure that the nexus is loaded by the system class loader. Otherwise, a class might not be able to initialize itself if it is loaded by different class loader that does not have the system class loader in its hierarchy.

    • Field Detail

      • PROPERTY

        public static final String PROPERTY
        A system property that allows to disable the use of the Nexus class which is normally injected into the system class loader.
        See Also:
        Constant Field Values
    • Method Detail

      • initialize

        public static void initialize​(Class<?> type,
                                      int identification)
                               throws Exception

        Initializes a loaded type. This method must only be invoked via the system class loader.

        Important: This method must never be called directly but only by using a NexusAccessor.InitializationAppender which enforces to access this class for the system class loader to assure a VM global singleton. This avoids a duplication of the class if this nexus is loaded by different class loaders. For this reason, the last parameter must not use a Byte Buddy specific type as those types can be loaded by different class loaders, too. Any access of the instance is done using Java reflection instead.

        Parameters:
        type - The loaded type to initialize.
        identification - An identification for the initializer to run.
        Throws:
        Exception - If an exception occurs.
      • register

        public static void register​(String name,
                                    ClassLoader classLoader,
                                    ReferenceQueue<? super ClassLoader> referenceQueue,
                                    int identification,
                                    Object typeInitializer)

        Registers a new loaded type initializer.

        Important: This method must never be called directly but only by using a NexusAccessor which enforces to access this class for the system class loader to assure a VM global singleton. This avoids a duplication of the class if this nexus is loaded by different class loaders. For this reason, the last parameter must not use a Byte Buddy specific type as those types can be loaded by different class loaders, too. Any access of the instance is done using Java reflection instead.

        Parameters:
        name - The name of the type for the loaded type initializer.
        classLoader - The class loader of the type for the loaded type initializer.
        referenceQueue - The reference queue to notify upon the class loader's collection which will be enqueued a reference which can be handed to clean(Reference) or null if no reference queue should be notified.
        identification - An identification for the initializer to run.
        typeInitializer - The type initializer to register. The initializer must be an instance of LoadedTypeInitializer where it does however not matter which class loader loaded this latter type.
      • clean

        public static void clean​(Reference<? super ClassLoader> reference)

        Cleans any stale entries from this nexus. Entries are considered stale if their class loader was collected before a class was initialized.

        Important: This method must never be called directly but only by using a NexusAccessor which enforces to access this class for the system class loader to assure a VM global singleton. This avoids a duplication of the class if this nexus is loaded by different class loaders. For this reason, the last parameter must not use a Byte Buddy specific type as those types can be loaded by different class loaders, too. Any access of the instance is done using Java reflection instead.

        Parameters:
        reference - The stale reference to clean.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object