1
|
|
|
2
|
|
package com.thoughtworks.xstream.objecttree.reflection;
|
3
|
|
|
4
|
|
import com.thoughtworks.xstream.objecttree.ObjectAccessException;
|
5
|
|
import com.thoughtworks.xstream.objecttree.ObjectTree;
|
6
|
|
|
7
|
|
import java.lang.reflect.Field;
|
8
|
|
import java.lang.reflect.Modifier;
|
9
|
|
import java.util.LinkedList;
|
10
|
|
import java.util.List;
|
11
|
|
|
12
|
|
public class ReflectionObjectGraph implements ObjectTree {
|
13
|
|
|
14
|
|
private LinkedList fieldStack = new LinkedList();
|
15
|
|
private LinkedList instanceStack = new LinkedList();
|
16
|
|
private Class rootType;
|
17
|
|
private ObjectFactory objectFactory;
|
18
|
|
|
19
|
77
|
public ReflectionObjectGraph(Object root, ObjectFactory objectFactory) {
|
20
|
77
|
this.objectFactory = objectFactory;
|
21
|
77
|
init(root);
|
22
|
|
}
|
23
|
|
|
24
|
84
|
public ReflectionObjectGraph(Class rootType, ObjectFactory objectFactory) {
|
25
|
84
|
this.rootType = rootType;
|
26
|
84
|
this.objectFactory = objectFactory;
|
27
|
84
|
init(null);
|
28
|
|
}
|
29
|
|
|
30
|
|
private static class RootHolder {
|
31
|
|
Object value;
|
32
|
|
}
|
33
|
|
|
34
|
161
|
private void init(Object root) {
|
35
|
161
|
RootHolder holder = new RootHolder();
|
36
|
161
|
holder.value = root;
|
37
|
161
|
instanceStack.addLast(holder);
|
38
|
161
|
push("value");
|
39
|
|
}
|
40
|
|
|
41
|
287
|
public void push(String fieldName) {
|
42
|
287
|
Object top = instanceStack.getLast();
|
43
|
|
|
44
|
287
|
Field field = null;
|
45
|
287
|
Class currentClass = top.getClass();
|
46
|
287
|
try {
|
47
|
|
|
48
|
287
|
while (field == null) {
|
49
|
291
|
try {
|
50
|
291
|
field = currentClass.getDeclaredField(fieldName);
|
51
|
|
} catch (NoSuchFieldException e) {
|
52
|
4
|
if (Object.class.equals(currentClass)) {
|
53
|
0
|
throw new ObjectAccessException("Cannot access field " + fieldName, e);
|
54
|
|
}
|
55
|
4
|
currentClass = currentClass.getSuperclass();
|
56
|
|
}
|
57
|
|
}
|
58
|
|
|
59
|
|
} catch (SecurityException e) {
|
60
|
0
|
throw new ObjectAccessException("Cannot access field " + fieldName, e);
|
61
|
|
}
|
62
|
287
|
field.setAccessible(true);
|
63
|
287
|
fieldStack.addLast(field);
|
64
|
|
|
65
|
287
|
try {
|
66
|
287
|
instanceStack.addLast(field.get(top));
|
67
|
|
} catch (IllegalArgumentException e) {
|
68
|
0
|
throw new ObjectAccessException("Cannot access field " + fieldName, e);
|
69
|
|
} catch (IllegalAccessException e) {
|
70
|
0
|
throw new ObjectAccessException("Cannot access field " + fieldName, e);
|
71
|
|
}
|
72
|
|
|
73
|
|
}
|
74
|
|
|
75
|
126
|
public void pop() {
|
76
|
126
|
fieldStack.removeLast();
|
77
|
126
|
instanceStack.removeLast();
|
78
|
|
}
|
79
|
|
|
80
|
173
|
public Class type() {
|
81
|
173
|
if (fieldStack.size() == 1) {
|
82
|
3
|
return rootType;
|
83
|
|
} else {
|
84
|
170
|
Field field = (Field) fieldStack.getLast();
|
85
|
170
|
Class type = field.getType();
|
86
|
170
|
return type;
|
87
|
|
}
|
88
|
|
}
|
89
|
|
|
90
|
385
|
public Object get() {
|
91
|
385
|
return instanceStack.getLast();
|
92
|
|
}
|
93
|
|
|
94
|
136
|
public void set(Object value) {
|
95
|
136
|
try {
|
96
|
136
|
instanceStack.removeLast();
|
97
|
136
|
Field field = (Field) fieldStack.getLast();
|
98
|
136
|
Object top = instanceStack.getLast();
|
99
|
136
|
field.set(top, value);
|
100
|
136
|
instanceStack.addLast(value);
|
101
|
|
} catch (IllegalAccessException e) {
|
102
|
0
|
throw new ObjectAccessException("Cannot set field", e);
|
103
|
|
}
|
104
|
|
}
|
105
|
|
|
106
|
39
|
public void create(Class type) {
|
107
|
39
|
set(objectFactory.create(type));
|
108
|
|
}
|
109
|
|
|
110
|
65
|
public String[] fieldNames() {
|
111
|
65
|
List fieldNames = new LinkedList();
|
112
|
65
|
Class theClass = get().getClass();
|
113
|
65
|
Class currentClass = theClass;
|
114
|
|
|
115
|
65
|
while (!Object.class.equals(currentClass)) {
|
116
|
68
|
getFields(fieldNames, currentClass);
|
117
|
68
|
currentClass = currentClass.getSuperclass();
|
118
|
|
}
|
119
|
|
|
120
|
65
|
String[] result = new String[fieldNames.size()];
|
121
|
65
|
fieldNames.toArray(result);
|
122
|
65
|
return result;
|
123
|
|
}
|
124
|
|
|
125
|
68
|
private void getFields(List fieldNames, Class theClass) {
|
126
|
68
|
Field[] fields = theClass.getDeclaredFields();
|
127
|
68
|
for (int i = 0; i < fields.length; i++) {
|
128
|
152
|
Field field = fields[i];
|
129
|
152
|
int modifiers = field.getModifiers();
|
130
|
152
|
if (field.getName().startsWith("this$")) {
|
131
|
27
|
continue;
|
132
|
|
}
|
133
|
125
|
if (Modifier.isFinal(modifiers) ||
|
134
|
|
Modifier.isStatic(modifiers) ||
|
135
|
|
Modifier.isTransient(modifiers)) {
|
136
|
3
|
continue;
|
137
|
|
}
|
138
|
122
|
fieldNames.add(field.getName());
|
139
|
|
}
|
140
|
|
}
|
141
|
|
|
142
|
35
|
public ObjectTree newStack(Class type) {
|
143
|
35
|
return new ReflectionObjectGraph(type, objectFactory);
|
144
|
|
}
|
145
|
|
|
146
|
33
|
public ObjectTree newStack(Object instance) {
|
147
|
33
|
return new ReflectionObjectGraph(instance, objectFactory);
|
148
|
|
}
|
149
|
|
|
150
|
|
}
|
151
|
|
|