1 | |
package org.apache.ojb.broker.metadata.fieldaccess; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.lang.reflect.Field; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.apache.ojb.broker.metadata.MetadataException; |
22 | |
import org.apache.ojb.broker.util.ClassHelper; |
23 | |
import org.apache.ojb.broker.core.proxy.ProxyHelper; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class PersistentFieldDirectImpl extends PersistentFieldBase |
38 | |
{ |
39 | |
private static final long serialVersionUID = -5458024240998909205L; |
40 | |
|
41 | |
private transient boolean isInitialized; |
42 | |
private transient List fieldsList; |
43 | |
private transient Field field; |
44 | |
private transient boolean nonNested; |
45 | |
|
46 | |
public PersistentFieldDirectImpl() |
47 | |
{ |
48 | |
} |
49 | |
|
50 | |
public PersistentFieldDirectImpl(Class type, String fieldname) |
51 | |
{ |
52 | |
super(type, fieldname); |
53 | |
} |
54 | |
|
55 | |
public Class getType() |
56 | |
{ |
57 | |
return getField().getType(); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
protected Field getField() |
66 | |
{ |
67 | |
|
68 | |
if (!isInitialized) |
69 | |
{ |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
fieldsList = getFieldGraph(makeAccessible()); |
76 | |
field = (Field) fieldsList.get(fieldsList.size() - 1); |
77 | |
nonNested = fieldsList.size() == 1; |
78 | |
isInitialized = true; |
79 | |
} |
80 | |
return field; |
81 | |
} |
82 | |
|
83 | |
private List getFieldsList() |
84 | |
{ |
85 | |
|
86 | |
if (!isInitialized) getField(); |
87 | |
return fieldsList; |
88 | |
} |
89 | |
|
90 | |
protected boolean isNestedField() |
91 | |
{ |
92 | |
return !nonNested; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void set(Object target, Object value) throws MetadataException |
99 | |
{ |
100 | |
|
101 | |
if(target == null) return; |
102 | |
Object current = target; |
103 | |
if (isNestedField()) |
104 | |
{ |
105 | |
List fields = getFieldsList(); |
106 | |
int size = fields.size() - 1; |
107 | |
Field field; |
108 | |
for (int i = 0; i < size; i++) |
109 | |
{ |
110 | |
field = (Field) fields.get(i); |
111 | |
Object attribute; |
112 | |
try |
113 | |
{ |
114 | |
attribute = getValueFrom(field, current); |
115 | |
} |
116 | |
catch (Exception e) |
117 | |
{ |
118 | |
throw new MetadataException("Can't read field '" + field.getName() + "' of type " + field.getType().getName(), e); |
119 | |
} |
120 | |
if (attribute != null || value != null) |
121 | |
{ |
122 | |
|
123 | |
|
124 | |
if (attribute == null) |
125 | |
{ |
126 | |
try |
127 | |
{ |
128 | |
attribute = ClassHelper.newInstance(field.getType()); |
129 | |
} |
130 | |
catch (Exception e) |
131 | |
{ |
132 | |
throw new MetadataException("Can't create nested object of type '" |
133 | |
+ field.getType() + "' for field '" |
134 | |
+ field.getName() + "'", e); |
135 | |
} |
136 | |
} |
137 | |
try |
138 | |
{ |
139 | |
|
140 | |
setValueFor(field, current, attribute); |
141 | |
} |
142 | |
|
143 | |
catch (Exception e) |
144 | |
{ |
145 | |
throw new MetadataException("Can't set nested object of type '" |
146 | |
+ field.getType() + "' for field '" |
147 | |
+ field.getName() + "'", e); |
148 | |
} |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | |
return; |
153 | |
} |
154 | |
current = attribute; |
155 | |
} |
156 | |
} |
157 | |
setValueFor(getField(), current, value); |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public Object get(Object target) throws MetadataException |
164 | |
{ |
165 | |
Object result = target; |
166 | |
if (isNestedField()) |
167 | |
{ |
168 | |
List fields = getFieldsList(); |
169 | |
for (int i = 0; i < fields.size(); i++) |
170 | |
{ |
171 | |
if (result == null) break; |
172 | |
result = getValueFrom((Field) fields.get(i), result); |
173 | |
} |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | |
result = result != null ? getValueFrom(getField(), result) : null; |
178 | |
} |
179 | |
return result; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
protected Object getValueFrom(Field field, Object target) |
185 | |
{ |
186 | |
try |
187 | |
{ |
188 | |
return field.get(ProxyHelper.getRealObject(target)); |
189 | |
|
190 | |
|
191 | |
} |
192 | |
catch (IllegalAccessException e) |
193 | |
{ |
194 | |
throw new MetadataException( |
195 | |
"IllegalAccess error reading field: " + |
196 | |
(field != null ? field.getName() : null) + " from object: " |
197 | |
+ (target != null ? target.getClass().getName() : null), e); |
198 | |
} |
199 | |
catch (IllegalArgumentException e) |
200 | |
{ |
201 | |
throw new MetadataException( |
202 | |
"IllegalArgument error reading field: " + |
203 | |
buildErrorGetMsg(target, field), e); |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
protected void setValueFor(Field field, Object target, final Object value) |
208 | |
{ |
209 | |
try |
210 | |
{ |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
if ((value != null) || !field.getType().isPrimitive()) |
218 | |
{ |
219 | |
field.set(ProxyHelper.getRealObject(target), value); |
220 | |
|
221 | |
|
222 | |
} |
223 | |
} |
224 | |
catch (NullPointerException ignored) |
225 | |
{ |
226 | |
getLog().info("Target object '" + (target != null ? target.getClass().getName() : null) |
227 | |
+ "' for field '" + (field != null ? field.getName() : null) |
228 | |
+ "' of type '" + (field != null ? field.getType().getName() : null) |
229 | |
+ "' seems to be null. Can't write into null.", ignored); |
230 | |
} |
231 | |
catch (Exception e) |
232 | |
{ |
233 | |
getLog().error("while set field: " + buildErrorSetMsg(target, value, field)); |
234 | |
throw new MetadataException("IllegalAccess error setting field:" + |
235 | |
(field != null ? field.getName() : null) + " in object:" + target.getClass().getName(), e); |
236 | |
} |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
protected boolean makeAccessible() |
243 | |
{ |
244 | |
return true; |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public boolean usesAccessorsAndMutators() |
252 | |
{ |
253 | |
return false; |
254 | |
} |
255 | |
} |