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