1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils;
20
21
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.Serializable;
26 import java.io.StreamCorruptedException;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class DynaProperty implements Serializable {
45
46
47
48
49
50
51
52
53
54
55
56 private static final int BOOLEAN_TYPE = 1;
57 private static final int BYTE_TYPE = 2;
58 private static final int CHAR_TYPE = 3;
59 private static final int DOUBLE_TYPE = 4;
60 private static final int FLOAT_TYPE = 5;
61 private static final int INT_TYPE = 6;
62 private static final int LONG_TYPE = 7;
63 private static final int SHORT_TYPE = 8;
64
65
66
67
68
69
70
71
72
73
74 public DynaProperty(final String name) {
75
76 this(name, Object.class);
77
78 }
79
80
81
82
83
84
85
86
87 public DynaProperty(final String name, final Class<?> type) {
88
89 super();
90 this.name = name;
91 this.type = type;
92 if (type != null && type.isArray()) {
93 this.contentType = type.getComponentType();
94 }
95
96 }
97
98
99
100
101
102
103
104
105
106 public DynaProperty(final String name, final Class<?> type, final Class<?> contentType) {
107
108 super();
109 this.name = name;
110 this.type = type;
111 this.contentType = contentType;
112
113 }
114
115
116
117
118 protected String name = null;
119
120
121
122
123 public String getName() {
124 return (this.name);
125 }
126
127
128 protected transient Class<?> type = null;
129
130
131
132
133
134
135
136
137
138
139
140
141 public Class<?> getType() {
142 return (this.type);
143 }
144
145
146
147 protected transient Class<?> contentType;
148
149
150
151
152
153
154
155
156
157
158
159 public Class<?> getContentType() {
160 return contentType;
161 }
162
163
164
165
166
167
168
169
170
171
172 public boolean isIndexed() {
173
174 if (type == null) {
175 return (false);
176 } else if (type.isArray()) {
177 return (true);
178 } else if (List.class.isAssignableFrom(type)) {
179 return (true);
180 } else {
181 return (false);
182 }
183
184 }
185
186
187
188
189
190
191
192
193 public boolean isMapped() {
194
195 if (type == null) {
196 return (false);
197 } else {
198 return (Map.class.isAssignableFrom(type));
199 }
200
201 }
202
203
204
205
206
207
208
209
210
211 @Override
212 public boolean equals(final Object obj) {
213
214 boolean result = false;
215
216 result = (obj == this);
217
218 if ((!result) && obj instanceof DynaProperty) {
219 final DynaProperty that = (DynaProperty) obj;
220 result =
221 ((this.name == null) ? (that.name == null) : (this.name.equals(that.name))) &&
222 ((this.type == null) ? (that.type == null) : (this.type.equals(that.type))) &&
223 ((this.contentType == null) ? (that.contentType == null) : (this.contentType.equals(that.contentType)));
224 }
225
226 return result;
227 }
228
229
230
231
232
233
234 @Override
235 public int hashCode() {
236
237 int result = 1;
238
239 result = result * 31 + ((name == null) ? 0 : name.hashCode());
240 result = result * 31 + ((type == null) ? 0 : type.hashCode());
241 result = result * 31 + ((contentType == null) ? 0 : contentType.hashCode());
242
243 return result;
244 }
245
246
247
248
249
250 @Override
251 public String toString() {
252
253 final StringBuilder sb = new StringBuilder("DynaProperty[name=");
254 sb.append(this.name);
255 sb.append(",type=");
256 sb.append(this.type);
257 if (isMapped() || isIndexed()) {
258 sb.append(" <").append(this.contentType).append(">");
259 }
260 sb.append("]");
261 return (sb.toString());
262
263 }
264
265
266
267
268
269
270
271
272
273 private void writeObject(final ObjectOutputStream out) throws IOException {
274
275 writeAnyClass(this.type,out);
276
277 if (isMapped() || isIndexed()) {
278 writeAnyClass(this.contentType,out);
279 }
280
281
282 out.defaultWriteObject();
283 }
284
285
286
287
288 private void writeAnyClass(final Class<?> clazz, final ObjectOutputStream out) throws IOException {
289
290 int primitiveType = 0;
291 if (Boolean.TYPE.equals(clazz)) {
292 primitiveType = BOOLEAN_TYPE;
293 } else if (Byte.TYPE.equals(clazz)) {
294 primitiveType = BYTE_TYPE;
295 } else if (Character.TYPE.equals(clazz)) {
296 primitiveType = CHAR_TYPE;
297 } else if (Double.TYPE.equals(clazz)) {
298 primitiveType = DOUBLE_TYPE;
299 } else if (Float.TYPE.equals(clazz)) {
300 primitiveType = FLOAT_TYPE;
301 } else if (Integer.TYPE.equals(clazz)) {
302 primitiveType = INT_TYPE;
303 } else if (Long.TYPE.equals(clazz)) {
304 primitiveType = LONG_TYPE;
305 } else if (Short.TYPE.equals(clazz)) {
306 primitiveType = SHORT_TYPE;
307 }
308
309 if (primitiveType == 0) {
310
311 out.writeBoolean(false);
312 out.writeObject(clazz);
313 } else {
314
315 out.writeBoolean(true);
316 out.writeInt(primitiveType);
317 }
318 }
319
320
321
322
323
324
325
326
327
328 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
329
330 this.type = readAnyClass(in);
331
332 if (isMapped() || isIndexed()) {
333 this.contentType = readAnyClass(in);
334 }
335
336
337 in.defaultReadObject();
338 }
339
340
341
342
343
344 private Class<?> readAnyClass(final ObjectInputStream in) throws IOException, ClassNotFoundException {
345
346 if (in.readBoolean()) {
347
348 switch (in.readInt()) {
349
350 case BOOLEAN_TYPE: return Boolean.TYPE;
351 case BYTE_TYPE: return Byte.TYPE;
352 case CHAR_TYPE: return Character.TYPE;
353 case DOUBLE_TYPE: return Double.TYPE;
354 case FLOAT_TYPE: return Float.TYPE;
355 case INT_TYPE: return Integer.TYPE;
356 case LONG_TYPE: return Long.TYPE;
357 case SHORT_TYPE: return Short.TYPE;
358 default:
359
360 throw new StreamCorruptedException(
361 "Invalid primitive type. "
362 + "Check version of beanutils used to serialize is compatible.");
363
364 }
365
366 } else {
367
368 return ((Class<?>) in.readObject());
369 }
370 }
371 }