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