View Javadoc

1   /*
2    * @(#) SerializableMethod.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.Method;
16  
17  /***
18   * A SerializableMethod <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.4 March 24, 2004
29   * @since 1.3
30   */
31  public class SerializableMethod implements Serializable
32  {
33  	/*** the method to use */
34  	private Method method = null;
35  
36  	/***
37  	 * constructs a new SerializableMethod
38  	 * 
39  	 * @param method the method
40  	 */
41  	public SerializableMethod(final Method method)
42  	{
43  		super();
44  		this.method = method;
45  	}
46  
47  	/***
48  	 * constructs a new SerializableMethod
49  	 * 
50  	 * @param clazz the clazz this field is instance of
51  	 * @param methodName the name of the method
52  	 * @param parameterTypes The parameterTypes of the method
53  	 * @throws NoSuchMethodException whenever the method is not defined in clazz
54  	 */
55  	public SerializableMethod(final Class clazz, final String methodName,
56  			final Class[] parameterTypes) throws NoSuchMethodException
57  	{
58  		this.method = ClassUtil
59  				.resolveMethod(clazz, methodName, parameterTypes);
60  	}
61  
62  	/***
63  	 * deserializes the field
64  	 * 
65  	 * @return the Field
66  	 */
67  	public Method deSerialize()
68  	{
69  		return this.method;
70  	}
71  
72  	/***
73  	 * writes a serializable method to stream
74  	 * 
75  	 * @param out the outputstream
76  	 * @throws IOException on IOException
77  	 */
78  	private void writeObject(final ObjectOutputStream out) throws IOException
79  	{
80  		try
81  		{
82  			out.writeObject(this.method.getDeclaringClass());
83  			out.writeObject(this.method.getName());
84  			out.writeObject(new MethodSignature(this.method));
85  		} catch (Exception exception)
86  		{
87  			throw new IOException(exception.getMessage());
88  		}
89  	}
90  
91  	/***
92  	 * reads a serializable method from stream
93  	 * 
94  	 * @param in the inputstream
95  	 * @throws IOException on IOException
96  	 */
97  	private void readObject(final java.io.ObjectInputStream in)
98  			throws IOException
99  	{
100 		try
101 		{
102 			Class declaringClass = (Class) in.readObject();
103 			String methodName = (String) in.readObject();
104 			MethodSignature signature = (MethodSignature) in.readObject();
105 			this.method = ClassUtil.resolveMethod(declaringClass, methodName,
106 					signature.getParameterTypes());
107 		} catch (Exception exception)
108 		{
109 			throw new IOException(exception.getMessage());
110 		}
111 	}
112 }