From 31ade173f5cc9c000040cfe34fb71330535cb863 Mon Sep 17 00:00:00 2001 From: kstanger Date: Wed, 13 Feb 2019 12:53:55 -0800 Subject: [PATCH] Use J2ObjC's @RetainedWith annotation instead of @Weak on ImmutableMap.keySet to prevent crashes on iOS. TESTED=Created a map inside a autorelease pool and saved a reference to the keyset. Checked that the keyset functions correctly outside the autorelease pool. Checked that both the keyset and the map are deallocated by the autorelease pool when not holding a reference to the keyset. Did this test with both ImmutableMap.of(<2 entries>) and ImmutableBiMap.of(<2 entries>). RELNOTES=J2ObjC: Fixes crashes from use of ImmutableMap.keySet(). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=233811320 --- android/guava/src/com/google/common/collect/ImmutableMap.java | 3 ++- .../src/com/google/common/collect/ImmutableMapKeySet.java | 3 +-- guava/src/com/google/common/collect/ImmutableMap.java | 3 ++- guava/src/com/google/common/collect/ImmutableMapKeySet.java | 3 +-- guava/src/com/google/common/collect/RegularImmutableMap.java | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/android/guava/src/com/google/common/collect/ImmutableMap.java b/android/guava/src/com/google/common/collect/ImmutableMap.java index 5c1ca77ca01f..a25c423385dc 100644 --- a/android/guava/src/com/google/common/collect/ImmutableMap.java +++ b/android/guava/src/com/google/common/collect/ImmutableMap.java @@ -25,6 +25,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.concurrent.LazyInit; +import com.google.j2objc.annotations.RetainedWith; import com.google.j2objc.annotations.WeakOuter; import java.io.Serializable; import java.util.AbstractMap; @@ -537,7 +538,7 @@ public ImmutableSet> entrySet() { abstract ImmutableSet> createEntrySet(); - @LazyInit private transient ImmutableSet keySet; + @LazyInit @RetainedWith private transient ImmutableSet keySet; /** * Returns an immutable set of the keys in this map, in the same order that they appear in {@link diff --git a/android/guava/src/com/google/common/collect/ImmutableMapKeySet.java b/android/guava/src/com/google/common/collect/ImmutableMapKeySet.java index 2b78941e7201..77babc276e5d 100644 --- a/android/guava/src/com/google/common/collect/ImmutableMapKeySet.java +++ b/android/guava/src/com/google/common/collect/ImmutableMapKeySet.java @@ -18,7 +18,6 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; -import com.google.j2objc.annotations.Weak; import java.io.Serializable; import org.checkerframework.checker.nullness.compatqual.NullableDecl; @@ -30,7 +29,7 @@ */ @GwtCompatible(emulated = true) final class ImmutableMapKeySet extends IndexedImmutableSet { - @Weak private final ImmutableMap map; + private final ImmutableMap map; ImmutableMapKeySet(ImmutableMap map) { this.map = map; diff --git a/guava/src/com/google/common/collect/ImmutableMap.java b/guava/src/com/google/common/collect/ImmutableMap.java index 00a789219a16..0f6562b92ec0 100644 --- a/guava/src/com/google/common/collect/ImmutableMap.java +++ b/guava/src/com/google/common/collect/ImmutableMap.java @@ -26,6 +26,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.concurrent.LazyInit; +import com.google.j2objc.annotations.RetainedWith; import com.google.j2objc.annotations.WeakOuter; import java.io.Serializable; import java.util.AbstractMap; @@ -718,7 +719,7 @@ public ImmutableSet> entrySet() { abstract ImmutableSet> createEntrySet(); - @LazyInit private transient ImmutableSet keySet; + @LazyInit @RetainedWith private transient ImmutableSet keySet; /** * Returns an immutable set of the keys in this map, in the same order that they appear in {@link diff --git a/guava/src/com/google/common/collect/ImmutableMapKeySet.java b/guava/src/com/google/common/collect/ImmutableMapKeySet.java index b858023b65c2..56110a87e774 100644 --- a/guava/src/com/google/common/collect/ImmutableMapKeySet.java +++ b/guava/src/com/google/common/collect/ImmutableMapKeySet.java @@ -20,7 +20,6 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; -import com.google.j2objc.annotations.Weak; import java.io.Serializable; import java.util.Spliterator; import java.util.function.Consumer; @@ -34,7 +33,7 @@ */ @GwtCompatible(emulated = true) final class ImmutableMapKeySet extends IndexedImmutableSet { - @Weak private final ImmutableMap map; + private final ImmutableMap map; ImmutableMapKeySet(ImmutableMap map) { this.map = map; diff --git a/guava/src/com/google/common/collect/RegularImmutableMap.java b/guava/src/com/google/common/collect/RegularImmutableMap.java index d8f1fff6bbd4..f78180daee9f 100644 --- a/guava/src/com/google/common/collect/RegularImmutableMap.java +++ b/guava/src/com/google/common/collect/RegularImmutableMap.java @@ -209,7 +209,7 @@ ImmutableSet createKeySet() { @GwtCompatible(emulated = true) private static final class KeySet extends IndexedImmutableSet { - @Weak private final RegularImmutableMap map; + private final RegularImmutableMap map; KeySet(RegularImmutableMap map) { this.map = map;