org.springframework.util.ReflectionUtils
获取方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // 在类中查找指定方法 Method getName = ReflectionUtils.findMethod(Hello2. class , "getName" ); System.out.println(getName); //public java.lang.String com.xc.xcspringboot.model.Hello2.getName() // 同上,额外提供方法参数类型作查找条件 Method getName1 = ReflectionUtils.findMethod(Hello2. class , "getName" , String. class ); System.out.println(getName1); //public java.lang.String com.xc.xcspringboot.model.Hello2.getName(java.lang.String) // 获得类中所有方法,包括继承而来的 Method[] allDeclaredMethods = ReflectionUtils.getAllDeclaredMethods(Hello2. class ); // System.out.println(ArrayUtils.toString(allDeclaredMethods)); // 在类中查找指定构造方法 Constructor<Hello2> hello2Constructor = ReflectionUtils.accessibleConstructor(Hello2. class ); System.out.println(hello2Constructor); //public com.xc.xcspringboot.model.Hello2() Constructor<Hello2> hello2Constructor2 = ReflectionUtils.accessibleConstructor(Hello2. class , Integer. class , String. class ); System.out.println(hello2Constructor2); //public com.xc.xcspringboot.model.Hello2(java.lang.Integer,java.lang.String) // 是否是 equals() 方法 Method equals_method = ReflectionUtils.findMethod(Hello2. class , "equals" , Object. class ); boolean equalsMethod = ReflectionUtils.isEqualsMethod(equals_method); System.out.println(equalsMethod); //true for (Method method : allDeclaredMethods) { boolean equalsMethod2 = ReflectionUtils.isEqualsMethod(method); // System.out.println(equalsMethod2); } // 是否是 hashCode() 方法 Method hashCode_method = ReflectionUtils.findMethod(Hello2. class , "hashCode" ); boolean hashCodeMethod = ReflectionUtils.isHashCodeMethod(hashCode_method); System.out.println(hashCodeMethod); //true // 是否是 toString() 方法 Method toString = ReflectionUtils.findMethod(Hello2. class , "toString" ); boolean toStringMethod = ReflectionUtils.isToStringMethod(toString); System.out.println(toStringMethod); //true // 是否是从 Object 类继承而来的方法 for (Method method : allDeclaredMethods) { boolean equalsMethod2 = ReflectionUtils.isObjectMethod(method); // System.out.println(equalsMethod2); } // 检查一个方法是否声明抛出指定异常 boolean declaresException = ReflectionUtils.declaresException(getName1, XcException. class ); System.out.println(declaresException); //true |
执行方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 执行方法 Hello2 hello2 = new Hello2( 1 , "aaa" ); Object invokeMethod = ReflectionUtils.invokeMethod(getName, hello2); System.out.println(invokeMethod); //aaa // 同上,提供方法参数 Object invokeMethod2 = ReflectionUtils.invokeMethod(getName1, hello2, "bbb" ); System.out.println(invokeMethod2); //bbb // 取消 Java 权限检查。以便后续执行该私有方法 Method getName2 = ReflectionUtils.findMethod(Hello2. class , "getName2" ); ReflectionUtils.makeAccessible(getName2); Object invokeMethod3 = ReflectionUtils.invokeMethod(getName2, hello2); System.out.println(invokeMethod3); //aaa // 取消 Java 权限检查。以便后续执行私有构造方法 Constructor<Hello2> hello2Constructor3 = ReflectionUtils.accessibleConstructor(Hello2. class , Integer. class , String. class , String. class ); ReflectionUtils.makeAccessible(hello2Constructor3); Hello2 hello3 = hello2Constructor3.newInstance( 1 , "aaa" , "176" ); System.out.println(hello3); //Hello2(id=1, name=aaa, age=null, phone=176) |
获取字段
1 2 3 4 5 6 7 8 9 | // 在类中查找指定属性 Field name = ReflectionUtils.findField(Hello2. class , "name" ); System.out.println(name); //private java.lang.String com.xc.xcspringboot.test.org.springframework.util.ReflectionUtilsTest.Hello2.name // 同上,多提供了属性的类型 Field name3 = ReflectionUtils.findField(Hello2. class , "name3" , String. class ); System.out.println(name3); //private java.lang.String com.xc.xcspringboot.test.org.springframework.util.ReflectionUtilsTest.Hello2.name // 是否为一个 "public static final" 属性 boolean publicStaticFinal = ReflectionUtils.isPublicStaticFinal(name3); System.out.println(publicStaticFinal); //true |
设置字段
1 2 3 4 5 6 7 8 9 10 | // 取消 Java 的权限控制检查。以便后续读写该私有属性 ReflectionUtils.makeAccessible(name_field); // 设置 target 对象的 field 属性值,值为 value ReflectionUtils.setField(name_field, hello3, "aac" ); // 获取 target 对象的 field 属性值 Object field = ReflectionUtils.getField(name_field, hello3); System.out.println(field); //aac // 同类对象属性对等赋值 ReflectionUtils.shallowCopyFieldState(hello3, hello2); System.out.println(hello2); //Hello2(id=1, name=aac, age=null, phone=176) |
附:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | @Data @AllArgsConstructor @NoArgsConstructor public class Hello2 { public static final String name3 = "name3" ; @ApiModelProperty (value = "主键" ) @NotNull (message = "修改角色必须有id" , groups = UpdateValid. class ) @ApiComment (value = "主键" , sample = "1" ) private Integer id; @ApiModelProperty (value = "名称" ) @NotNull (message = "添加角色必须有name" , groups = AddValid. class ) @Length (min = 1 , max = 10 , message = "名称不合法" , groups = {AddValid. class , UpdateValid. class }) @Size (min = 1 , max = 5 , message = "name 长度小于1或大于5" ) @ApiComment (value = "名称" , sample = "xc" ) private String name; @ApiModelProperty (value = "年龄" ) @NotNull (message = "age 为空" ) @ApiComment (value = "年龄" , sample = "18" ) private Integer age; @ApiModelProperty (value = "手机号" ) @NotNull (message = "手机号不能为空" ) @PhoneValid private String phone; public String getName(String aaa) throws XcException { return aaa; } public Hello2( @NotNull (message = "修改角色必须有id" , groups = UpdateValid. class ) Integer id, String name) { this .id = id; this .name = name; } private Hello2( @NotNull (message = "修改角色必须有id" , groups = UpdateValid. class ) Integer id, String name, String phone) { this .id = id; this .name = name; this .phone = phone; } private String getName2() { return name; } } |
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix