Java 反思:对象不是声明的实例 class
Java Reflection: object is not an instance of declaring class
用户class
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(unique=true)
private String username;
@JsonIgnore
private String password;
private String firstName;
private String lastName;
private Date dob;
private String motherName;
private String fatherName;
private String mobileNumber;
@Column(unique=true)
private String email;
@ManyToOne
@JoinColumn(name="role_id", nullable=false)
private Role role;
private String status;
public void setPassword(String password) {
this.password = PasswordEncoder.getEncodedPassword(password);
}
}
这是我的 UserRequest class
@Data
public class UserRequest {
private Long id;
private String username;
private String password;
private String firstName;
private String lastName;
private Date dob;
private String motherName;
private String fatherName;
private String mobileNumber;
private String email;
private Long role;
private String status;
}
我的复制方法存在于 Utility class
public static void copy(Object dest, Object source) throws IntrospectionException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
PropertyDescriptor[] pdList = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pdList) {
Method writeMethod = null;
Method readMethod = null;
try {
writeMethod = pd.getWriteMethod();
readMethod = pd.getReadMethod();
} catch (Exception e) {
}
if (readMethod == null || writeMethod == null) {
continue;
}
Object val = readMethod.invoke(source);
writeMethod.invoke(dest, val);
}
}
并且我将此复制方法称为类似这样
Utility.copy(user,userRequest);
并在执行 writeMethod.invoke(dest, val) 时出现以下异常。有人可以帮忙吗?
object is not an instance of declaring class at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
您只会获得 来源 class 的 BeanInfo
,然后从 [=10] 获得 属性 描述符=]...所以 writeMethod
是 source class 上的 setter,但你在 [=] 的实例上调用它18=]目的地class.
您可能需要分别获取源和目标 class 的 BeanInfo
,然后对于源中的每个 属性,找到具有相同名称的那个(如果any) 在目标 class 中,并使用 that 属性 的 write 方法来设置值。
用户class
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(unique=true)
private String username;
@JsonIgnore
private String password;
private String firstName;
private String lastName;
private Date dob;
private String motherName;
private String fatherName;
private String mobileNumber;
@Column(unique=true)
private String email;
@ManyToOne
@JoinColumn(name="role_id", nullable=false)
private Role role;
private String status;
public void setPassword(String password) {
this.password = PasswordEncoder.getEncodedPassword(password);
}
}
这是我的 UserRequest class
@Data
public class UserRequest {
private Long id;
private String username;
private String password;
private String firstName;
private String lastName;
private Date dob;
private String motherName;
private String fatherName;
private String mobileNumber;
private String email;
private Long role;
private String status;
}
我的复制方法存在于 Utility class
public static void copy(Object dest, Object source) throws IntrospectionException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
PropertyDescriptor[] pdList = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pdList) {
Method writeMethod = null;
Method readMethod = null;
try {
writeMethod = pd.getWriteMethod();
readMethod = pd.getReadMethod();
} catch (Exception e) {
}
if (readMethod == null || writeMethod == null) {
continue;
}
Object val = readMethod.invoke(source);
writeMethod.invoke(dest, val);
}
}
并且我将此复制方法称为类似这样
Utility.copy(user,userRequest);
并在执行 writeMethod.invoke(dest, val) 时出现以下异常。有人可以帮忙吗?
object is not an instance of declaring class at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564)
您只会获得 来源 class 的 BeanInfo
,然后从 [=10] 获得 属性 描述符=]...所以 writeMethod
是 source class 上的 setter,但你在 [=] 的实例上调用它18=]目的地class.
您可能需要分别获取源和目标 class 的 BeanInfo
,然后对于源中的每个 属性,找到具有相同名称的那个(如果any) 在目标 class 中,并使用 that 属性 的 write 方法来设置值。