Class MRJReflection

java.lang.Object
org.stianloader.mrjmania.MRJReflection

public class MRJReflection extends Object
Polyfills for reflection-related workloads.
  • Method Details

    • canAccess

      @Contract(pure=true) public static boolean canAccess(@NotNull @NotNull AccessibleObject subject, @Nullable @Nullable Object accessedInstance)
      Invoke AccessibleObject#canAccess if available (J9+), or use the more imprecise AccessibleObject.isAccessible() that can be error-prone in a post-JPMS world.
      Parameters:
      subject - The field/method/constructor that is the subject of this operation.
      accessedInstance - The instance for non-static fields and methods. Otherwise null.
      Returns:
      true if the subject can be accessed if using accessedInstance as the implementing instance of the member.
      Throws:
      IllegalStateException - If the nullability constraints for accessedInstance were violated.
    • dropReturn

      @NotNull @Contract(pure=true) public static @NotNull MethodHandle dropReturn(@NotNull @NotNull MethodHandle handle)
      Returns a MethodHandle, that has the same method signature and behaviour as the input MethodHandle, but returns void and discards the result value.

      This method uses a polyfill for Java versions ≤ 15 that seems to work but might not be guaranteed to do so as even I don't quite understand it. If this method doesn't work as advertised, then that might be the cause.

      Parameters:
      handle - The MethodHandle that has a return value to drop.
      Returns:
      The MethodHandle without a return value.
    • trySetAccessible

      public static final boolean trySetAccessible(@NotNull @NotNull AccessibleObject subject)
      Attempt to make the given AccessibleObject (constructor/method/field) accessible.

      This method will swallow any somewhat expected exceptions, which might not always be desirable. Just use your debugger though.

      Parameters:
      subject - The AccessibleObject that is the subject of this operation.
      Returns:
      true if succeeded, false otherwise.