Class TinyDynamicDI


  • public class TinyDynamicDI
    extends java.lang.Object
    Helper utilities for dynamic dependency injection.
    • Constructor Detail

      • TinyDynamicDI

        public TinyDynamicDI()
    • Method Detail

      • attachRecordedAnnotation

        public static <T> T attachRecordedAnnotation​(@NonNull
                                                     @NonNull java.util.function.Supplier<java.lang.Object> instanceSupplier,
                                                     java.lang.Class<?> instanceClass,
                                                     @NonNull
                                                     @NonNull Recorded recorded)
        Attaches a Recorded instance to non-annotated object by creating a proxy object around it. Useful when dynamically injecting more components into an already initialized TinyDI context.
        Type Parameters:
        T - The type the result to be casted to. It must be an interface. Recorded is a safe choice.
        Parameters:
        instanceSupplier - The instance supplier whose instance will be bound to the annotation.
        instanceClass - The type of the instance.
        recorded - An Recorded instance with the specified values. Usually RecordedAnnotation can be used for the purpose.
        Returns:
        A proxy instance bounding toInstance, it's implemented interfaces along with the passed record one.
        Throws:
        java.lang.IllegalArgumentException - if <T> is not an interface
        java.lang.NullPointerException - if toInstance or record are null
      • realClass

        public static java.lang.Class<?> realClass​(@NonNull
                                                   @NonNull java.lang.Object instance)
        Obtains the class behind a proxy object with TinyDI's TinyDynamicDI.TransparentInvocationHandler.
        Parameters:
        instance - The instance to work with.
        Returns:
        The class behind the proxy object, or the instance's class if the latter is not a proxy.
        Throws:
        java.lang.ClassCastException - if the proxy object's handler is not of TinyDynamicDI.TransparentInvocationHandler type.
      • realInstance

        public static java.lang.Object realInstance​(java.lang.Object instance)
        Obtains the instance behind a proxy object with TinyDI's TinyDynamicDI.TransparentInvocationHandler.
        Parameters:
        instance - The instance to work with.
        Returns:
        The instance behind the proxy object, or the actual instance if the latter is not a proxy.
        Throws:
        java.lang.ClassCastException - if the proxy object's handler is not of TinyDynamicDI.TransparentInvocationHandler type.