01: package org.gui4j.core.util;
02:
03: import java.io.IOException;
04: import java.io.ObjectInputStream;
05: import java.io.ObjectOutputStream;
06: import java.lang.reflect.Field;
07:
08: /**
09: * Entspricht der Klasse {@link java.lang.reflect.Field}, ausser
10: * dass diese Klasse serialisierbar ist.
11: */
12: final public class FieldCallReflection implements FieldCall {
13: private Field field;
14:
15: /**
16: * Constructor for FieldCallReflection.
17: * @param field
18: */
19: private FieldCallReflection(Field field) {
20: this .field = field;
21: }
22:
23: public static FieldCall getInstance(Field field) {
24: return field == null ? null : new FieldCallReflection(field);
25: }
26:
27: public Class getType() {
28: return field.getType();
29: }
30:
31: public static FieldCall getField(Class c, String fieldName)
32: throws NoSuchFieldException {
33: Field field = c.getField(fieldName);
34: return field == null ? null : new FieldCallReflection(field);
35: }
36:
37: private void writeObject(ObjectOutputStream out) throws IOException {
38: out.writeObject(field.getDeclaringClass());
39: out.writeObject(field.getName());
40: }
41:
42: private void readObject(ObjectInputStream in) throws IOException,
43: ClassNotFoundException {
44: Class c = (Class) in.readObject();
45: String fieldName = (String) in.readObject();
46: try {
47: field = c.getField(fieldName);
48: } catch (NoSuchFieldException e) {
49: // should not happen
50: assert false;
51: }
52: }
53:
54: public Object get(Object base) throws IllegalAccessException {
55: try {
56: return field.get(base);
57: } catch (IllegalArgumentException e) {
58: System.err.println("Field = " + field.getName()
59: + " base = " + base);
60: throw e;
61: }
62: }
63:
64: }
|