View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2015 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.internal.util;
14  
15  import java.io.IOException;
16  import java.io.Serializable;
17  import java.lang.reflect.Method;
18  import java.util.WeakHashMap;
19  
20  import net.sf.oval.internal.Log;
21  
22  /**
23   * Serializable Wrapper for java.lang.reflect.Method objects since they do not implement Serializable
24   *
25   * @author Sebastian Thomschke
26   */
27  public final class SerializableMethod implements Serializable
28  {
29  	private static final Log LOG = Log.getLog(SerializableMethod.class);
30  
31  	private static final WeakHashMap<Method, SerializableMethod> CACHE = new WeakHashMap<Method, SerializableMethod>();
32  
33  	private static final long serialVersionUID = 1L;
34  
35  	public static SerializableMethod getInstance(final Method method)
36  	{
37  		synchronized (CACHE)
38  		{
39  			SerializableMethod sm = CACHE.get(method);
40  			if (sm == null)
41  			{
42  				sm = new SerializableMethod(method);
43  				CACHE.put(method, sm);
44  			}
45  			return sm;
46  		}
47  	}
48  
49  	private final Class< ? > declaringClass;
50  	private transient Method method;
51  	private final String name;
52  
53  	private final Class< ? >[] parameterTypes;
54  
55  	private SerializableMethod(final Method method)
56  	{
57  		this.method = method;
58  		name = method.getName();
59  		parameterTypes = method.getParameterTypes();
60  		declaringClass = method.getDeclaringClass();
61  	}
62  
63  	/**
64  	 * @return the declaringClass
65  	 */
66  	public Class< ? > getDeclaringClass()
67  	{
68  		return declaringClass;
69  	}
70  
71  	/**
72  	 * @return the method
73  	 */
74  	public Method getMethod()
75  	{
76  		return method;
77  	}
78  
79  	/**
80  	 * @return the name
81  	 */
82  	public String getName()
83  	{
84  		return name;
85  	}
86  
87  	/**
88  	 * @return the parameterTypes
89  	 */
90  	public Class< ? >[] getParameterTypes()
91  	{
92  		return parameterTypes;
93  	}
94  
95  	private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
96  	{
97  		in.defaultReadObject();
98  		try
99  		{
100 			method = declaringClass.getDeclaredMethod(name, parameterTypes);
101 		}
102 		catch (final NoSuchMethodException ex)
103 		{
104 			LOG.debug("Unexpected NoSuchMethodException occured.", ex);
105 			throw new IOException(ex.getMessage());
106 		}
107 	}
108 }