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-2023 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 */
22 public class SerializableConstructor implements Serializable
23 {
24 /** the constructor. */
25 private Constructor<?> constructor = null;
26
27 /**
28 * constructs a new SerializableConstructor.
29 * @param constructor Constructor<?>; The constructor
30 */
31 public SerializableConstructor(final Constructor<?> constructor)
32 {
33 super();
34 this.constructor = constructor;
35 }
36
37 /**
38 * constructs a new SerializableConstructor.
39 * @param clazz Class<?>; the clazz this field is instance of
40 * @param parameterTypes Class<?>...; the parameterTypes of the constructor
41 * @throws NoSuchMethodException whenever the method is not defined in clazz
42 */
43 public SerializableConstructor(final Class<?> clazz, final Class<?>... parameterTypes) throws NoSuchMethodException
44 {
45 this.constructor = ClassUtil.resolveConstructor(clazz, parameterTypes);
46 }
47
48 /**
49 * deserializes the field.
50 * @return the Constructor
51 */
52 public Constructor<?> deSerialize()
53 {
54 return this.constructor;
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.constructor.getDeclaringClass());
67 out.writeObject(new MethodSignature(this.constructor));
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 MethodSignature signature = (MethodSignature) in.readObject();
86 this.constructor = ClassUtil.resolveConstructor(declaringClass, signature.getParameterTypes());
87 }
88 catch (Exception exception)
89 {
90 throw new IOException(exception.getMessage());
91 }
92 }
93 }