Skip to content

Commit 5f0e886

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Make lock objects transient.
This let us move away from the current hack (which was never in a Guava _release_) of using a lock of `new Integer(1)` (because we needed something `Serializable` while we figured this all out). RELNOTES=n/a PiperOrigin-RevId: 659714359
1 parent 641b4c5 commit 5f0e886

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

android/guava/src/com/google/common/base/Suppliers.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.google.common.annotations.GwtIncompatible;
2424
import com.google.common.annotations.J2ktIncompatible;
2525
import com.google.common.annotations.VisibleForTesting;
26+
import java.io.IOException;
27+
import java.io.ObjectInputStream;
2628
import java.io.Serializable;
2729
import java.time.Duration;
2830
import java.util.concurrent.TimeUnit;
@@ -120,8 +122,7 @@ public String toString() {
120122

121123
@VisibleForTesting
122124
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();
125126

126127
final Supplier<T> delegate;
127128
transient volatile boolean initialized;
@@ -135,6 +136,8 @@ static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T
135136

136137
@Override
137138
@ParametricNullness
139+
// We set the field only once (during construction or deserialization).
140+
@SuppressWarnings("SynchronizeOnNonFinalField")
138141
public T get() {
139142
// A 2-field variant of Double Checked Locking.
140143
if (!initialized) {
@@ -158,6 +161,13 @@ public String toString() {
158161
+ ")";
159162
}
160163

164+
@GwtIncompatible // serialization
165+
@J2ktIncompatible // serialization
166+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
167+
in.defaultReadObject();
168+
lock = new Object();
169+
}
170+
161171
private static final long serialVersionUID = 0;
162172
}
163173

@@ -275,8 +285,7 @@ public String toString() {
275285
@SuppressWarnings("GoodTime") // lots of violations
276286
static class ExpiringMemoizingSupplier<T extends @Nullable Object>
277287
implements Supplier<T>, Serializable {
278-
private final Object lock =
279-
new Integer(1); // something serializable
288+
private transient Object lock = new Object();
280289

281290
final Supplier<T> delegate;
282291
final long durationNanos;
@@ -291,6 +300,8 @@ static class ExpiringMemoizingSupplier<T extends @Nullable Object>
291300

292301
@Override
293302
@ParametricNullness
303+
// We set the field only once (during construction or deserialization).
304+
@SuppressWarnings("SynchronizeOnNonFinalField")
294305
public T get() {
295306
// Another variant of Double Checked Locking.
296307
//
@@ -324,6 +335,13 @@ public String toString() {
324335
return "Suppliers.memoizeWithExpiration(" + delegate + ", " + durationNanos + ", NANOS)";
325336
}
326337

338+
@GwtIncompatible // serialization
339+
@J2ktIncompatible // serialization
340+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
341+
in.defaultReadObject();
342+
lock = new Object();
343+
}
344+
327345
private static final long serialVersionUID = 0;
328346
}
329347

guava/src/com/google/common/base/Suppliers.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.google.common.annotations.GwtIncompatible;
2424
import com.google.common.annotations.J2ktIncompatible;
2525
import com.google.common.annotations.VisibleForTesting;
26+
import java.io.IOException;
27+
import java.io.ObjectInputStream;
2628
import java.io.Serializable;
2729
import java.time.Duration;
2830
import java.util.concurrent.TimeUnit;
@@ -120,8 +122,7 @@ public String toString() {
120122

121123
@VisibleForTesting
122124
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();
125126

126127
final Supplier<T> delegate;
127128
transient volatile boolean initialized;
@@ -135,6 +136,8 @@ static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T
135136

136137
@Override
137138
@ParametricNullness
139+
// We set the field only once (during construction or deserialization).
140+
@SuppressWarnings("SynchronizeOnNonFinalField")
138141
public T get() {
139142
// A 2-field variant of Double Checked Locking.
140143
if (!initialized) {
@@ -158,6 +161,13 @@ public String toString() {
158161
+ ")";
159162
}
160163

164+
@GwtIncompatible // serialization
165+
@J2ktIncompatible // serialization
166+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
167+
in.defaultReadObject();
168+
lock = new Object();
169+
}
170+
161171
private static final long serialVersionUID = 0;
162172
}
163173

@@ -275,8 +285,7 @@ public String toString() {
275285
@SuppressWarnings("GoodTime") // lots of violations
276286
static class ExpiringMemoizingSupplier<T extends @Nullable Object>
277287
implements Supplier<T>, Serializable {
278-
private final Object lock =
279-
new Integer(1); // something serializable
288+
private transient Object lock = new Object();
280289

281290
final Supplier<T> delegate;
282291
final long durationNanos;
@@ -291,6 +300,8 @@ static class ExpiringMemoizingSupplier<T extends @Nullable Object>
291300

292301
@Override
293302
@ParametricNullness
303+
// We set the field only once (during construction or deserialization).
304+
@SuppressWarnings("SynchronizeOnNonFinalField")
294305
public T get() {
295306
// Another variant of Double Checked Locking.
296307
//
@@ -324,6 +335,13 @@ public String toString() {
324335
return "Suppliers.memoizeWithExpiration(" + delegate + ", " + durationNanos + ", NANOS)";
325336
}
326337

338+
@GwtIncompatible // serialization
339+
@J2ktIncompatible // serialization
340+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
341+
in.defaultReadObject();
342+
lock = new Object();
343+
}
344+
327345
private static final long serialVersionUID = 0;
328346
}
329347

0 commit comments

Comments
 (0)