View Javadoc

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