原文网址:Spring工具类系列--ReflectionUtils的使用_IT利刃出鞘的博客-CSDN博客
简介
本文介绍Spring的ReflectionUtils的使用。
ReflectionUtils工具类的作用:便利地进行反射操作。
Spring还有一个工具类:ReflectUtils,它们在功能上的最大区别是:ReflectUtils可以获取 type类的所有属性描述(此类和父类的所有字段(包括private)),但ReflectionUtils无法获得父类private的字段。
方法大全
获取Method
方法 |
说明 |
Method findMethod(Class<?> clazz, String name) |
在类中查找指定方法 |
Method findMethod(Class<?> clazz, String name, Class<?>… paramTypes) |
同上,额外提供方法参数类型作查找条件 |
Method[] getAllDeclaredMethods(Class<?> leafClass) |
获得类中所有方法,包括继承而来的 |
Constructor accessibleConstructor(Class clazz, Class<?>… parameterTypes) |
在类中查找指定构造方法 |
boolean isEqualsMethod(Method method) |
是否是 equals() 方法 |
boolean isHashCodeMethod(Method method) |
是否是 hashCode() 方法 |
boolean isToStringMethod(Method method) |
是否是 toString() 方法 |
boolean isObjectMethod(Method method) |
是否是从 Object 类继承而来的方法 |
boolean declaresException(Method method, Class<?> exceptionType) |
检查一个方法是否声明抛出指定异常 |
调用Method
上边是文章的部分内容,为便于维护,全文已转移到此网址:Spring工具类-ReflectionUtils的使用 - 自学精灵