Skip to content

Commit

Permalink
Mark Synchronized.java and references as J2ktIncompatible.
Browse files Browse the repository at this point in the history
Change Guava locks in remaining j2kt compatible code to J2ktCompatibleMonitor.

RELNOTES=n/a
PiperOrigin-RevId: 645331066
  • Loading branch information
stefanhaustein authored and Google Java Core Libraries committed Jun 21, 2024
1 parent b310b7e commit 1648ef7
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import java.util.ArrayList;
Expand All @@ -40,7 +41,9 @@
public class TearDownStack implements TearDownAccepter {
private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());

@GuardedBy("stack")
@VisibleForTesting final Object lock = new Object();

@GuardedBy("lock")
final LinkedList<TearDown> stack = new LinkedList<>();

private final boolean suppressThrows;
Expand All @@ -55,7 +58,7 @@ public TearDownStack(boolean suppressThrows) {

@Override
public final void addTearDown(TearDown tearDown) {
synchronized (stack) {
synchronized (lock) {
stack.addFirst(checkNotNull(tearDown));
}
}
Expand All @@ -64,7 +67,7 @@ public final void addTearDown(TearDown tearDown) {
public final void runTearDown() {
List<Throwable> exceptions = new ArrayList<>();
List<TearDown> stackCopy;
synchronized (stack) {
synchronized (lock) {
stackCopy = Lists.newArrayList(stack);
stack.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.common.testing;


import com.google.common.annotations.GwtCompatible;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -55,14 +54,18 @@
@GwtCompatible
@ElementTypesAreNonnullByDefault
public class TestLogHandler extends Handler {
private final Object lock = new Object();

/** We will keep a private list of all logged records */
private final List<LogRecord> list = new ArrayList<>();

/** Adds the most recently logged record to our list. */
@Override
public synchronized void publish(@Nullable LogRecord record) {
if (record != null) {
list.add(record);
public void publish(@Nullable LogRecord record) {
synchronized (lock) {
if (record != null) {
list.add(record);
}
}
}

Expand All @@ -72,8 +75,10 @@ public void flush() {}
@Override
public void close() {}

public synchronized void clear() {
list.clear();
public void clear() {
synchronized (lock) {
list.clear();
}
}

/** Returns a snapshot of the logged records. */
Expand All @@ -84,8 +89,10 @@ public synchronized void clear() {
* TODO(cpovirk): consider renaming this method to reflect that it takes a snapshot (and/or return
* an ImmutableList)
*/
public synchronized List<LogRecord> getStoredLogRecords() {
List<LogRecord> result = new ArrayList<>(list);
return Collections.unmodifiableList(result);
public List<LogRecord> getStoredLogRecords() {
synchronized (lock) {
List<LogRecord> result = new ArrayList<>(list);
return Collections.unmodifiableList(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private TearDownStack buildTearDownStack() {

@Override
public void tearDown() throws Exception {
synchronized (result.stack) {
synchronized (result.lock) {
assertEquals(
"The test should have cleared the stack (say, by virtue of running runTearDown)",
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public void testOrderingUnmodifiable() {
assertOrderingReadOnly(Multimaps.unmodifiableMultimap(multimap));
}

@J2ktIncompatible // Synchronized
public void testOrderingSynchronized() {
Multimap<String, Integer> multimap = initializeMultimap5();
assertOrderingReadOnly(Multimaps.synchronizedMultimap(multimap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ public void testImmutableEntryNull() {
}

/** See {@link SynchronizedBiMapTest} for more tests. */
@J2ktIncompatible // Synchronized
public void testSynchronizedBiMap() {
BiMap<String, Integer> bimap = HashBiMap.create();
bimap.put("one", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void testSerializingUnmodifiableTreeMultimap() {
}

@GwtIncompatible // slow (~10s)
@J2ktIncompatible // Synchronized
public void testUnmodifiableSynchronizedArrayListMultimap() {
checkUnmodifiableMultimap(
Multimaps.synchronizedListMultimap(
Expand All @@ -187,6 +188,7 @@ public void testSerializingUnmodifiableSynchronizedArrayListMultimap() {
}

@GwtIncompatible // slow (~10s)
@J2ktIncompatible // Synchronized
public void testUnmodifiableSynchronizedHashMultimap() {
checkUnmodifiableMultimap(
Multimaps.synchronizedSetMultimap(
Expand All @@ -207,6 +209,7 @@ public void testSerializingUnmodifiableSynchronizedHashMultimap() {
}

@GwtIncompatible // slow (~10s)
@J2ktIncompatible // Synchronized
public void testUnmodifiableSynchronizedTreeMultimap() {
TreeMultimap<String, Integer> delegate =
TreeMultimap.create(Ordering.<String>natural(), INT_COMPARATOR);
Expand Down Expand Up @@ -932,11 +935,13 @@ public String transformEntry(String key, Integer value) {
assertEquals("{a=[a1, a4, a4], b=[b6]}", transformed.toString());
}

@J2ktIncompatible // Synchronized
public void testSynchronizedMultimapSampleCodeCompilation() {
// Extra indirection for J2KT, to avoid error: not enough information to infer type variable K
this.<@Nullable Object, @Nullable Object>genericTestSynchronizedMultimapSampleCodeCompilation();
}

@J2ktIncompatible // Synchronized
private <K extends @Nullable Object, V extends @Nullable Object>
void genericTestSynchronizedMultimapSampleCodeCompilation() {
K key = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public boolean isEmpty() {
return super.isEmpty();
}

/* Don't test iterator(); it may or may not hold the mutex. */

@Override
public boolean remove(@Nullable Object o) {
assertTrue(Thread.holdsLock(mutex));
Expand Down
16 changes: 13 additions & 3 deletions android/guava/src/com/google/common/base/Suppliers.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public String toString() {

@VisibleForTesting
static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T>, Serializable {
private final Object lock =
new Integer(1); // something serializable

final Supplier<T> delegate;
transient volatile boolean initialized;
// "value" does not need to be volatile; visibility piggy-backs
Expand All @@ -136,7 +139,7 @@ static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T
public T get() {
// A 2-field variant of Double Checked Locking.
if (!initialized) {
synchronized (this) {
synchronized (lock) {
if (!initialized) {
T t = delegate.get();
value = t;
Expand All @@ -161,6 +164,8 @@ public String toString() {

@VisibleForTesting
static class NonSerializableMemoizingSupplier<T extends @Nullable Object> implements Supplier<T> {
private final Object lock = new Object();

@SuppressWarnings("UnnecessaryLambda") // Must be a fixed singleton object
private static final Supplier<Void> SUCCESSFULLY_COMPUTED =
() -> {
Expand All @@ -181,7 +186,7 @@ static class NonSerializableMemoizingSupplier<T extends @Nullable Object> implem
public T get() {
// Because Supplier is read-heavy, we use the "double-checked locking" pattern.
if (delegate != SUCCESSFULLY_COMPUTED) {
synchronized (this) {
synchronized (lock) {
if (delegate != SUCCESSFULLY_COMPUTED) {
T t = delegate.get();
value = t;
Expand Down Expand Up @@ -272,6 +277,9 @@ public String toString() {
@SuppressWarnings("GoodTime") // lots of violations
static class ExpiringMemoizingSupplier<T extends @Nullable Object>
implements Supplier<T>, Serializable {
private final Object lock =
new Integer(1); // something serializable

final Supplier<T> delegate;
final long durationNanos;
@CheckForNull transient volatile T value;
Expand All @@ -295,7 +303,7 @@ public T get() {
long nanos = expirationNanos;
long now = System.nanoTime();
if (nanos == 0 || now - nanos >= 0) {
synchronized (this) {
synchronized (lock) {
if (nanos == expirationNanos) { // recheck for lost race
T t = delegate.get();
value = t;
Expand Down Expand Up @@ -367,11 +375,13 @@ public String toString() {
* Returns a supplier whose {@code get()} method synchronizes on {@code delegate} before calling
* it, making it thread-safe.
*/
@J2ktIncompatible
public static <T extends @Nullable Object> Supplier<T> synchronizedSupplier(
Supplier<T> delegate) {
return new ThreadSafeSupplier<>(delegate);
}

@J2ktIncompatible
private static class ThreadSafeSupplier<T extends @Nullable Object>
implements Supplier<T>, Serializable {
final Supplier<T> delegate;
Expand Down
2 changes: 2 additions & 0 deletions android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,7 @@ public String toString() {
* @param bimap the bimap to be wrapped in a synchronized view
* @return a synchronized view of the specified bimap
*/
@J2ktIncompatible // Synchronized
public static <K extends @Nullable Object, V extends @Nullable Object>
BiMap<K, V> synchronizedBiMap(BiMap<K, V> bimap) {
return Synchronized.biMap(bimap, null);
Expand Down Expand Up @@ -3609,6 +3610,7 @@ public NavigableMap<K, V> tailMap(@ParametricNullness K fromKey, boolean inclusi
* @since 13.0
*/
@GwtIncompatible // NavigableMap
@J2ktIncompatible // Synchronized
public static <K extends @Nullable Object, V extends @Nullable Object>
NavigableMap<K, V> synchronizedNavigableMap(NavigableMap<K, V> navigableMap) {
return Synchronized.navigableMap(navigableMap);
Expand Down
4 changes: 4 additions & 0 deletions android/guava/src/com/google/common/collect/Multimaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ M invertFrom(Multimap<? extends V, ? extends K> source, M dest) {
* @param multimap the multimap to be wrapped in a synchronized view
* @return a synchronized view of the specified multimap
*/
@J2ktIncompatible // Synchronized
public static <K extends @Nullable Object, V extends @Nullable Object>
Multimap<K, V> synchronizedMultimap(Multimap<K, V> multimap) {
return Synchronized.multimap(multimap, null);
Expand Down Expand Up @@ -899,6 +900,7 @@ public Comparator<? super V> valueComparator() {
* @param multimap the multimap to be wrapped
* @return a synchronized view of the specified multimap
*/
@J2ktIncompatible // Synchronized
public static <K extends @Nullable Object, V extends @Nullable Object>
SetMultimap<K, V> synchronizedSetMultimap(SetMultimap<K, V> multimap) {
return Synchronized.setMultimap(multimap, null);
Expand Down Expand Up @@ -946,6 +948,7 @@ public static <K, V> SetMultimap<K, V> unmodifiableSetMultimap(
* @param multimap the multimap to be wrapped
* @return a synchronized view of the specified multimap
*/
@J2ktIncompatible // Synchronized
public static <K extends @Nullable Object, V extends @Nullable Object>
SortedSetMultimap<K, V> synchronizedSortedSetMultimap(SortedSetMultimap<K, V> multimap) {
return Synchronized.sortedSetMultimap(multimap, null);
Expand Down Expand Up @@ -978,6 +981,7 @@ SortedSetMultimap<K, V> unmodifiableSortedSetMultimap(SortedSetMultimap<K, V> de
* @param multimap the multimap to be wrapped
* @return a synchronized view of the specified multimap
*/
@J2ktIncompatible // Synchronized
public static <K extends @Nullable Object, V extends @Nullable Object>
ListMultimap<K, V> synchronizedListMultimap(ListMultimap<K, V> multimap) {
return Synchronized.listMultimap(multimap, null);
Expand Down
2 changes: 2 additions & 0 deletions android/guava/src/com/google/common/collect/Queues.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ public static <E> int drainUninterruptibly(
* @return a synchronized view of the specified queue
* @since 14.0
*/
@J2ktIncompatible // Synchronized
public static <E extends @Nullable Object> Queue<E> synchronizedQueue(Queue<E> queue) {
return Synchronized.queue(queue, null);
}
Expand Down Expand Up @@ -440,6 +441,7 @@ public static <E> int drainUninterruptibly(
* @return a synchronized view of the specified deque
* @since 15.0
*/
@J2ktIncompatible // Synchronized
public static <E extends @Nullable Object> Deque<E> synchronizedDeque(Deque<E> deque) {
return Synchronized.deque(deque, null);
}
Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/collect/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,7 @@ public NavigableSet<E> tailSet(@ParametricNullness E fromElement, boolean inclus
* @since 13.0
*/
@GwtIncompatible // NavigableSet
@J2ktIncompatible // Synchronized
public static <E extends @Nullable Object> NavigableSet<E> synchronizedNavigableSet(
NavigableSet<E> navigableSet) {
return Synchronized.navigableSet(navigableSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* @author Mike Bostock
* @author Jared Levy
*/
@J2ktIncompatible
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
/*
Expand Down
2 changes: 2 additions & 0 deletions android/guava/src/com/google/common/collect/Tables.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Supplier;
Expand Down Expand Up @@ -730,6 +731,7 @@ public Map<Object, Object> apply(Map<Object, Object> input) {
* @return a synchronized view of the specified table
* @since 22.0
*/
@J2ktIncompatible // Synchronized
public static <R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
Table<R, C, V> synchronizedTable(Table<R, C, V> table) {
return Synchronized.table(table, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
@GwtCompatible
@ElementTypesAreNonnullByDefault
final class LazyLogger {
private final Object lock = new Object();

private final String loggerName;
private volatile @Nullable Logger logger;

Expand All @@ -45,7 +47,7 @@ Logger get() {
if (local != null) {
return local;
}
synchronized (this) {
synchronized (lock) {
local = logger;
if (local != null) {
return local;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,31 @@ public class CollectionSpliteratorTester<E> extends AbstractCollectionTester<E>

@CollectionFeature.Require(absent = KNOWN_ORDER)
public void testSpliteratorUnknownOrder() {
synchronized (collection) {
SpliteratorTester.of(collection::spliterator).expect(getSampleElements());
}
SpliteratorTester.of(collection::spliterator).expect(getSampleElements());
}

@CollectionFeature.Require(KNOWN_ORDER)
public void testSpliteratorKnownOrder() {
synchronized (collection) {
SpliteratorTester.of(collection::spliterator).expect(getOrderedElements()).inOrder();
}
SpliteratorTester.of(collection::spliterator).expect(getOrderedElements()).inOrder();
}

@CollectionFeature.Require(ALLOWS_NULL_VALUES)
@CollectionSize.Require(absent = ZERO)
public void testSpliteratorNullable() {
initCollectionWithNullElement();
synchronized (collection) { // for Collections.synchronized
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.NONNULL));
}
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.NONNULL));
}

@CollectionFeature.Require(SUPPORTS_ADD)
public void testSpliteratorNotImmutable_CollectionAllowsAdd() {
// If add is supported, verify that IMMUTABLE is not reported.
synchronized (collection) { // for Collections.synchronized
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
}
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
}

@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testSpliteratorNotImmutable_CollectionAllowsRemove() {
// If remove is supported, verify that IMMUTABLE is not reported.
synchronized (collection) { // for Collections.synchronized
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
}
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
}

@J2ktIncompatible
Expand Down
Loading

0 comments on commit 1648ef7

Please sign in to comment.