23
23
import com .google .common .annotations .GwtIncompatible ;
24
24
import com .google .common .annotations .J2ktIncompatible ;
25
25
import com .google .common .annotations .VisibleForTesting ;
26
+ import java .io .IOException ;
27
+ import java .io .ObjectInputStream ;
26
28
import java .io .Serializable ;
27
29
import java .time .Duration ;
28
30
import java .util .concurrent .TimeUnit ;
@@ -120,8 +122,7 @@ public String toString() {
120
122
121
123
@ VisibleForTesting
122
124
static class MemoizingSupplier <T extends @ Nullable Object > implements Supplier <T >, Serializable {
123
- private final Object lock =
124
- new Integer (1 ); // something serializable
125
+ private transient Object lock = new Object ();
125
126
126
127
final Supplier <T > delegate ;
127
128
transient volatile boolean initialized ;
@@ -135,6 +136,8 @@ static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T
135
136
136
137
@ Override
137
138
@ ParametricNullness
139
+ // We set the field only once (during construction or deserialization).
140
+ @ SuppressWarnings ("SynchronizeOnNonFinalField" )
138
141
public T get () {
139
142
// A 2-field variant of Double Checked Locking.
140
143
if (!initialized ) {
@@ -158,6 +161,13 @@ public String toString() {
158
161
+ ")" ;
159
162
}
160
163
164
+ @ GwtIncompatible // serialization
165
+ @ J2ktIncompatible // serialization
166
+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
167
+ in .defaultReadObject ();
168
+ lock = new Object ();
169
+ }
170
+
161
171
private static final long serialVersionUID = 0 ;
162
172
}
163
173
@@ -275,8 +285,7 @@ public String toString() {
275
285
@ SuppressWarnings ("GoodTime" ) // lots of violations
276
286
static class ExpiringMemoizingSupplier <T extends @ Nullable Object >
277
287
implements Supplier <T >, Serializable {
278
- private final Object lock =
279
- new Integer (1 ); // something serializable
288
+ private transient Object lock = new Object ();
280
289
281
290
final Supplier <T > delegate ;
282
291
final long durationNanos ;
@@ -291,6 +300,8 @@ static class ExpiringMemoizingSupplier<T extends @Nullable Object>
291
300
292
301
@ Override
293
302
@ ParametricNullness
303
+ // We set the field only once (during construction or deserialization).
304
+ @ SuppressWarnings ("SynchronizeOnNonFinalField" )
294
305
public T get () {
295
306
// Another variant of Double Checked Locking.
296
307
//
@@ -324,6 +335,13 @@ public String toString() {
324
335
return "Suppliers.memoizeWithExpiration(" + delegate + ", " + durationNanos + ", NANOS)" ;
325
336
}
326
337
338
+ @ GwtIncompatible // serialization
339
+ @ J2ktIncompatible // serialization
340
+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
341
+ in .defaultReadObject ();
342
+ lock = new Object ();
343
+ }
344
+
327
345
private static final long serialVersionUID = 0 ;
328
346
}
329
347
0 commit comments