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
13
14
15
16
17
18
19
20
21
22 public class SerializableConstructor implements Serializable
23 {
24
25 private Constructor<?> constructor = null;
26
27
28
29
30
31 public SerializableConstructor(final Constructor<?> constructor)
32 {
33 super();
34 this.constructor = constructor;
35 }
36
37
38
39
40
41
42
43 public SerializableConstructor(final Class<?> clazz, final Class<?>... parameterTypes) throws NoSuchMethodException
44 {
45 this.constructor = ClassUtil.resolveConstructor(clazz, parameterTypes);
46 }
47
48
49
50
51
52 public Constructor<?> deSerialize()
53 {
54 return this.constructor;
55 }
56
57
58
59
60
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
77
78
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 }