Class MethodHandleAccessorFactory

java.lang.Object
dev.derklaro.reflexion.internal.handles.MethodHandleAccessorFactory
All Implemented Interfaces:
AccessorFactory

public class MethodHandleAccessorFactory extends Object implements 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
  • Field Details

  • 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:
      isAvailable in interface AccessorFactory
      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:
      wrapField in interface AccessorFactory
      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:
      wrapMethod in interface AccessorFactory
      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:
      wrapConstructor in interface AccessorFactory
      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

      @Nullable protected @Nullable MethodHandles.Lookup 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.