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