1
2
3
4
5
6 package nl.tudelft.simulation.language.reflection;
7
8 import java.io.IOException;
9 import java.io.ObjectOutputStream;
10 import java.io.Serializable;
11 import java.lang.reflect.Constructor;
12
13 /***
14 * A SerializableConstructor.
15 * <p>
16 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
17 * Netherlands.
18 * <p>
19 * See for project information <a
20 * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
21 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
22 * </a>, no warranty
23 *
24 * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
25 * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:23 $
26 * @since 1.5
27 */
28 public class SerializableConstructor implements Serializable
29 {
30 /*** the constructor */
31 private Constructor<?> constructor = null;
32
33 /***
34 * constructs a new SerializableConstructor
35 *
36 * @param constructor
37 * The constructor
38 */
39 public SerializableConstructor(final Constructor<?> constructor)
40 {
41 super();
42 this.constructor = constructor;
43 }
44
45 /***
46 * constructs a new SerializableConstructor
47 *
48 * @param clazz
49 * the clazz this field is instance of
50 * @param parameterTypes
51 * the parameterTypes of the constructor
52 * @throws NoSuchMethodException
53 * whenever the method is not defined in clazz
54 */
55 @SuppressWarnings("unchecked")
56 public SerializableConstructor(final Class<?> clazz, final Class[] parameterTypes)
57 throws NoSuchMethodException
58 {
59 this.constructor = ClassUtil.resolveConstructor(clazz, parameterTypes);
60 }
61
62 /***
63 * deserializes the field
64 *
65 * @return the Constructor
66 */
67 public Constructor<?> deSerialize()
68 {
69 return this.constructor;
70 }
71
72 /***
73 * writes a serializable method to stream
74 *
75 * @param out
76 * the outputstream
77 * @throws IOException
78 * on IOException
79 */
80 private void writeObject(final ObjectOutputStream out) throws IOException
81 {
82 try
83 {
84 out.writeObject(this.constructor.getDeclaringClass());
85 out.writeObject(new MethodSignature(this.constructor));
86 }
87 catch (Exception exception)
88 {
89 throw new IOException(exception.getMessage());
90 }
91 }
92
93 /***
94 * reads a serializable method from stream
95 *
96 * @param in
97 * the inputstream
98 * @throws IOException
99 * on IOException
100 */
101 private void readObject(final java.io.ObjectInputStream in) throws IOException
102 {
103 try
104 {
105 Class<?> declaringClass = (Class) in.readObject();
106 MethodSignature signature = (MethodSignature) in.readObject();
107 this.constructor = ClassUtil.resolveConstructor(declaringClass, signature.getParameterTypes());
108 }
109 catch (Exception exception)
110 {
111 throw new IOException(exception.getMessage());
112 }
113 }
114 }