-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeepCopy.java
57 lines (48 loc) · 2.02 KB
/
DeepCopy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
public class DeepCopy {
public static Object CopyObject(Object object) {
try {
Object clone = object.getClass().newInstance();
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true);
if (field.get(object) == null || Modifier.isFinal(field.getModifiers())) {
continue;
}
if(field.getType().equals(List.class)) {
List list = (List)field.get(object);
if(field.get(object) instanceof ArrayList) {
List copyList = new ArrayList();
for (int i = 0; i < list.size(); i++) {
copyList.add(list.get(i));
}
field.set(clone, copyList);
}
}
else if(field.getType().isPrimitive()
|| field.getType().equals(String.class)
|| field.getType().getSuperclass().equals(Number.class)
|| field.getType().equals(Boolean.class)) {
field.set(clone, field.get(object));
}
else {
Object childObj = field.get(object);
if(childObj == object) {
field.set(clone,clone);
}
else {
field.set(clone,CopyObject(field.get(object)));
}
}
}
return clone;
}
catch(Exception e)
{
System.out.println("Error: " + e.toString());
return null;
}
}
}