类 Methods

java.lang.Object
com.ajaxjs.util.reflect.Methods

public class Methods extends Object
方法相关的反射
  • 构造器详细资料

    • Methods

      public Methods()
  • 方法详细资料

    • getDeclaredMethod

      public static Method getDeclaredMethod(Class<?> clz, String methodName)
      根据类和方法名获取该类声明的方法
      参数:
      clz - 要查找方法的类
      methodName - 方法名
      返回:
      声明的方法,如果方法不存在则返回 null
    • getMethod

      public static Method getMethod(Object obj, String method, Class<?>... args)
      根据类、方法的字符串和参数列表获取方法对象,支持重载的方法
      参数:
      obj - 可以是实例对象,也可以是类对象
      method - 方法名称
      args - 明确的参数类型列表
      返回:
      匹配的方法对象,null 表示找不到
    • getMethod

      public static Method getMethod(Object obj, String method, Object... args)
      根据方法名称和参数列表查找方法。注意参数对象类型由于没有向上转型会造成不匹配而找不到方法,这时应使用上一个方法或 getMethodByUpCastingSearch()
      参数:
      obj - 实例对象
      method - 方法名称
      args - 对应重载方法的参数列表
      返回:
      匹配的方法对象,null 表示找不到
    • getMethodByUpCastingSearch

      public static Method getMethodByUpCastingSearch(Class<?> clz, String method, Object arg)
      根据方法名称和参数列表查找方法。自动循环参数类型向上转型。仅支持一个参数。
      参数:
      clz - 实例对象的类对象
      method - 方法名称
      arg - 参数对象,可能是子类或接口,所以要在这里找到对应的方法,当前只支持单个参数;且不能传 Class,必须为对象
      返回:
      匹配的方法对象,null 表示找不到
    • getDeclaredMethodByInterface

      public static Method getDeclaredMethodByInterface(Class<?> clz, String method, Object arg)
      循环 object 向上转型(接口)
      参数:
      clz - 主类
      method - 方法名称
      arg - 参数对象,可能是子类或接口,所以要在这里找到对应的方法,当前只支持单个参数
      返回:
      方法对象
    • getSuperClassDeclaredMethod

      public static Method getSuperClassDeclaredMethod(Class<?> clz, String method, Class<?> argClz)
      查找对象父类身上指定的方法
      参数:
      clz - 主类
      method - 方法名称
      argClz - 参数类引用
      返回:
      匹配的方法对象,null 表示找不到
    • getSuperClassDeclaredMethod

      public static Method getSuperClassDeclaredMethod(Class<?> clz, String method)
      查找对象父类身上指定的方法(注意该方法不需要校验参数类型是否匹配,故有可能不是目标方法,而造成异常,请谨慎使用)
      参数:
      clz - 主类
      method - 方法名称
      返回:
      匹配的方法对象,null 表示找不到
    • getAllSuperClazz

      public static Class<?>[] getAllSuperClazz(Class<?> clz)
    • executeMethod_Throwable

      public static Object executeMethod_Throwable(Object instance, Method method, Object... args) throws Throwable
      调用方法
      参数:
      instance - 对象实例,bean
      method - 方法对象
      args - 参数列表
      返回:
      执行结果
      抛出:
      Throwable - 任何异常
    • getUnderLayerErr

      public static Throwable getUnderLayerErr(Throwable e)
      获取实际抛出的那个异常对象。 InvocationTargetException 太过于宽泛,在 trouble shouting的时候,不能给人非常直观的信息 AOP 的缘故,不能直接捕获原来的异常,要不断 e.getCause()....
      参数:
      e - 异常对象
      返回:
      实际异常对象
    • getUnderLayerErrMsg

      public static String getUnderLayerErrMsg(Throwable e)
      获取实际抛出的那个异常对象,并去掉前面的包名。
      参数:
      e - 异常对象
      返回:
      实际异常对象信息
    • executeMethod

      public static Object executeMethod(Object instance, Method method, Object... args)
      调用方法,该方法不会抛出异常
      参数:
      instance - 对象实例,bean
      method - 方法对象
      args - 参数列表
      返回:
      执行结果
    • executeMethod

      public static Object executeMethod(Object instance, String method, Object... args)
      调用方法
      参数:
      instance - 对象实例,bean
      method - 方法对象名称
      args - 参数列表
      返回:
      执行结果
    • executeMethod

      public static Object executeMethod(Object instance, String method, Class<?> argType, Object argValue)
      调用方法。 注意获取方法对象,原始类型和包装类型不能混用,否则得不到正确的方法, 例如 Integer 不能与 int 混用。 这里提供一个 argType 的参数,指明参数类型为何。
      参数:
      instance - 对象实例
      method - 方法名称
      argType - 参数类型
      argValue - 参数值
      返回:
      执行结果
    • executeStaticMethod

      public static Object executeStaticMethod(Method method, Object... args)
      执行静态方法
      参数:
      method - 方法对象
      args - 方法参数列表
      返回:
      执行结果
    • isStaticMethod

      public static boolean isStaticMethod(Method method)
      是否静态方法
      参数:
      method - 方法对象
      返回:
      true 表示为静态方法
    • executeDefault

      public static Object executeDefault(Object proxy, Method method, Object[] args)
      通过反射调用接口的默认方法。

      此方法旨在提供一种通过反射机制调用Java 8及以上版本接口中默认方法的手段。 它绕过了直接调用默认方法需要实例化一个类的限制,通过MethodHandles和反射机制实现。

      调用 Interface 的 default 方法 ... ...

      参数:
      proxy - 接口的代理实例,用于调用默认方法。
      method - 要调用的默认方法的Method对象。
      args - 调用方法时所需的参数数组。
      返回:
      调用默认方法后的返回值。
      抛出:
      RuntimeException - 如果在调用过程中发生任何异常,将会抛出运行时异常。