Class ClassAllocationUtil

java.lang.Object
eu.cloudnetservice.utils.base.ClassAllocationUtil

@Internal public final class ClassAllocationUtil extends Object
A collection of utilities to instantiate data classes. For internal use only.
Since:
4.0
  • Field Details

    • NO_ARGS_CONSTRUCTOR_TYPE

      private static final MethodType NO_ARGS_CONSTRUCTOR_TYPE
    • SUPPLIER_GET_SIGNATURE

      private static final MethodType SUPPLIER_GET_SIGNATURE
    • SUPPLIER_FACTORY_SIGNATURE

      private static final MethodType SUPPLIER_FACTORY_SIGNATURE
    • LOOKUP_ALLOCATOR

      private static final Function<Class<?>, Supplier<Object>> LOOKUP_ALLOCATOR
    • UNSAFE_ALLOCATOR

      private static final Function<Class<?>, Supplier<Object>> UNSAFE_ALLOCATOR
  • Constructor Details

    • ClassAllocationUtil

      private ClassAllocationUtil()
  • Method Details

    • getInaccessibleFieldValue

      @NonNull private static @NonNull Object getInaccessibleFieldValue(@NonNull @NonNull Field field) throws IllegalAccessException
      Gets the value of a static field in a class, resetting the field access permissions after obtaining it. This is done to prevent other code with insufficient permission to access a field because this code left the door open.
      Parameters:
      field - the static field to get the value of.
      Returns:
      the value of the given field.
      Throws:
      IllegalAccessException - if the field is inaccessible to the caller.
      NullPointerException - if the given field is null.
    • makeLookupAlloc

      @NonNull private static @NonNull Function<Class<?>, Supplier<Object>> makeLookupAlloc(@NonNull MethodHandles.Lookup lookup)
      Makes an allocator for a no-args constructor based on the given lookup.
      Parameters:
      lookup - the lookup to use to find the no-args constructor in the requested class.
      Returns:
      the allocator factory function which uses no-args constructors resolved by the given lookup.
      Throws:
      NullPointerException - if the given lookup is null.
    • makeUnsafeAlloc

      @NonNull private static @NonNull Function<Class<?>, Supplier<Object>> makeUnsafeAlloc(@NonNull @NonNull Object theUnsafe)
      Makes an allocator that uses sun.misc.Unsafe.allocateInstance to obtain a class instance. This means that no constructors are run when obtaining the class instance, which is fine as this is only used to obtain instances of data classes (whose fields are initialized later).
      Parameters:
      theUnsafe - the singleton instance of sun.misc.Unsafe.
      Returns:
      the allocator factory function which creates new class instances using sun.misc.Unsafe.
      Throws:
      NullPointerException - if the given unsafe instance is null.
    • makeInstanceFactory

      @NonNull public static @NonNull Supplier<Object> makeInstanceFactory(@NonNull @NonNull Class<?> targetClass)
      Constructs an instance for the given target class. The result of this method invocation should be cached by the caller, to prevent many allocations of the same thing.
      Parameters:
      targetClass - the target class to get an instance allocator for.
      Returns:
      an instance factory for the given target class.
      Throws:
      IllegalStateException - if no instance factory can handle the given target class.
      NullPointerException - if the given target class is null.