使用反射java的深度复制

使用反射java的深度复制,java,reflection,deep-copy,Java,Reflection,Deep Copy,我无法使用反射从类字段获取容器。我尝试了以下方法,但出现异常: Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at java.util.Collection

我无法使用反射从类字段获取容器。我尝试了以下方法,但出现异常:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at java.util.Collections.addAll(Collections.java:5455)
publicstaticvoidcopy(objectfrom,objectto)抛出NoSuchFieldException、IllegalAccessException{
Class fromClass=from.getClass();
类toClass=to.getClass();
Field[]sourceFields=fromClass.getDeclaredFields();
for(字段fromField:sourceFields){
Field toField=toClass.getDeclaredField(fromField.getName());
toField.setAccessible(true);
fromField.setAccessible(true);
if(fromField.getType().equals(toField.getType())){
if(!(fromField.getType()==String.class | | fromField.getType().isPrimitive()){
if(fromField.getType().isAssignableFrom(List.class)){
List=(List)fromField.get(from);
List list1=(List)toField.get(to);
Collections.addAll(列表1,列表);
toField.set(to,fromField.get(from));
}else if(fromField.getType().isAssignableFrom(Set.class)){
Set=(Set)fromField.get(from);
Set set1=(Set)toField.get(to);
set1.clear();
set.addAll(set1);
toField.set(to,fromField.get(from));
}
}否则{
toField.set(to,fromField.get(from));
}
}
}
}

我不想使用通过序列化复制的方法,我对反射感兴趣。

我希望你这样做是为了培训?如果没有,那么使用一些开源库,这比你想象的要困难得多

您的问题是您正在将添加到
列表,而
列表是一个不支持添加的实现(顺便说一句,那么您将忽略结果)。我建议创建一个新列表并对其重新排序,而不是添加到现有列表中

List List=(List)fromField.get(from);
List list1=(List)toField.get(to);
List newList=newarraylist();
如果(列表!=null)
Collections.addAll(newList,list);
if(list1!=null)
Collections.addAll(newList,list1);
设置(到,新列表);

Set
类似-您当前的
Set
代码没有任何意义,它在
Class
对象上运行。

您试图添加到的列表似乎不支持
add
操作。您可能需要调试它,并查看您正在处理的
List
的实现。使用toField的getClass查看它是什么列表实现。某些实现不支持
add
(如
Arrays.asList
)。实现类是AbstractListClass的子类,没有自己的add,您可以检查它。
public static void copy(Object from, Object to) throws NoSuchFieldException, IllegalAccessException {
        Class<?> fromClass = from.getClass();
        Class<?> toClass = to.getClass();
        Field[] sourceFields = fromClass.getDeclaredFields();
        for (Field fromField : sourceFields) {
            Field toField = toClass.getDeclaredField(fromField.getName());
            toField.setAccessible(true);
            fromField.setAccessible(true);
            if (fromField.getType().equals(toField.getType())) {
                if (!(fromField.getType() == String.class || fromField.getType().isPrimitive())) {
                        if (fromField.getType().isAssignableFrom(List.class)) {
                            List list = (List) fromField.get(from);
                            List list1 = (List) toField.get(to);
                            Collections.addAll(list1,list);
                            toField.set(to, fromField.get(from));
                        } else if (fromField.getType().isAssignableFrom(Set.class)) {
                            Set set = (Set) fromField.get(from);
                            Set set1 = (Set) toField.get(to);
                            set1.clear();
                            set.addAll(set1);
                            toField.set(to, fromField.get(from));
                        }
                } else {
                    toField.set(to, fromField.get(from));
                }
            }
        }
    }