Class MethodHandleAccessorFactory
java.lang.Object
dev.derklaro.reflexion.internal.handles.MethodHandleAccessorFactory
- All Implemented Interfaces:
AccessorFactory
A reflexion accessor factory which uses method handles to wrap methods and fields. DO NOT OVERRIDE THIS
CLASS! IT IS ONLY OPEN FOR INTERNAL USE!
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA method accessor for constructors which is based on method handles.private static final classAn accessor for field get/set access using method handles.private static final classA method accessor based on method handles. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new method handle accessor factory instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate @NotNull MethodHandleconvertFieldToGeneric(@NotNull Field field, boolean staticField, boolean set) Converts the given field to a generic handle which can be invoked using an object and returns an object rather than requiring exact class instances.private @NotNull MethodHandleconvertToGeneric(@NotNull MethodHandle handle, boolean staticMethod, boolean ctor) Converts the given method handle to a generic handle which can be invoked with an array of objects and returns an object rather than requiring exact class instances.protected @Nullable MethodHandles.LookupGets the IMPL_LOOKUP field instance if possible.booleanChecks if this factory is available.@NotNull MethodAccessor<Constructor<?>> wrapConstructor(@NotNull Reflexion rfx, @NotNull Constructor<?> ctr) Wraps the given java.lang.reflect constructor into a reflexion MethodAccessor wrapper.@NotNull FieldAccessorWraps the given java.lang.reflect field into a reflexion FieldAccessor wrapper.@NotNull MethodAccessor<Method> wrapMethod(@NotNull Reflexion reflexion, @NotNull Method method) Wraps the given java.lang.reflect method into a reflexion MethodAccessor wrapper.
-
Field Details
-
trustedLookup
-
-
Constructor Details
-
MethodHandleAccessorFactory
public MethodHandleAccessorFactory()Constructs a new method handle accessor factory instance.
-
-
Method Details
-
isAvailable
public boolean isAvailable()Checks if this factory is available. This method should never throw an exception but can decide the return value based on one.- Specified by:
isAvailablein interfaceAccessorFactory- Returns:
- true if this factory is available, false otherwise.
-
wrapField
@NotNull public @NotNull FieldAccessor wrapField(@NotNull @NotNull Reflexion reflexion, @NotNull @NotNull Field field) Wraps the given java.lang.reflect field into a reflexion FieldAccessor wrapper.- Specified by:
wrapFieldin interfaceAccessorFactory- Parameters:
reflexion- the base instance which tries to convert the field.field- the field to wrap.- Returns:
- a wrapping accessor for the given field.
-
wrapMethod
@NotNull public @NotNull MethodAccessor<Method> wrapMethod(@NotNull @NotNull Reflexion reflexion, @NotNull @NotNull Method method) Wraps the given java.lang.reflect method into a reflexion MethodAccessor wrapper.- Specified by:
wrapMethodin interfaceAccessorFactory- Parameters:
reflexion- the base instance which tries to convert the method.method- the method to wrap.- Returns:
- a wrapping accessor for the given method.
-
wrapConstructor
@NotNull public @NotNull MethodAccessor<Constructor<?>> wrapConstructor(@NotNull @NotNull Reflexion rfx, @NotNull @NotNull Constructor<?> ctr) Wraps the given java.lang.reflect constructor into a reflexion MethodAccessor wrapper.- Specified by:
wrapConstructorin interfaceAccessorFactory- Parameters:
rfx- the base instance which tries to convert the constructor.ctr- the constructor to wrap.- Returns:
- a wrapping accessor for the given constructor.
-
getTrustedLookup
Gets the IMPL_LOOKUP field instance if possible. This method does not throw an exception but returns null if the field is not accessible for some reason.- Returns:
- the IMPL_LOOKUP field instance, null if the lookup is not possible.
-
convertToGeneric
@NotNull private @NotNull MethodHandle convertToGeneric(@NotNull @NotNull MethodHandle handle, boolean staticMethod, boolean ctor) Converts the given method handle to a generic handle which can be invoked with an array of objects and returns an object rather than requiring exact class instances.- Parameters:
handle- the handle to generify.staticMethod- if the method wrapped by the given method handle is static.ctor- if the method wrapped by the given method handle is a constructor.- Returns:
- a generified version of the given method handle.
- Throws:
NullPointerException- if the given method handle is null.
-
convertFieldToGeneric
@NotNull private @NotNull MethodHandle convertFieldToGeneric(@NotNull @NotNull Field field, boolean staticField, boolean set) throws Exception Converts the given field to a generic handle which can be invoked using an object and returns an object rather than requiring exact class instances.- Parameters:
field- the field to wrap in a method handle.staticField- if the given field is static.set- if the returned method handle should be a setter for the given field.- Returns:
- a method handle which can be used to either get or set the value of the given field.
- Throws:
Exception- if any exception occurs during the field unreflection.NullPointerException- if the given field is null.
-