View Javadoc

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