Package io.github.connellite.reflection
Class MethodHandleReflectionUtil
java.lang.Object
io.github.connellite.reflection.MethodHandleReflectionUtil
Field access, method invocation, and construction through cached
MethodHandle
and VarHandle instances. Resolve a handle once (methodHandle(Method),
varHandle(Field), constructorHandle(Class, Class...),
resolveRecordConstructorHandle(Class), etc.), then pass it as the first argument
to invoke(MethodHandle, Object...), get(VarHandle, Object),
set(VarHandle, Object, Object), or getInstance(MethodHandle, Object...).
For MethodHandle invocation:
invoke(MethodHandle, Object...)— coercion path viaMethodHandle.invoke(java.lang.Object...), similar to classic reflection.asType(MethodHandle, MethodType)thenMethodHandle.invokeExact(java.lang.Object...)at the call site — strict signatures and primitive returns (with a cast). A utility wrapper cannot preserveinvokeExacttypes throughObjectvarargs.
Classic reflection helpers (ReflectionUtil.getMethodByName(java.lang.Class<?>, java.lang.String), ReflectionUtil.lookupClass(),
enum maps, generic introspection, etc.) remain on ReflectionUtil.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MethodHandleasType(MethodHandle handle, MethodType type) static <T> MethodHandleconstructorHandle(Class<T> type) No-argument declared constructor as aMethodHandle.static <T> MethodHandleconstructorHandle(Class<T> type, Class<?>... parameterTypes) Declared constructor with the given parameter types as aMethodHandle.static MethodHandleconstructorHandle(Constructor<?> constructor) Unreflects a declared constructor into aMethodHandle.static ObjectReads a static field through aVarHandle.static ObjectReads an instance field through aVarHandle.static <T> TReads a field and casts it totype.static <T> TgetInstance(MethodHandle constructorHandle) Invokes a no-arg constructorMethodHandle.static <T> TgetInstance(MethodHandle constructorHandle, Object... initargs) Invokes a constructorMethodHandlewithMethodHandle.invoke(Object...).static Objectinvoke(MethodHandle handle, Object... args) Invokes withMethodHandle.invoke(Object...)— arguments may be converted (boxing, widening, reference casts).static MethodHandlemethodHandle(Class<?> clazz, String methodName) Resolves a declared no-arg method by name and unreflects it.static MethodHandlemethodHandle(Method method) Unreflects a declared method into aMethodHandle.static <T> MethodHandleresolveRecordConstructorHandle(Class<T> recordClass) Canonical record constructor as aMethodHandle.static voidWrites a static field through aVarHandle.static voidWrites an instance field through aVarHandle.static voidWrites a field value and rejectsfinalfields usingfieldmetadata.static VarHandleResolves a declared field by name and unreflects it.static VarHandleUnreflects a declared field into aVarHandle.
-
Constructor Details
-
MethodHandleReflectionUtil
public MethodHandleReflectionUtil()
-
-
Method Details
-
methodHandle
Unreflects a declared method into aMethodHandle.- Throws:
IllegalAccessException
-
methodHandle
public static MethodHandle methodHandle(Class<?> clazz, String methodName) throws NoSuchMethodException, IllegalAccessException Resolves a declared no-arg method by name and unreflects it. -
varHandle
Unreflects a declared field into aVarHandle.- Throws:
IllegalAccessException
-
varHandle
public static VarHandle varHandle(Class<?> clazz, String fieldName) throws NoSuchFieldException, IllegalAccessException Resolves a declared field by name and unreflects it. -
constructorHandle
public static <T> MethodHandle constructorHandle(Class<T> type) throws NoSuchMethodException, IllegalAccessException No-argument declared constructor as aMethodHandle. -
constructorHandle
public static <T> MethodHandle constructorHandle(Class<T> type, Class<?>... parameterTypes) throws NoSuchMethodException, IllegalAccessException Declared constructor with the given parameter types as aMethodHandle. -
constructorHandle
public static MethodHandle constructorHandle(Constructor<?> constructor) throws IllegalAccessException Unreflects a declared constructor into aMethodHandle.- Throws:
IllegalAccessException
-
resolveRecordConstructorHandle
public static <T> MethodHandle resolveRecordConstructorHandle(Class<T> recordClass) throws NoSuchMethodException, IllegalAccessException Canonical record constructor as aMethodHandle. -
asType
-
invoke
public static Object invoke(MethodHandle handle, Object... args) throws InvocationTargetException, IllegalAccessException Invokes withMethodHandle.invoke(Object...)— arguments may be converted (boxing, widening, reference casts). Convenience path; not signature-strict.For
MethodHandle.invokeExact(java.lang.Object...), adapt withasType(MethodHandle, MethodType)and invoke on the handle at the use site. -
get
Reads a static field through aVarHandle. -
get
Reads an instance field through aVarHandle. -
get
Reads a field and casts it totype.- Parameters:
receiver- instance for an instance field, ornullfor a static field
-
set
Writes a static field through aVarHandle. -
set
Writes an instance field through aVarHandle. -
set
Writes a field value and rejectsfinalfields usingfieldmetadata.- Parameters:
receiver- instance for an instance field, ornullfor a static field- Throws:
IllegalStateException- iffieldisfinal
-
getInstance
public static <T> T getInstance(MethodHandle constructorHandle) throws InstantiationException, IllegalAccessException, InvocationTargetException Invokes a no-arg constructorMethodHandle. -
getInstance
public static <T> T getInstance(MethodHandle constructorHandle, Object... initargs) throws InstantiationException, IllegalAccessException, InvocationTargetException Invokes a constructorMethodHandlewithMethodHandle.invoke(Object...).
-