Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/ReflectionAccessor.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.impl;
9
10 import java.lang.reflect.AccessibleObject;
11 import java.lang.reflect.Array;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.Field;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Modifier;
16 import java.util.List;
17
18 import com.sleepycat.compat.DbCompat;
19
20 /**
21 * Implements Accessor using reflection.
22 *
23 * @author Mark Hayes
24 */
25 class ReflectionAccessor implements Accessor {
26
27 private static final FieldAccess[] EMPTY_KEYS = {};
28
29 private Class type;
30 private Accessor superAccessor;
31 private Constructor constructor;
32 private FieldAccess priKey;
33 private FieldAccess[] secKeys;
34 private FieldAccess[] nonKeys;
35
36 private ReflectionAccessor(Class type, Accessor superAccessor) {
37 this.type = type;
38 this.superAccessor = superAccessor;
39 try {
40 constructor = type.getDeclaredConstructor();
41 } catch (NoSuchMethodException e) {
42 throw DbCompat.unexpectedState(type.getName());
43 }
44 if (!Modifier.isPublic(type.getModifiers()) ||
45 !Modifier.isPublic(constructor.getModifiers())) {
46 setAccessible(constructor, type.getName() + "()");
47 }
48 }
49
50 /**
51 * Creates an accessor for a complex type.
52 */
53 ReflectionAccessor(Catalog catalog,
54 Class type,
55 Accessor superAccessor,
56 FieldInfo priKeyField,
57 List<FieldInfo> secKeyFields,
58 List<FieldInfo> nonKeyFields) {
59 this(type, superAccessor);
60 if (priKeyField != null) {
61 priKey = getField(catalog, priKeyField,
62 true /*isRequiredKeyField*/);
63 } else {
64 priKey = null;
65 }
66 if (secKeyFields.size() > 0) {
67 secKeys = getFields(catalog, secKeyFields,
68 false /*isRequiredKeyField*/);
69 } else {
70 secKeys = EMPTY_KEYS;
71 }
72 if (nonKeyFields.size() > 0) {
73 nonKeys = getFields(catalog, nonKeyFields,
74 false /*isRequiredKeyField*/);
75 } else {
76 nonKeys = EMPTY_KEYS;
77 }
78 }
79
80 /**
81 * Creates an accessor for a composite key type.
82 */
83 ReflectionAccessor(Catalog catalog,
84 Class type,
85 List<FieldInfo> fieldInfos) {
86 this(type, null);
87 priKey = null;
88 secKeys = EMPTY_KEYS;
89 nonKeys = getFields(catalog, fieldInfos, true /*isRequiredKeyField*/);
90 }
91
92 private FieldAccess[] getFields(Catalog catalog,
93 List<FieldInfo> fieldInfos,
94 boolean isRequiredKeyField) {
95 int index = 0;
96 FieldAccess[] fields = new FieldAccess[fieldInfos.size()];
97 for (FieldInfo info : fieldInfos) {
98 fields[index] = getField(catalog, info, isRequiredKeyField);
99 index += 1;
100 }
101 return fields;
102 }
103
104 private FieldAccess getField(Catalog catalog,
105 FieldInfo fieldInfo,
106 boolean isRequiredKeyField) {
107 Field field;
108 try {
109 field = type.getDeclaredField(fieldInfo.getName());
110 } catch (NoSuchFieldException e) {
111 throw DbCompat.unexpectedException(e);
112 }
113 if (!Modifier.isPublic(type.getModifiers()) ||
114 !Modifier.isPublic(field.getModifiers())) {
115 setAccessible(field, field.getName());
116 }
117 Class fieldCls = field.getType();
118 if (fieldCls.isPrimitive()) {
119 assert SimpleCatalog.isSimpleType(fieldCls);
120 return new PrimitiveAccess
121 (field, (SimpleFormat) catalog.getFormat(fieldCls.getName()));
122 } else if (isRequiredKeyField) {
123 Format format = catalog.getFormat(fieldInfo.getClassName());
124 assert format != null;
125 return new KeyObjectAccess(field, format);
126 } else if (fieldCls == String.class) {
127 return new StringAccess(field);
128 } else {
129 return new ObjectAccess(field);
130 }
131 }
132
133 private void setAccessible(AccessibleObject object, String memberName) {
134 try {
135 object.setAccessible(true);
136 } catch (SecurityException e) {
137 throw new IllegalStateException
138 ("Unable to access non-public member: " +
139 type.getName() + '.' + memberName +
140 ". Please configure the Java Security Manager setting: " +
141 " ReflectPermission suppressAccessChecks", e);
142 }
143 }
144
145 public Object newInstance() {
146 try {
147 return constructor.newInstance();
148 } catch (IllegalAccessException e) {
149 throw DbCompat.unexpectedException(e);
150 } catch (InstantiationException e) {
151 throw DbCompat.unexpectedException(e);
152 } catch (InvocationTargetException e) {
153 throw DbCompat.unexpectedException(e);
154 }
155 }
156
157 public Object newArray(int len) {
158 return Array.newInstance(type, len);
159 }
160
161 public boolean isPriKeyFieldNullOrZero(Object o) {
162 try {
163 if (priKey != null) {
164 return priKey.isNullOrZero(o);
165 } else if (superAccessor != null) {
166 return superAccessor.isPriKeyFieldNullOrZero(o);
167 } else {
168 throw DbCompat.unexpectedState("No primary key field");
169 }
170 } catch (IllegalAccessException e) {
171 throw DbCompat.unexpectedException(e);
172 }
173 }
174
175 public void writePriKeyField(Object o, EntityOutput output)
176 throws RefreshException {
177
178 try {
179 if (priKey != null) {
180 priKey.write(o, output);
181 } else if (superAccessor != null) {
182 superAccessor.writePriKeyField(o, output);
183 } else {
184 throw DbCompat.unexpectedState("No primary key field");
185 }
186 } catch (IllegalAccessException e) {
187 throw DbCompat.unexpectedException(e);
188 }
189 }
190
191 public void readPriKeyField(Object o, EntityInput input)
192 throws RefreshException {
193
194 try {
195 if (priKey != null) {
196 priKey.read(o, input);
197 } else if (superAccessor != null) {
198 superAccessor.readPriKeyField(o, input);
199 } else {
200 throw DbCompat.unexpectedState("No primary key field");
201 }
202 } catch (IllegalAccessException e) {
203 throw DbCompat.unexpectedException(e);
204 }
205 }
206
207 public void writeSecKeyFields(Object o, EntityOutput output)
208 throws RefreshException {
209
210 try {
211
212 /*
213 * In JE 5.0, String is treated as primitive type, so String does
214 * not need to be registered. [#19247]
215 */
216 if (priKey != null && !priKey.isPrimitive && !priKey.isString) {
217 output.registerPriKeyObject(priKey.field.get(o));
218 }
219 if (superAccessor != null) {
220 superAccessor.writeSecKeyFields(o, output);
221 }
222 for (int i = 0; i < secKeys.length; i += 1) {
223 secKeys[i].write(o, output);
224 }
225 } catch (IllegalAccessException e) {
226 throw DbCompat.unexpectedException(e);
227 }
228 }
229
230 public void readSecKeyFields(Object o,
231 EntityInput input,
232 int startField,
233 int endField,
234 int superLevel)
235 throws RefreshException {
236
237 try {
238 if (priKey != null && !priKey.isPrimitive && !priKey.isString) {
239 input.registerPriKeyObject(priKey.field.get(o));
240 } else if (priKey != null && priKey.isString) {
241 input.registerPriStringKeyObject(priKey.field.get(o));
242 }
243 if (superLevel != 0 && superAccessor != null) {
244 superAccessor.readSecKeyFields
245 (o, input, startField, endField, superLevel - 1);
246 } else {
247 if (superLevel > 0) {
248 throw DbCompat.unexpectedState
249 ("Superclass does not exist");
250 }
251 }
252 if (superLevel <= 0) {
253 for (int i = startField;
254 i <= endField && i < secKeys.length;
255 i += 1) {
256 secKeys[i].read(o, input);
257 }
258 }
259 } catch (IllegalAccessException e) {
260 throw DbCompat.unexpectedException(e);
261 }
262 }
263
264 public void writeNonKeyFields(Object o, EntityOutput output)
265 throws RefreshException {
266
267 try {
268 if (superAccessor != null) {
269 superAccessor.writeNonKeyFields(o, output);
270 }
271 for (int i = 0; i < nonKeys.length; i += 1) {
272 nonKeys[i].write(o, output);
273 }
274 } catch (IllegalAccessException e) {
275 throw DbCompat.unexpectedException(e);
276 }
277 }
278
279 public void readNonKeyFields(Object o,
280 EntityInput input,
281 int startField,
282 int endField,
283 int superLevel)
284 throws RefreshException {
285
286 try {
287 if (superLevel != 0 && superAccessor != null) {
288 superAccessor.readNonKeyFields
289 (o, input, startField, endField, superLevel - 1);
290 } else {
291 if (superLevel > 0) {
292 throw DbCompat.unexpectedState
293 ("Superclass does not exist");
294 }
295 }
296 if (superLevel <= 0) {
297 for (int i = startField;
298 i <= endField && i < nonKeys.length;
299 i += 1) {
300 nonKeys[i].read(o, input);
301 }
302 }
303 } catch (IllegalAccessException e) {
304 throw DbCompat.unexpectedException(e);
305 }
306 }
307
308 public void writeCompositeKeyFields(Object o, EntityOutput output)
309 throws RefreshException {
310
311 try {
312 for (int i = 0; i < nonKeys.length; i += 1) {
313 nonKeys[i].write(o, output);
314 }
315 } catch (IllegalAccessException e) {
316 throw DbCompat.unexpectedException(e);
317 }
318 }
319
320 public void readCompositeKeyFields(Object o, EntityInput input)
321 throws RefreshException {
322
323 try {
324 for (int i = 0; i < nonKeys.length; i += 1) {
325 nonKeys[i].read(o, input);
326 }
327 } catch (IllegalAccessException e) {
328 throw DbCompat.unexpectedException(e);
329 }
330 }
331
332 public Object getField(Object o,
333 int field,
334 int superLevel,
335 boolean isSecField) {
336 if (superLevel > 0) {
337 return superAccessor.getField
338 (o, field, superLevel - 1, isSecField);
339 }
340 try {
341 Field fld =
342 isSecField ? secKeys[field].field : nonKeys[field].field;
343 return fld.get(o);
344 } catch (IllegalAccessException e) {
345 throw DbCompat.unexpectedException(e);
346 }
347 }
348
349 public void setField(Object o,
350 int field,
351 int superLevel,
352 boolean isSecField,
353 Object value) {
354 if (superLevel > 0) {
355 superAccessor.setField
356 (o, field, superLevel - 1, isSecField, value);
357 return;
358 }
359 try {
360 Field fld =
361 isSecField ? secKeys[field].field : nonKeys[field].field;
362 fld.set(o, value);
363 } catch (IllegalAccessException e) {
364 throw DbCompat.unexpectedException(e);
365 }
366 }
367
368 public void setPriField(Object o, Object value) {
369 try {
370 if (priKey != null) {
371 priKey.field.set(o, value);
372 } else if (superAccessor != null) {
373 superAccessor.setPriField(o, value);
374 } else {
375 throw DbCompat.unexpectedState("No primary key field");
376 }
377 } catch (IllegalAccessException e) {
378 throw DbCompat.unexpectedException(e);
379 }
380 }
381
382 /**
383 * Abstract base class for field access classes.
384 */
385 private static abstract class FieldAccess {
386
387 Field field;
388 boolean isPrimitive;
389 boolean isString = false;
390
391 FieldAccess(Field field) {
392 this.field = field;
393 isPrimitive = field.getType().isPrimitive();
394 isString =
395 field.getType().getName().equals(String.class.getName());
396 }
397
398 /**
399 * Writes a field.
400 */
401 abstract void write(Object o, EntityOutput out)
402 throws IllegalAccessException, RefreshException;
403
404 /**
405 * Reads a field.
406 */
407 abstract void read(Object o, EntityInput in)
408 throws IllegalAccessException, RefreshException;
409
410 /**
411 * Returns whether a field is null (for reference types) or zero (for
412 * primitive integer types). This implementation handles the reference
413 * types.
414 */
415 boolean isNullOrZero(Object o)
416 throws IllegalAccessException {
417
418 return field.get(o) == null;
419 }
420 }
421
422 /**
423 * Access for fields with object types.
424 */
425 private static class ObjectAccess extends FieldAccess {
426
427 ObjectAccess(Field field) {
428 super(field);
429 }
430
431 @Override
432 void write(Object o, EntityOutput out)
433 throws IllegalAccessException, RefreshException {
434
435 out.writeObject(field.get(o), null);
436 }
437
438 @Override
439 void read(Object o, EntityInput in)
440 throws IllegalAccessException, RefreshException {
441
442 field.set(o, in.readObject());
443 }
444 }
445
446 /**
447 * Access for primary key fields and composite key fields with object
448 * types.
449 */
450 private static class KeyObjectAccess extends FieldAccess {
451
452 private Format format;
453
454 KeyObjectAccess(Field field, Format format) {
455 super(field);
456 this.format = format;
457 }
458
459 @Override
460 void write(Object o, EntityOutput out)
461 throws IllegalAccessException, RefreshException {
462
463 out.writeKeyObject(field.get(o), format);
464 }
465
466 @Override
467 void read(Object o, EntityInput in)
468 throws IllegalAccessException, RefreshException {
469
470 field.set(o, in.readKeyObject(format));
471 }
472 }
473
474 /**
475 * Access for String fields, that are not primary key fields or composite
476 * key fields with object types.
477 */
478 private static class StringAccess extends FieldAccess {
479 StringAccess(Field field) {
480 super(field);
481 }
482
483 @Override
484 void write(Object o, EntityOutput out)
485 throws IllegalAccessException, RefreshException {
486
487 out.writeString((String) field.get(o));
488 }
489
490 @Override
491 void read(Object o, EntityInput in)
492 throws IllegalAccessException, RefreshException {
493
494 field.set(o, in.readStringObject());
495 }
496 }
497
498 /**
499 * Access for fields with primitive types.
500 */
501 private static class PrimitiveAccess extends FieldAccess {
502
503 private SimpleFormat format;
504
505 PrimitiveAccess(Field field, SimpleFormat format) {
506 super(field);
507 this.format = format;
508 }
509
510 @Override
511 void write(Object o, EntityOutput out)
512 throws IllegalAccessException {
513
514 format.writePrimitiveField(o, out, field);
515 }
516
517 @Override
518 void read(Object o, EntityInput in)
519 throws IllegalAccessException, RefreshException {
520
521 format.readPrimitiveField(o, in, field);
522 }
523
524 @Override
525 boolean isNullOrZero(Object o)
526 throws IllegalAccessException {
527
528 return field.getLong(o) == 0;
529 }
530 }
531 }