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.Constructor;
7   
8   import org.djutils.reflection.ClassUtil;
9   import org.djutils.reflection.MethodSignature;
10  
11  /**
12   * A SerializableConstructor.
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   */
21  public class SerializableConstructor implements Serializable
22  {
23      /** the constructor. */
24      private Constructor<?> constructor = null;
25  
26      /**
27       * constructs a new SerializableConstructor.
28       * @param constructor Constructor&lt;?&gt;; The constructor
29       */
30      public SerializableConstructor(final Constructor<?> constructor)
31      {
32          super();
33          this.constructor = constructor;
34      }
35  
36      /**
37       * constructs a new SerializableConstructor.
38       * @param clazz Class&lt;?&gt;; the clazz this field is instance of
39       * @param parameterTypes Class&lt;?&gt;...; the parameterTypes of the constructor
40       * @throws NoSuchMethodException whenever the method is not defined in clazz
41       */
42      public SerializableConstructor(final Class<?> clazz, final Class<?>... parameterTypes) throws NoSuchMethodException
43      {
44          this.constructor = ClassUtil.resolveConstructor(clazz, parameterTypes);
45      }
46  
47      /**
48       * deserializes the field.
49       * @return the Constructor
50       */
51      public Constructor<?> deSerialize()
52      {
53          return this.constructor;
54      }
55  
56      /**
57       * writes a serializable method to stream.
58       * @param out ObjectOutputStream; the outputstream
59       * @throws IOException on IOException
60       */
61      private void writeObject(final ObjectOutputStream out) throws IOException
62      {
63          try
64          {
65              out.writeObject(this.constructor.getDeclaringClass());
66              out.writeObject(new MethodSignature(this.constructor));
67          }
68          catch (Exception exception)
69          {
70              throw new IOException(exception.getMessage());
71          }
72      }
73  
74      /**
75       * reads a serializable method from stream.
76       * @param in java.io.ObjectInputStream; the inputstream
77       * @throws IOException on IOException
78       */
79      private void readObject(final java.io.ObjectInputStream in) throws IOException
80      {
81          try
82          {
83              Class<?> declaringClass = (Class<?>) in.readObject();
84              MethodSignature signature = (MethodSignature) in.readObject();
85              this.constructor = ClassUtil.resolveConstructor(declaringClass, signature.getParameterTypes());
86          }
87          catch (Exception exception)
88          {
89              throw new IOException(exception.getMessage());
90          }
91      }
92  }