1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.language.reflection;
11
12 import java.io.IOException;
13 import java.io.ObjectOutputStream;
14 import java.io.Serializable;
15 import java.lang.reflect.Field;
16
17 /***
18 * A SerializableField <br>
19 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
20 * University of Technology </a>, the Netherlands. <br>
21 * See for project information <a
22 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
23 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
24 * License (GPL) </a>, no warranty <br>
25 *
26 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
27 * Jacobs </a>
28 * @version 1.3 March 24, 2004
29 * @since 1.3
30 */
31 public class SerializableField implements Serializable
32 {
33 /*** the field */
34 private Field field = null;
35
36 /***
37 * constructs a new SerializableField
38 *
39 * @param field The field
40 */
41 public SerializableField(final Field field)
42 {
43 super();
44 this.field = field;
45 }
46
47 /***
48 * constructs a new SerializableField
49 *
50 * @param clazz the clazz this field is instance of
51 * @param fieldName the name of the field
52 * @throws NoSuchFieldException whenever the field is not defined in clazz
53 */
54 public SerializableField(final Class clazz, final String fieldName)
55 throws NoSuchFieldException
56 {
57 this.field = ClassUtil.resolveField(clazz, fieldName);
58 }
59
60 /***
61 * deserializes the field
62 *
63 * @return the Field
64 */
65 public Field deSerialize()
66 {
67 return this.field;
68 }
69
70 /***
71 * writes a serializable method to stream
72 *
73 * @param out the outputstream
74 * @throws IOException on IOException
75 */
76 private void writeObject(final ObjectOutputStream out) throws IOException
77 {
78 try
79 {
80 out.writeObject(this.field.getDeclaringClass());
81 out.writeObject(new FieldSignature(this.field.getName()));
82 } catch (Exception exception)
83 {
84 throw new IOException(exception.getMessage());
85 }
86 }
87
88 /***
89 * reads a serializable method from stream
90 *
91 * @param in the inputstream
92 * @throws IOException on IOException
93 */
94 private void readObject(final java.io.ObjectInputStream in)
95 throws IOException
96 {
97 try
98 {
99 Class declaringClass = (Class) in.readObject();
100 FieldSignature signature = (FieldSignature) in.readObject();
101 this.field = ClassUtil.resolveField(declaringClass, signature
102 .getStringValue());
103 } catch (Exception exception)
104 {
105 throw new IOException(exception.getMessage());
106 }
107 }
108 }