Class MethodInvoker

java.lang.Object
org.miaixz.bus.core.lang.reflect.method.MethodInvoker
All Implemented Interfaces:
Invoker

public class MethodInvoker extends Object implements Invoker
方法调用器,通过反射调用方法。
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • MethodInvoker

      public MethodInvoker(Method method)
      构造
      Parameters:
      method - 方法
  • Method Details

    • of

      public static MethodInvoker of(Method method)
      创建方法调用器
      Parameters:
      method - 方法
      Returns:
      方法调用器
    • invokeHandle

      public static <T> T invokeHandle(MethodHandle methodHandle, Object... args)
      执行方法句柄,MethodHandle.invokeWithArguments(Object...)包装 非static方法需先调用MethodHandle.bindTo(Object)绑定执行对象。

      需要注意的是,此处没有使用MethodHandle.invoke(Object...),因为其参数第一个必须为对象或类。 MethodHandle.invokeWithArguments(Object...)只需传参数即可。

      Type Parameters:
      T - 返回值类型
      Parameters:
      methodHandle - MethodHandle
      args - 方法参数值,支持子类转换和自动拆装箱
      Returns:
      方法返回值
    • invoke

      public static <T> T invoke(Object obj, Method method, Object... args) throws InternalException
      执行接口或对象中的方法
       interface Duck {
           default String quack() {
               return "Quack";
           }
       }
       
       Duck duck = (Duck) Proxy.newProxyInstance(ClassKit.getClassLoader(), new Class[] { Duck.class },
               MethodInvoker::invoke);
       
      Type Parameters:
      T - 返回结果类型
      Parameters:
      obj - 接口的子对象或代理对象
      method - 方法
      args - 参数,自动根据Method定义类型转换
      Returns:
      结果
      Throws:
      InternalException - 执行异常包装
    • invokeExact

      public static <T> T invokeExact(Object obj, Method method, Object... args) throws InternalException
      执行接口或对象中的方法,参数类型不做转换,必须与方法参数类型完全匹配
       interface Duck {
           default String quack() {
               return "Quack";
           }
       }
       
       Duck duck = (Duck) Proxy.newProxyInstance(MethodInvoker.getClassLoader(), new Class[] { Duck.class },
               MethodInvoker::invoke);
       
      Type Parameters:
      T - 返回结果类型
      Parameters:
      obj - 接口的子对象或代理对象
      method - 方法
      args - 参数
      Returns:
      结果
      Throws:
      InternalException - 执行异常包装
    • setCheckArgs

      public MethodInvoker setCheckArgs(boolean checkArgs)
      设置是否检查参数
       1. 参数个数是否与方法参数个数一致
       2. 如果某个参数为null但是方法这个位置的参数为原始类型,则赋予原始类型默认值
       
      Parameters:
      checkArgs - 是否检查参数
      Returns:
      this
    • invoke

      public <T> T invoke(Object target, Object... args) throws InternalException
      Description copied from interface: Invoker
      调用指定目标对象的方法。
      Specified by:
      invoke in interface Invoker
      Type Parameters:
      T - 返回类型
      Parameters:
      target - 目标对象,调用的方法属于该对象。
      args - 方法调用的参数数组。
      Returns:
      方法的返回值,方法的返回类型可以是任意类型。
      Throws:
      InternalException
    • invokeStatic

      public <T> T invokeStatic(Object... args) throws InternalException
      执行静态方法
      Type Parameters:
      T - 对象类型
      Parameters:
      args - 参数对象
      Returns:
      结果
      Throws:
      InternalException - 多种异常包装
    • getType

      public Class<?> getType()
      Description copied from interface: Invoker
      获取调用方法的返回类型或参数类型。
      Specified by:
      getType in interface Invoker
      Returns:
      调用方法的返回类型,作为Class对象返回。