01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.internal.util;
13:
14: import java.io.IOException;
15: import java.io.Serializable;
16: import java.lang.reflect.Constructor;
17: import java.util.WeakHashMap;
18:
19: import net.sf.oval.internal.Log;
20:
21: /**
22: * Serializable Wrapper for java.lang.reflect.Constructor objects since they do not implement Serializable
23: *
24: * @author Sebastian Thomschke
25: */
26: public class SerializableConstructor implements Serializable {
27: private final static Log LOG = Log
28: .getLog(SerializableConstructor.class);
29:
30: private static final WeakHashMap<Constructor, SerializableConstructor> CACHE = new WeakHashMap<Constructor, SerializableConstructor>();
31:
32: private static final long serialVersionUID = 1L;
33:
34: public static SerializableConstructor getInstance(
35: final Constructor constructor) {
36: /*
37: * intentionally the following code is not synchronized
38: */
39: SerializableConstructor sm = CACHE.get(constructor);
40: if (sm == null) {
41: sm = new SerializableConstructor(constructor);
42: CACHE.put(constructor, sm);
43: }
44: return sm;
45: }
46:
47: private transient Constructor constructor;
48:
49: private final Class<?> declaringClass;
50:
51: private final Class<?>[] parameterTypes;
52:
53: protected SerializableConstructor(final Constructor constructor) {
54: this .constructor = constructor;
55: parameterTypes = constructor.getParameterTypes();
56: declaringClass = constructor.getDeclaringClass();
57: }
58:
59: /**
60: * @return the constructor
61: */
62: public Constructor getConstructor() {
63: return constructor;
64: }
65:
66: /**
67: * @return the declaringClass
68: */
69: public Class<?> getDeclaringClass() {
70: return declaringClass;
71: }
72:
73: /**
74: * @return the parameterTypes
75: */
76: public Class<?>[] getParameterTypes() {
77: return parameterTypes;
78: }
79:
80: private void readObject(final java.io.ObjectInputStream in)
81: throws IOException, ClassNotFoundException {
82: in.defaultReadObject();
83: try {
84: constructor = declaringClass.getConstructor(parameterTypes);
85: } catch (final NoSuchMethodException ex) {
86: LOG.debug("Unexpected NoSuchMethodException occured", ex);
87: throw new IOException(ex.getMessage());
88: }
89: }
90: }
|