1 package nl.tudelft.simulation.language.reflection;
2
3 import java.io.IOException;
4 import java.io.ObjectOutputStream;
5 import java.io.Serializable;
6 import java.lang.reflect.Field;
7
8 import org.djutils.reflection.ClassUtil;
9 import org.djutils.reflection.FieldSignature;
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class SerializableField implements Serializable
24 {
25
26 private Field field = null;
27
28
29
30
31
32 public SerializableField(final Field field)
33 {
34 super();
35 this.field = field;
36 }
37
38
39
40
41
42
43
44 public SerializableField(final Class<?> clazz, final String fieldName) throws NoSuchFieldException
45 {
46 this.field = ClassUtil.resolveField(clazz, fieldName);
47 }
48
49
50
51
52
53 public Field deSerialize()
54 {
55 return this.field;
56 }
57
58
59
60
61
62
63 private void writeObject(final ObjectOutputStream out) throws IOException
64 {
65 try
66 {
67 out.writeObject(this.field.getDeclaringClass());
68 out.writeObject(new FieldSignature(this.field.getName()));
69 }
70 catch (Exception exception)
71 {
72 throw new IOException(exception.getMessage());
73 }
74 }
75
76
77
78
79
80
81 private void readObject(final java.io.ObjectInputStream in) throws IOException
82 {
83 try
84 {
85 Class<?> declaringClass = (Class<?>) in.readObject();
86 FieldSignature signature = (FieldSignature) in.readObject();
87 this.field = ClassUtil.resolveField(declaringClass, signature.getStringValue());
88 }
89 catch (Exception exception)
90 {
91 throw new IOException(exception.getMessage());
92 }
93 }
94 }