Class ClassAllocationUtil
java.lang.Object
eu.cloudnetservice.utils.base.ClassAllocationUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final MethodTypeprivate static final MethodTypeprivate static final MethodType -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGets the value of a static field in a class, resetting the field access permissions after obtaining it.makeInstanceFactory(@NonNull Class<?> targetClass) Constructs an instance for the given target class.makeLookupAlloc(MethodHandles.Lookup lookup) Makes an allocator for a no-args constructor based on the given lookup.makeUnsafeAlloc(@NonNull Object theUnsafe) Makes an allocator that uses sun.misc.Unsafe.allocateInstance to obtain a class instance.
-
Field Details
-
NO_ARGS_CONSTRUCTOR_TYPE
-
SUPPLIER_GET_SIGNATURE
-
SUPPLIER_FACTORY_SIGNATURE
-
LOOKUP_ALLOCATOR
-
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.
-