Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/rmi/transport/WeakRef.java
+++ new/src/share/classes/sun/rmi/transport/WeakRef.java
1 1 /*
2 2 * Copyright (c) 1996, 2001, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package sun.rmi.transport;
26 26
27 27 import java.lang.ref.*;
28 28 import sun.rmi.runtime.Log;
29 29
30 30 /**
31 31 * WeakRef objects are used by the RMI runtime to hold potentially weak
32 32 * references to exported remote objects in the local object table.
33 33 *
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
34 34 * This class extends the functionality of java.lang.ref.WeakReference in
35 35 * several ways. The methods pin() and unpin() can be used to set
36 36 * whether the contained reference is strong or weak (it is weak upon
37 37 * construction). The hashCode() and equals() methods are overridden so
38 38 * that WeakRef objects hash and compare to each other according to the
39 39 * object identity of their referents.
40 40 *
41 41 * @author Ann Wollrath
42 42 * @author Peter Jones
43 43 */
44 -class WeakRef extends WeakReference {
44 +class WeakRef extends WeakReference<Object> {
45 45
46 46 /** value of the referent's "identity" hash code */
47 47 private int hashValue;
48 48
49 49 /** strong reference to the referent, for when this WeakRef is "pinned" */
50 50 private Object strongRef = null;
51 51
52 52 /**
53 53 * Create a new WeakRef to the given object.
54 54 */
55 55 public WeakRef(Object obj) {
56 56 super(obj);
57 57 setHashValue(obj); // cache object's "identity" hash code
58 58 }
59 59
60 60 /**
61 61 * Create a new WeakRef to the given object, registered with a queue.
62 62 */
63 - public WeakRef(Object obj, ReferenceQueue q) {
63 + public WeakRef(Object obj, ReferenceQueue<Object> q) {
64 64 super(obj, q);
65 65 setHashValue(obj); // cache object's "identity" hash code
66 66 }
67 67
68 68 /**
69 69 * Pin the contained reference (make this a strong reference).
70 70 */
71 71 public synchronized void pin() {
72 72 if (strongRef == null) {
73 73 strongRef = get();
74 74
75 75 if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
76 76 DGCImpl.dgcLog.log(Log.VERBOSE,
77 77 "strongRef = " + strongRef);
78 78 }
79 79 }
80 80 }
81 81
82 82 /**
83 83 * Unpin the contained reference (make this a weak reference).
84 84 */
85 85 public synchronized void unpin() {
86 86 if (strongRef != null) {
87 87 if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
88 88 DGCImpl.dgcLog.log(Log.VERBOSE,
89 89 "strongRef = " + strongRef);
90 90 }
91 91
92 92 strongRef = null;
93 93 }
94 94 }
95 95
96 96 /*
97 97 * Cache referent's "identity" hash code (so that we still have the
98 98 * value after the referent gets cleared).
99 99 *
100 100 * We cannot use the value from the object's hashCode() method, since
101 101 * if the object is of a remote class not extended from RemoteObject
102 102 * and it is trying to implement hashCode() and equals() so that it
103 103 * can be compared to stub objects, its own hash code could not have
104 104 * been initialized yet (see bugid 4102938). Also, object table keys
105 105 * based on server objects are indeed matched on object identity, so
106 106 * this is the correct hash technique regardless.
107 107 */
108 108 private void setHashValue(Object obj) {
109 109 if (obj != null) {
110 110 hashValue = System.identityHashCode(obj);
111 111 } else {
112 112 hashValue = 0;
113 113 }
114 114 }
115 115
116 116 /**
117 117 * Always return the "identity" hash code of the original referent.
118 118 */
119 119 public int hashCode() {
120 120 return hashValue;
121 121 }
122 122
123 123 /**
124 124 * Return true if "obj" is this identical WeakRef object, or, if the
125 125 * contained reference has not been cleared, if "obj" is another WeakRef
126 126 * object with the identical non-null referent. Otherwise, return false.
127 127 */
128 128 public boolean equals(Object obj) {
129 129 if (obj instanceof WeakRef) {
130 130 if (obj == this)
131 131 return true;
132 132
133 133 Object referent = get();
134 134 return (referent != null) && (referent == ((WeakRef) obj).get());
135 135 } else {
136 136 return false;
137 137 }
138 138 }
139 139 }
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX