public class LookupUtil extends Object
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
| Constructor and Description |
|---|
LookupUtil() |
| Modifier and Type | Method and Description |
|---|---|
static MethodHandle |
findConstructor(Class<?> callerClass,
Class<?>... argTypes)
查找指定的构造方法
|
static MethodHandle |
findConstructor(Class<?> callerClass,
MethodType type)
查找指定的构造方法
|
static MethodHandle |
findConstructorExact(Class<?> callerClass,
Class<?>... argTypes)
查找指定的构造方法,给定的参数类型必须完全匹配,不能有拆装箱或继承关系等/
|
static MethodHandle |
findMethod(Class<?> callerClass,
String name,
Class<?> returnType,
Class<?>... argTypes)
查找指定方法的方法句柄
此方法只会查找: 当前类的方法(包括构造方法和private方法) 父类的方法(包括构造方法和private方法) 当前类的static方法 |
static MethodHandle |
findMethod(Class<?> callerClass,
String name,
MethodType type)
查找指定方法的方法句柄
此方法只会查找: 当前类的方法(包括构造方法和private方法) 父类的方法(包括构造方法和private方法) 当前类的static方法 |
static MethodHandles.Lookup |
lookup()
jdk8中如果直接调用
MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial
时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。 |
static MethodHandles.Lookup |
lookup(Class<?> callerClass)
jdk8中如果直接调用
MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial
时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。 |
static MethodHandle |
unreflect(Member methodOrConstructor)
|
static MethodHandle |
unreflectMethod(Method method)
将
Method 转换为方法句柄MethodHandle |
public static MethodHandles.Lookup lookup()
MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial
时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。MethodHandles.Lookuppublic static MethodHandles.Lookup lookup(Class<?> callerClass)
MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial
时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。callerClass - 被调用的类或接口MethodHandles.Lookuppublic static MethodHandle unreflect(Member methodOrConstructor) throws HutoolException
methodOrConstructor - Method或者ConstructorMethodHandleHutoolException - IllegalAccessException 包装public static MethodHandle unreflectMethod(Method method) throws IllegalAccessException
Method 转换为方法句柄MethodHandlemethod - MethodMethodHandlesIllegalAccessException - 无权访问public static MethodHandle findMethod(Class<?> callerClass, String name, Class<?> returnType, Class<?>... argTypes)
callerClass - 方法所在类或接口name - 方法名称,null或者空则查找构造方法returnType - 返回值类型argTypes - 返回类型和参数类型列表MethodHandle,null表示未找到方法public static MethodHandle findMethod(Class<?> callerClass, String name, MethodType type)
callerClass - 方法所在类或接口name - 方法名称,null或者空则查找构造方法type - 返回类型和参数类型,可以使用MethodType#methodType构建MethodHandle,null表示未找到方法public static MethodHandle findConstructor(Class<?> callerClass, Class<?>... argTypes)
callerClass - 类argTypes - 参数类型列表public static MethodHandle findConstructorExact(Class<?> callerClass, Class<?>... argTypes)
callerClass - 类argTypes - 参数类型列表,完全匹配public static MethodHandle findConstructor(Class<?> callerClass, MethodType type)
callerClass - 类type - 参数类型,此处返回类型应为void.classCopyright © 2025. All rights reserved.