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