View Javadoc
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   * A SerializableField.
13   * <p>
14   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
16   * project is distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
18   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
19   * </p>
20   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
21   * @since 1.5
22   */
23  public class SerializableField implements Serializable
24  {
25      /** the field. */
26      private Field field = null;
27  
28      /**
29       * constructs a new SerializableField.
30       * @param field Field; The field
31       */
32      public SerializableField(final Field field)
33      {
34          super();
35          this.field = field;
36      }
37  
38      /**
39       * constructs a new SerializableField.
40       * @param clazz Class&lt;?&gt;; the clazz this field is instance of
41       * @param fieldName String; the name of the field
42       * @throws NoSuchFieldException whenever the field is not defined in clazz
43       */
44      public SerializableField(final Class<?> clazz, final String fieldName) throws NoSuchFieldException
45      {
46          this.field = ClassUtil.resolveField(clazz, fieldName);
47      }
48  
49      /**
50       * deserializes the field.
51       * @return the Field
52       */
53      public Field deSerialize()
54      {
55          return this.field;
56      }
57  
58      /**
59       * writes a serializable method to stream.
60       * @param out ObjectOutputStream; the outputstream
61       * @throws IOException on IOException
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       * reads a serializable method from stream.
78       * @param in java.io.ObjectInputStream; the inputstream
79       * @throws IOException on IOException
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  }