Package io.microsphere.invoke
Class MethodHandleUtils
- java.lang.Object
-
- io.microsphere.invoke.MethodHandleUtils
-
- All Implemented Interfaces:
Utils
public abstract class MethodHandleUtils extends java.lang.Object implements Utils
Utility class for working withMethodHandle.This class provides helper methods to simplify the usage of the
MethodHandlesAPI, including creating lookups, finding methods, and handling invocation failures.Example Usage
// Example 1: Find a virtual method handle MethodHandle mh = MethodHandleUtils.findVirtual(String.class, "length", int.class); int length = (int) mh.invokeExact("Hello"); System.out.println(length); // Output: 5 // Example 2: Find a static method handle MethodHandle mh = MethodHandleUtils.findStatic(Math.class, "abs", double.class); double abs = (double) mh.invokeExact(-42.0); System.out.println(abs); // Output: 42.0 // Example 3: Handle invokeExact failure try { MethodHandle mh = MethodHandleUtils.findVirtual(Object.class, "nonExistentMethod", void.class); mh.invokeExact(); } catch (Throwable e) { MethodHandleUtils.handleInvokeExactFailure(e, mh); }- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
MethodHandles,MethodHandle
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMethodHandleUtils.LookupModeThe allowedMethodHandles.Lookupmodes enumeration
-
Field Summary
Fields Modifier and Type Field Description static intALL_MODESA single-bit mask representing all accesses (public, private, protected and package) The value, 0x0f, happens to be the same as the value of the modifier bit.static intMODULEA single-bit mask representingmoduleaccess, which may contribute to the result oflookupModes.static intORIGINALA single-bit mask representingoriginalaccess which may contribute to the result oflookupModes.static java.lang.invoke.MethodHandles.LookupPUBLIC_LOOKUPTheMethodHandles.LookupforMethodHandles.publicLookup()static intUNCONDITIONALA single-bit mask representingunconditionalaccess which may contribute to the result oflookupModes.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static java.lang.invoke.MethodHandlefind(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class[] parameterTypes, ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,java.lang.invoke.MethodType,java.lang.invoke.MethodHandle> function)static java.lang.invoke.MethodHandlefindStatic(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)The convenient method to findMethodHandles.Lookup.findStatic(Class, String, MethodType)static java.lang.invoke.MethodHandlefindVirtual(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)The convenient method to findMethodHandles.Lookup.findVirtual(Class, String, MethodType)static voidhandleInvokeExactFailure(java.lang.Throwable e, java.lang.invoke.MethodHandle methodHandle, java.lang.Object... args)handle the failure ofMethodHandle.invokeExact(Object...)static java.lang.invoke.MethodHandles.Lookuplookup(java.lang.Class<?> requestedClass)Create an instance ofMethodHandles.Lookupby the specified lookup class withall accesses (public, private, protected and package)static java.lang.invoke.MethodHandles.Lookuplookup(java.lang.Class<?> requestedClass, MethodHandleUtils.LookupMode... lookupModes)Create an instance ofMethodHandles.Lookupby the specified lookup class withall access (public, private, protected and package)
-
-
-
Field Detail
-
MODULE
public static final int MODULE
A single-bit mask representingmoduleaccess, which may contribute to the result oflookupModes. The value is0x10, which does not correspond meaningfully to any particular modifier bit. In conjunction with thePUBLICmodifier bit, aLookupwith this lookup mode can access all public types in the module of the lookup class and public types in packages exported by other modules to the module of the class to be looked up.If this lookup mode is set, the previous lookup class is always
null.- Since:
- 9
- See Also:
MethodHandles.Lookup#MODULE, Constant Field Values
-
UNCONDITIONAL
public static final int UNCONDITIONAL
A single-bit mask representingunconditionalaccess which may contribute to the result oflookupModes. The value is0x20, which does not correspond meaningfully to any particular modifier bit. ALookupwith this lookup mode assumes readability. This lookup mode can access all public members of public types of all modules when the type is in a package that isexported unconditionally.If this lookup mode is set, the previous lookup class is always
null.- Since:
- 9
- See Also:
MethodHandles.publicLookup(),MethodHandles.Lookup#UNCONDITIONAL, Constant Field Values
-
ORIGINAL
public static final int ORIGINAL
A single-bit mask representingoriginalaccess which may contribute to the result oflookupModes. The value is0x40, which does not correspond meaningfully to any particular modifier bit.If this lookup mode is set, the
Lookupobject must be created by the original lookup class by callingMethodHandles.lookup()method or by a bootstrap method invoked by the VM. TheLookupobject with this lookup mode has full privilege access.- Since:
- 16
- See Also:
MethodHandles.Lookup#ORIGINAL, Constant Field Values
-
ALL_MODES
public static final int ALL_MODES
A single-bit mask representing all accesses (public, private, protected and package) The value, 0x0f, happens to be the same as the value of the modifier bit.- See Also:
- Constant Field Values
-
PUBLIC_LOOKUP
public static final java.lang.invoke.MethodHandles.Lookup PUBLIC_LOOKUP
TheMethodHandles.LookupforMethodHandles.publicLookup()
-
-
Method Detail
-
lookup
public static java.lang.invoke.MethodHandles.Lookup lookup(java.lang.Class<?> requestedClass)
Create an instance ofMethodHandles.Lookupby the specified lookup class withall accesses (public, private, protected and package)- Parameters:
requestedClass- the class to be looked up- Returns:
- non-null
-
lookup
public static java.lang.invoke.MethodHandles.Lookup lookup(java.lang.Class<?> requestedClass, MethodHandleUtils.LookupMode... lookupModes)Create an instance ofMethodHandles.Lookupby the specified lookup class withall access (public, private, protected and package)- Parameters:
requestedClass- the class to be looked up- Returns:
- non-null
-
findVirtual
public static java.lang.invoke.MethodHandle findVirtual(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)The convenient method to findMethodHandles.Lookup.findVirtual(Class, String, MethodType)- Parameters:
requestedClass- the class to be looked upmethodName- the target method nameparameterTypes- the types of target method parameters- Returns:
MethodHandle
-
findStatic
public static java.lang.invoke.MethodHandle findStatic(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class... parameterTypes)The convenient method to findMethodHandles.Lookup.findStatic(Class, String, MethodType)- Parameters:
requestedClass- the class to be looked upmethodName- the target method nameparameterTypes- the types of target method parameters- Returns:
MethodHandle
-
handleInvokeExactFailure
public static void handleInvokeExactFailure(java.lang.Throwable e, java.lang.invoke.MethodHandle methodHandle, java.lang.Object... args)handle the failure ofMethodHandle.invokeExact(Object...)- Parameters:
e-ThrowablemethodHandle-MethodHandleargs- the arguments ofMethodHandle.invokeExact(Object...)
-
find
protected static java.lang.invoke.MethodHandle find(java.lang.Class<?> requestedClass, java.lang.String methodName, java.lang.Class[] parameterTypes, ThrowableBiFunction<java.lang.invoke.MethodHandles.Lookup,java.lang.invoke.MethodType,java.lang.invoke.MethodHandle> function)
-
-