Package org.miaixz.bus.core.xyz
Class LookupKit
java.lang.Object
org.miaixz.bus.core.xyz.LookupKit
MethodHandles.Lookup工具
MethodHandles.Lookup是一个方法句柄查找对象,用于在指定类中查找符合给定方法名称、方法类型的方法句柄。
jdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial
时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。
参考:https://blog.csdn.net/u013202238/article/details/108687086
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MethodHandlefindConstructor(Class<?> callerClass, Class<?>... argTypes) 查找指定的构造方法static MethodHandlefindConstructor(Class<?> callerClass, MethodType type) 查找指定的构造方法static MethodHandlefindConstructorExact(Class<?> callerClass, Class<?>... argTypes) 查找指定的构造方法,给定的参数类型必须完全匹配,不能有拆装箱或继承关系等/static MethodHandlefindMethod(Class<?> callerClass, String name, Class<?> returnType, Class<?>... argTypes) 查找指定方法的方法句柄 此方法只会查找: 当前类的方法(包括构造方法和private方法) 父类的方法(包括构造方法和private方法) 当前类的static方法static MethodHandlefindMethod(Class<?> callerClass, String name, MethodType type) 查找指定方法的方法句柄 此方法只会查找: 当前类的方法(包括构造方法和private方法) 父类的方法(包括构造方法和private方法) 当前类的static方法static MethodHandles.Lookuplookup()jdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。static MethodHandles.Lookupjdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。static MethodHandlestatic MethodHandleunreflectMethod(Method method) 将Method转换为方法句柄MethodHandle
-
Constructor Details
-
LookupKit
public LookupKit()
-
-
Method Details
-
lookup
jdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。- Returns:
MethodHandles.Lookup
-
lookup
jdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。- Parameters:
callerClass- 被调用的类或接口- Returns:
MethodHandles.Lookup
-
unreflect
- Parameters:
methodOrConstructor-Method或者Constructor- Returns:
- 方法句柄
MethodHandle - Throws:
InternalException-IllegalAccessException包装
-
unreflectMethod
将Method转换为方法句柄MethodHandle- Parameters:
method-Method- Returns:
MethodHandles- Throws:
IllegalAccessException- 无权访问
-
findMethod
public static MethodHandle findMethod(Class<?> callerClass, String name, Class<?> returnType, Class<?>... argTypes) 查找指定方法的方法句柄 此方法只会查找:- 当前类的方法(包括构造方法和private方法)
- 父类的方法(包括构造方法和private方法)
- 当前类的static方法
- Parameters:
callerClass- 方法所在类或接口name- 方法名称,null或者空则查找构造方法returnType- 返回值类型argTypes- 返回类型和参数类型列表- Returns:
- 方法句柄
MethodHandle,null表示未找到方法
-
findMethod
查找指定方法的方法句柄 此方法只会查找:- 当前类的方法(包括构造方法和private方法)
- 父类的方法(包括构造方法和private方法)
- 当前类的static方法
- Parameters:
callerClass- 方法所在类或接口name- 方法名称,null或者空则查找构造方法type- 返回类型和参数类型,可以使用MethodType#methodType构建- Returns:
- 方法句柄
MethodHandle,null表示未找到方法
-
findConstructor
查找指定的构造方法- Parameters:
callerClass- 类argTypes- 参数类型列表- Returns:
- 构造方法句柄
-
findConstructorExact
查找指定的构造方法,给定的参数类型必须完全匹配,不能有拆装箱或继承关系等/- Parameters:
callerClass- 类argTypes- 参数类型列表,完全匹配- Returns:
- 构造方法句柄
-
findConstructor
查找指定的构造方法- Parameters:
callerClass- 类type- 参数类型,此处返回类型应为void.class- Returns:
- 构造方法句柄
-