1
2
3
4
5
6 package nl.tudelft.simulation.language.reflection;
7
8 import java.io.IOException;
9 import java.io.ObjectOutputStream;
10 import java.io.Serializable;
11 import java.lang.reflect.Field;
12
13 /***
14 * A SerializableField.
15 * <p>
16 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
17 * Netherlands.
18 * <p>
19 * See for project information <a
20 * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
21 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
22 * </a>, no warranty
23 *
24 * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
25 * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:23 $
26 * @since 1.5
27 */
28 public class SerializableField implements Serializable
29 {
30 /*** the field */
31 private Field field = null;
32
33 /***
34 * constructs a new SerializableField
35 *
36 * @param field
37 * The field
38 */
39 public SerializableField(final Field field)
40 {
41 super();
42 this.field = field;
43 }
44
45 /***
46 * constructs a new SerializableField
47 *
48 * @param clazz
49 * the clazz this field is instance of
50 * @param fieldName
51 * the name of the field
52 * @throws NoSuchFieldException
53 * whenever the field is not defined in clazz
54 */
55 public SerializableField(final Class clazz, final String fieldName) 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
74 * the outputstream
75 * @throws IOException
76 * on IOException
77 */
78 private void writeObject(final ObjectOutputStream out) throws IOException
79 {
80 try
81 {
82 out.writeObject(this.field.getDeclaringClass());
83 out.writeObject(new FieldSignature(this.field.getName()));
84 }
85 catch (Exception exception)
86 {
87 throw new IOException(exception.getMessage());
88 }
89 }
90
91 /***
92 * reads a serializable method from stream
93 *
94 * @param in
95 * the inputstream
96 * @throws IOException
97 * on IOException
98 */
99 private void readObject(final java.io.ObjectInputStream in) throws IOException
100 {
101 try
102 {
103 Class declaringClass = (Class) in.readObject();
104 FieldSignature signature = (FieldSignature) in.readObject();
105 this.field = ClassUtil.resolveField(declaringClass, signature.getStringValue());
106 }
107 catch (Exception exception)
108 {
109 throw new IOException(exception.getMessage());
110 }
111 }
112 }