Package org.stianloader.mrjmania
Class MRJReflection
java.lang.Object
org.stianloader.mrjmania.MRJReflection
Polyfills for reflection-related workloads.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanAccess(@NotNull AccessibleObject subject, @Nullable Object accessedInstance) Invoke AccessibleObject#canAccess if available (J9+), or use the more impreciseAccessibleObject.isAccessible()that can be error-prone in a post-JPMS world.static @NotNull MethodHandledropReturn(@NotNull MethodHandle handle) Returns aMethodHandle, that has the same method signature and behaviour as the inputMethodHandle, but returnsvoidand discards the result value.static final booleantrySetAccessible(@NotNull AccessibleObject subject) Attempt to make the givenAccessibleObject(constructor/method/field) accessible.
-
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 impreciseAccessibleObject.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. Otherwisenull.- Returns:
- true if the
subjectcan be accessed if usingaccessedInstanceas the implementing instance of the member. - Throws:
IllegalStateException- If the nullability constraints foraccessedInstancewere violated.
-
dropReturn
@NotNull @Contract(pure=true) public static @NotNull MethodHandle dropReturn(@NotNull @NotNull MethodHandle handle) Returns aMethodHandle, that has the same method signature and behaviour as the inputMethodHandle, but returnsvoidand 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- TheMethodHandlethat has a return value to drop.- Returns:
- The
MethodHandlewithout a return value.
-
trySetAccessible
Attempt to make the givenAccessibleObject(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- TheAccessibleObjectthat is the subject of this operation.- Returns:
trueif succeeded, false otherwise.
-