View Javadoc
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.Method;
7   
8   import org.djutils.reflection.ClassUtil;
9   import org.djutils.reflection.MethodSignature;
10  
11  /**
12   * A SerializableMethod.
13   * <p>
14   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
16   * project is distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
18   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
19   * </p>
20   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
21   */
22  public class SerializableMethod implements Serializable
23  {
24      /** the method to use. */
25      private Method method = null;
26  
27      /**
28       * constructs a new SerializableMethod.
29       * @param method Method; the method
30       */
31      public SerializableMethod(final Method method)
32      {
33          super();
34          this.method = method;
35      }
36  
37      /**
38       * constructs a new SerializableMethod.
39       * @param clazz Class&lt;?&gt;; the clazz this field is instance of
40       * @param methodName String; the name of the method
41       * @param parameterTypes Class&lt;?&gt;..; The parameterTypes of the method
42       * @throws NoSuchMethodException whenever the method is not defined in clazz
43       */
44      public SerializableMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes)
45              throws NoSuchMethodException
46      {
47          this.method = ClassUtil.resolveMethod(clazz, methodName, parameterTypes);
48      }
49  
50      /**
51       * deserializes the field.
52       * @return the Field
53       */
54      public Method deSerialize()
55      {
56          return this.method;
57      }
58  
59      /**
60       * writes a serializable method to stream.
61       * @param out ObjectOutputStream; the outputstream
62       * @throws IOException on IOException
63       */
64      private void writeObject(final ObjectOutputStream out) throws IOException
65      {
66          try
67          {
68              out.writeObject(this.method.getDeclaringClass());
69              out.writeObject(this.method.getName());
70              out.writeObject(new MethodSignature(this.method));
71          }
72          catch (Exception exception)
73          {
74              throw new IOException(exception.getMessage());
75          }
76      }
77  
78      /**
79       * reads a serializable method from stream.
80       * @param in java.io.ObjectInputStream; the inputstream
81       * @throws IOException on IOException
82       */
83      private void readObject(final java.io.ObjectInputStream in) throws IOException
84      {
85          try
86          {
87              Class<?> declaringClass = (Class<?>) in.readObject();
88              String methodName = (String) in.readObject();
89              MethodSignature signature = (MethodSignature) in.readObject();
90              this.method = ClassUtil.resolveMethod(declaringClass, methodName, signature.getParameterTypes());
91          }
92          catch (Exception exception)
93          {
94              throw new IOException(exception.getMessage());
95          }
96      }
97  }