Class LookupKit

java.lang.Object
org.miaixz.bus.core.xyz.LookupKit

public class LookupKit 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

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • LookupKit

      public LookupKit()
  • Method Details

    • lookup

      public static MethodHandles.Lookup lookup()
      jdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。
      Returns:
      MethodHandles.Lookup
    • lookup

      public static MethodHandles.Lookup lookup(Class<?> callerClass)
      jdk8中如果直接调用MethodHandles.lookup()获取到的MethodHandles.Lookup在调用findSpecial和unreflectSpecial 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。
      Parameters:
      callerClass - 被调用的类或接口
      Returns:
      MethodHandles.Lookup
    • unreflect

      public static MethodHandle unreflect(Member methodOrConstructor) throws InternalException
      Method或者Constructor 包装为方法句柄MethodHandle
      Parameters:
      methodOrConstructor - Method或者Constructor
      Returns:
      方法句柄MethodHandle
      Throws:
      InternalException - IllegalAccessException 包装
    • unreflectMethod

      public static MethodHandle unreflectMethod(Method method) throws IllegalAccessException
      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:
      方法句柄 MethodHandlenull表示未找到方法
    • findMethod

      public static MethodHandle findMethod(Class<?> callerClass, String name, MethodType type)
      查找指定方法的方法句柄 此方法只会查找:
      • 当前类的方法(包括构造方法和private方法)
      • 父类的方法(包括构造方法和private方法)
      • 当前类的static方法
      Parameters:
      callerClass - 方法所在类或接口
      name - 方法名称,null或者空则查找构造方法
      type - 返回类型和参数类型,可以使用MethodType#methodType构建
      Returns:
      方法句柄 MethodHandlenull表示未找到方法
    • findConstructor

      public static MethodHandle findConstructor(Class<?> callerClass, Class<?>... argTypes)
      查找指定的构造方法
      Parameters:
      callerClass - 类
      argTypes - 参数类型列表
      Returns:
      构造方法句柄
    • findConstructorExact

      public static MethodHandle findConstructorExact(Class<?> callerClass, Class<?>... argTypes)
      查找指定的构造方法,给定的参数类型必须完全匹配,不能有拆装箱或继承关系等/
      Parameters:
      callerClass - 类
      argTypes - 参数类型列表,完全匹配
      Returns:
      构造方法句柄
    • findConstructor

      public static MethodHandle findConstructor(Class<?> callerClass, MethodType type)
      查找指定的构造方法
      Parameters:
      callerClass - 类
      type - 参数类型,此处返回类型应为void.class
      Returns:
      构造方法句柄