简体   繁体   English

Java反射:对象不是声明类的实例

[英]Java Reflection: object is not an instance of declaring class

User 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);
        }
    }

Here is my UserRequest class这是我的 UserRequest 类

@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;
}

and my copy method is present in Utility class并且我的复制方法存在于 Utility 类中

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);
    }
}

and I'm calling this copy method something like this我正在像这样调用这个复制方法

 Utility.copy(user,userRequest);

and getting the following exception when writeMethod.invoke(dest, val) is executed.并在执行 writeMethod.invoke(dest, val) 时出现以下异常。 can anyone please help?有人可以帮忙吗?

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)对象不是在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) 处的 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 处声明类的实例。 base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:564)

You're only getting the BeanInfo for the source class, and then getting the property descriptors from that BeanInfo ... so writeMethod is a setter on the source class, but you're calling it on an instance of the destination class.您只获取类的BeanInfo ,然后从该BeanInfo获取属性描述符……因此writeMethod类的 setter,但您是在目标类的实例上调用它。

You probably need to get the BeanInfo for the source and destination classes separately, then for each property in the source, find the one with the same name (if any) in the destination class, and use that property's write method to set the value.您可能需要分别获取源类和目标类的BeanInfo ,然后对于源中的每个属性,在目标类中找到具有相同名称(如果有)的那个,并使用属性的 write 方法来设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象不是使用 java 反射声明类的实例 - object is not an instance of declaring class on using java reflection Java 反射 - 对象不是声明类的实例 - Java Reflection - Object is not an instance of declaring class Java对象不是声明类的实例 - Java object is not an instance of declaring class java.lang.IllegalArgumentException:当我使用反射时,object不是声明类的实例 - java.lang.IllegalArgumentException: object is not an instance of declaring class when I use reflection 使用对象实例而不是类的 Java 注释反射 - Java annotation reflection with object instance instead of class 对象不是声明类的实例 - Object is not an instance of declaring class spark java:java.lang.IllegalArgumentException:对象不是声明类的实例 - spark java: java.lang.IllegalArgumentException: object is not an instance of declaring class 为什么在使用反射调用方法时,“对象不是声明类的实例”? - Why do I get “object is not an instance of declaring class” when invoking a method using reflection? java.lang.IllegalArgumentException:对象不是声明类的实例 - java.lang.IllegalArgumentException: object is not an instance of declaring class Java如何解决“对象不是声明类的实例”错误? - Java how to fix “object is not an instance of declaring class” error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM