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 public class SerializableField implements Serializable
23 {
24
25 private Field field = null;
26
27
28
29
30
31 public SerializableField(final Field field)
32 {
33 super();
34 this.field = field;
35 }
36
37
38
39
40
41
42
43 public SerializableField(final Class<?> clazz, final String fieldName) throws NoSuchFieldException
44 {
45 this.field = ClassUtil.resolveField(clazz, fieldName);
46 }
47
48
49
50
51
52 public Field deSerialize()
53 {
54 return this.field;
55 }
56
57
58
59
60
61
62 private void writeObject(final ObjectOutputStream out) throws IOException
63 {
64 try
65 {
66 out.writeObject(this.field.getDeclaringClass());
67 out.writeObject(new FieldSignature(this.field.getName()));
68 }
69 catch (Exception exception)
70 {
71 throw new IOException(exception.getMessage());
72 }
73 }
74
75
76
77
78
79
80 private void readObject(final java.io.ObjectInputStream in) throws IOException
81 {
82 try
83 {
84 Class<?> declaringClass = (Class<?>) in.readObject();
85 FieldSignature signature = (FieldSignature) in.readObject();
86 this.field = ClassUtil.resolveField(declaringClass, signature.getStringValue());
87 }
88 catch (Exception exception)
89 {
90 throw new IOException(exception.getMessage());
91 }
92 }
93 }