获取方法
// 在类中查找指定方法 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
执行方法
// 执行方法 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)
获取字段
// 在类中查找指定属性 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
设置字段
// 取消 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)
附:
@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; } }