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.Constructor;
18  import java.util.WeakHashMap;
19  
20  import net.sf.oval.internal.Log;
21  
22  /**
23   * Serializable Wrapper for java.lang.reflect.Constructor objects since they do not implement Serializable
24   *
25   * @author Sebastian Thomschke
26   */
27  public final class SerializableConstructor implements Serializable
28  {
29  	private static final Log LOG = Log.getLog(SerializableConstructor.class);
30  
31  	private static final WeakHashMap<Constructor< ? >, SerializableConstructor> CACHE = //
32  	new WeakHashMap<Constructor< ? >, SerializableConstructor>();
33  
34  	private static final long serialVersionUID = 1L;
35  
36  	public static SerializableConstructor getInstance(final Constructor< ? > constructor)
37  	{
38  		synchronized (CACHE)
39  		{
40  			SerializableConstructor sm = CACHE.get(constructor);
41  			if (sm == null)
42  			{
43  				sm = new SerializableConstructor(constructor);
44  				CACHE.put(constructor, sm);
45  			}
46  			return sm;
47  		}
48  	}
49  
50  	private transient Constructor< ? > constructor;
51  
52  	private final Class< ? > declaringClass;
53  
54  	private final Class< ? >[] parameterTypes;
55  
56  	private SerializableConstructor(final Constructor< ? > constructor)
57  	{
58  		this.constructor = constructor;
59  		parameterTypes = constructor.getParameterTypes();
60  		declaringClass = constructor.getDeclaringClass();
61  	}
62  
63  	/**
64  	 * @return the constructor
65  	 */
66  	public Constructor< ? > getConstructor()
67  	{
68  		return constructor;
69  	}
70  
71  	/**
72  	 * @return the declaringClass
73  	 */
74  	public Class< ? > getDeclaringClass()
75  	{
76  		return declaringClass;
77  	}
78  
79  	/**
80  	 * @return the parameterTypes
81  	 */
82  	public Class< ? >[] getParameterTypes()
83  	{
84  		return parameterTypes;
85  	}
86  
87  	private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
88  	{
89  		in.defaultReadObject();
90  		try
91  		{
92  			constructor = declaringClass.getDeclaredConstructor(parameterTypes);
93  		}
94  		catch (final NoSuchMethodException ex)
95  		{
96  			LOG.debug("Unexpected NoSuchMethodException occured", ex);
97  			throw new IOException(ex.getMessage());
98  		}
99  	}
100 }