Java 如何通过反射API复制对象?

Java 如何通过反射API复制对象?,java,reflection,Java,Reflection,我想通过反射API创建对象的副本。这是我的密码: private <T> T copy(T entity) throws IllegalAccessException, InstantiationException { List<Field> fields = new ArrayList<Field>(); Field[] retrievedFields = entity.getClass().getDeclaredFields();

我想通过
反射API
创建对象的副本。这是我的密码:

private <T> T copy(T entity) throws IllegalAccessException, InstantiationException {
    List<Field> fields = new ArrayList<Field>();
    Field[] retrievedFields = entity.getClass().getDeclaredFields();
    for (Field field : retrievedFields) {
        fields.add(field);
    }
    T newEntity = (T) entity.getClass().newInstance();
    for (Field field : fields) {
        field.setAccessible(true);
        field.set(newEntity, field.get(entity));
    }
    return newEntity;
}
private T copy(T entity)抛出IllegaAccessException,实例化Exception{
列表字段=新的ArrayList();
字段[]retrievedFields=entity.getClass().getDeclaredFields();
for(字段:retrievedFields){
字段。添加(字段);
}
T newEntity=(T)entity.getClass().newInstance();
用于(字段:字段){
字段。setAccessible(true);
set(newEntity,field.get(entity));
}
返回新实体;
}

但是我不知道如何获取字段值。

您可以使用超类来获取超类。“对象”将是所有类的超类。对象的超类为空。您可以检查“Object”或null以确定终止条件。无论如何,对象的declaredFields不会返回任何内容

private <T> T copy(T entity) throws IllegalAccessException, InstantiationException {
    Class<?> clazz = entity.getClass();
    T newEntity = (T) entity.getClass().newInstance();

    while (clazz != null) {
        copyFields(entity, newEntity, clazz);
        clazz = clazz.getSuperclass();
    }

    return newEntity;
}

private <T> T copyFields(T entity, T newEntity, Class<?> clazz) throws IllegalAccessException {
    List<Field> fields = new ArrayList<>();
    for (Field field : clazz.getDeclaredFields()) {
        fields.add(field);
    }
    for (Field field : fields) {
        field.setAccessible(true);
        field.set(newEntity, field.get(entity));
    }
    return newEntity;
}
private T copy(T entity)抛出IllegaAccessException,实例化Exception{
Class clazz=entity.getClass();
T newEntity=(T)entity.getClass().newInstance();
while(clazz!=null){
copyFields(实体、新实体、clazz);
clazz=clazz.getSuperclass();
}
返回新实体;
}
私有T copyFields(T实体、T newEntity、类clazz)抛出IllegalAccessException{
列表字段=新的ArrayList();
for(字段:clazz.getDeclaredFields()){
字段。添加(字段);
}
用于(字段:字段){
字段。setAccessible(true);
set(newEntity,field.get(entity));
}
返回新实体;
}

此代码不适用于深度克隆。 可以认为这是开始:

private Object copy(Object entity) throws IllegalAccessException, InstantiationException {
    Class<?> clazz = entity.getClass();
    LOG.debug("Clazz="+clazz);
    Object newEntity = clazz.newInstance();

    while (clazz != null) {
        copyFields(entity, newEntity, clazz);
        clazz = clazz.getSuperclass();
    }
    return newEntity;
}
private void copyFields(Object entity, Object newEntity, Class<?> clazz) throws IllegalAccessException, InstantiationException {
    List<Field> fields = new ArrayList<>();
    for (Field field : clazz.getDeclaredFields()) {
        fields.add(field);
    }
    for (Field field : fields) {
        field.setAccessible(true);
        LOG.debug("Field name="+field.getName());
        LOG.debug("Field type="+field.getType());
        if (!field.getType().isPrimitive()) {
            entity = copy(field.getType().newInstance());
        } else field.set(newEntity, field.get(entity));
    }
}
私有对象副本(对象实体)抛出IllegaAccessException、InstanceionException{
Class clazz=entity.getClass();
LOG.debug(“Clazz=“+Clazz”);
Object newEntity=clazz.newInstance();
while(clazz!=null){
copyFields(实体、新实体、clazz);
clazz=clazz.getSuperclass();
}
返回新实体;
}
私有void copyFields(对象实体、对象newEntity、类clazz)引发IllegaAccessException、InstanceionException{
列表字段=新的ArrayList();
for(字段:clazz.getDeclaredFields()){
字段。添加(字段);
}
用于(字段:字段){
字段。setAccessible(true);
LOG.debug(“Field name=“+Field.getName());
LOG.debug(“Field type=“+Field.getType());
如果(!field.getType().isPrimitive()){
entity=copy(field.getType().newInstance());
}else field.set(newEntity,field.get(entity));
}
}

有一个
集合
。会不会有一个
get
?你看过javadoc吗?有
get(Object)
方法。对象是字段吗?请阅读javadoc。如果
对象
是字段,而
字段
是字段,那么重点是什么?(
Object
是要获取其字段值的对象。)我在问题中更新了代码。现在正确吗?它说“返回指定对象上此字段表示的字段值”和“参数:obj-从中提取所表示字段值的对象”