1 package org.djutils.event.ref;
2
3 import java.io.IOException;
4 import java.io.ObjectOutputStream;
5 import java.io.Serializable;
6 import java.lang.reflect.Field;
7
8 import org.pmw.tinylog.Logger;
9
10 /**
11 * A WeakReference. The WeakReference extends the <code>java.lang.ref.WeakReference</code> and besides implementing the
12 * Reference interface no changes are defined.
13 * <p>
14 * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
16 * distributed under a three-clause BSD-style license, which can be found at
17 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
18 * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
19 * https://simulation.tudelft.nl/dsol/manual</a>.
20 * </p>
21 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
22 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23 * @param <T> the type of the reference
24 */
25 public class WeakReference<T extends Serializable> extends Reference<T>
26 {
27 /** The default serial version UID for serializable classes. */
28 private static final long serialVersionUID = 20140830L;
29
30 /** the wrapped reference to the referent. */
31 private final transient java.lang.ref.WeakReference<T> referent;
32
33 /**
34 * Creates a new weak reference that refers to the given object. The new reference is not registered with any queue.
35 * @param referent T; object the new weak reference will refer to
36 */
37 public WeakReference(final T referent)
38 {
39 this.referent = new java.lang.ref.WeakReference<T>(referent);
40 }
41
42 /** {@inheritDoc} */
43 @Override
44 public final T get()
45 {
46 return this.referent.get();
47 }
48
49 /**
50 * Write a serializable method to a stream.
51 * @param out ObjectOutputStream; the output stream
52 * @throws IOException on IOException
53 */
54 private synchronized void writeObject(final ObjectOutputStream out) throws IOException
55 {
56 out.writeObject(this.get());
57 }
58
59 /**
60 * Read a serializable method from a stream.
61 * @param in java.io.ObjectInputStream; the input stream
62 * @throws IOException on IOException
63 * @throws ClassNotFoundException on ClassNotFoundException
64 */
65 @SuppressWarnings("unchecked")
66 private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
67 {
68 try
69 {
70 Field field = getClass().getDeclaredField("referent");
71 field.setAccessible(true);
72 field.set(this, new java.lang.ref.WeakReference<T>((T) in.readObject()));
73 field.setAccessible(false);
74 }
75 catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException exception)
76 {
77 Logger.error(exception, "Error using ReadObject on StrongReference");
78 }
79 }
80 }