类 Methods
java.lang.Object
com.ajaxjs.util.reflect.Methods
方法相关的反射
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static ObjectexecuteDefault(Object proxy, Method method, Object[] args) 通过反射调用接口的默认方法。static ObjectexecuteMethod(Object instance, Method method, Object... args) 调用方法,该方法不会抛出异常static ObjectexecuteMethod(Object instance, String method, Class<?> argType, Object argValue) 调用方法。static ObjectexecuteMethod(Object instance, String method, Object... args) 调用方法static ObjectexecuteMethod_Throwable(Object instance, Method method, Object... args) 调用方法static ObjectexecuteStaticMethod(Method method, Object... args) 执行静态方法static Class<?>[]getAllSuperClazz(Class<?> clz) static MethodgetDeclaredMethod(Class<?> clz, String methodName) 根据类和方法名获取该类声明的方法static MethodgetDeclaredMethodByInterface(Class<?> clz, String method, Object arg) 循环 object 向上转型(接口)static Method根据类、方法的字符串和参数列表获取方法对象,支持重载的方法static Method根据方法名称和参数列表查找方法。static MethodgetMethodByUpCastingSearch(Class<?> clz, String method, Object arg) 根据方法名称和参数列表查找方法。static MethodgetSuperClassDeclaredMethod(Class<?> clz, String method) 查找对象父类身上指定的方法(注意该方法不需要校验参数类型是否匹配,故有可能不是目标方法,而造成异常,请谨慎使用)static MethodgetSuperClassDeclaredMethod(Class<?> clz, String method, Class<?> argClz) 查找对象父类身上指定的方法static Throwable获取实际抛出的那个异常对象。static String获取实际抛出的那个异常对象,并去掉前面的包名。static booleanisStaticMethod(Method method) 是否静态方法
-
构造器详细资料
-
Methods
public Methods()
-
-
方法详细资料
-
getDeclaredMethod
根据类和方法名获取该类声明的方法- 参数:
clz- 要查找方法的类methodName- 方法名- 返回:
- 声明的方法,如果方法不存在则返回 null
-
getMethod
根据类、方法的字符串和参数列表获取方法对象,支持重载的方法- 参数:
obj- 可以是实例对象,也可以是类对象method- 方法名称args- 明确的参数类型列表- 返回:
- 匹配的方法对象,null 表示找不到
-
getMethod
根据方法名称和参数列表查找方法。注意参数对象类型由于没有向上转型会造成不匹配而找不到方法,这时应使用上一个方法或 getMethodByUpCastingSearch()- 参数:
obj- 实例对象method- 方法名称args- 对应重载方法的参数列表- 返回:
- 匹配的方法对象,null 表示找不到
-
getMethodByUpCastingSearch
根据方法名称和参数列表查找方法。自动循环参数类型向上转型。仅支持一个参数。- 参数:
clz- 实例对象的类对象method- 方法名称arg- 参数对象,可能是子类或接口,所以要在这里找到对应的方法,当前只支持单个参数;且不能传 Class,必须为对象- 返回:
- 匹配的方法对象,null 表示找不到
-
getDeclaredMethodByInterface
循环 object 向上转型(接口)- 参数:
clz- 主类method- 方法名称arg- 参数对象,可能是子类或接口,所以要在这里找到对应的方法,当前只支持单个参数- 返回:
- 方法对象
-
getSuperClassDeclaredMethod
查找对象父类身上指定的方法- 参数:
clz- 主类method- 方法名称argClz- 参数类引用- 返回:
- 匹配的方法对象,null 表示找不到
-
getSuperClassDeclaredMethod
查找对象父类身上指定的方法(注意该方法不需要校验参数类型是否匹配,故有可能不是目标方法,而造成异常,请谨慎使用)- 参数:
clz- 主类method- 方法名称- 返回:
- 匹配的方法对象,null 表示找不到
-
getAllSuperClazz
-
executeMethod_Throwable
public static Object executeMethod_Throwable(Object instance, Method method, Object... args) throws Throwable 调用方法- 参数:
instance- 对象实例,beanmethod- 方法对象args- 参数列表- 返回:
- 执行结果
- 抛出:
Throwable- 任何异常
-
getUnderLayerErr
获取实际抛出的那个异常对象。 InvocationTargetException 太过于宽泛,在 trouble shouting的时候,不能给人非常直观的信息 AOP 的缘故,不能直接捕获原来的异常,要不断 e.getCause()....- 参数:
e- 异常对象- 返回:
- 实际异常对象
-
getUnderLayerErrMsg
获取实际抛出的那个异常对象,并去掉前面的包名。- 参数:
e- 异常对象- 返回:
- 实际异常对象信息
-
executeMethod
调用方法,该方法不会抛出异常- 参数:
instance- 对象实例,beanmethod- 方法对象args- 参数列表- 返回:
- 执行结果
-
executeMethod
调用方法- 参数:
instance- 对象实例,beanmethod- 方法对象名称args- 参数列表- 返回:
- 执行结果
-
executeMethod
public static Object executeMethod(Object instance, String method, Class<?> argType, Object argValue) 调用方法。 注意获取方法对象,原始类型和包装类型不能混用,否则得不到正确的方法, 例如 Integer 不能与 int 混用。 这里提供一个 argType 的参数,指明参数类型为何。- 参数:
instance- 对象实例method- 方法名称argType- 参数类型argValue- 参数值- 返回:
- 执行结果
-
executeStaticMethod
执行静态方法- 参数:
method- 方法对象args- 方法参数列表- 返回:
- 执行结果
-
isStaticMethod
是否静态方法- 参数:
method- 方法对象- 返回:
- true 表示为静态方法
-
executeDefault
通过反射调用接口的默认方法。此方法旨在提供一种通过反射机制调用Java 8及以上版本接口中默认方法的手段。 它绕过了直接调用默认方法需要实例化一个类的限制,通过MethodHandles和反射机制实现。
- 参数:
proxy- 接口的代理实例,用于调用默认方法。method- 要调用的默认方法的Method对象。args- 调用方法时所需的参数数组。- 返回:
- 调用默认方法后的返回值。
- 抛出:
RuntimeException- 如果在调用过程中发生任何异常,将会抛出运行时异常。
-