Skip to content
Permalink
Browse files

Use J2ObjC's @RetainedWith annotation instead of @weak on

ImmutableMap.entrySet() to prevent crashes on iOS.

TESTED=Created a map inside an autorelease pool and saved a reference to the
entry set. Checked that the entry set functions correctly outside the
autorelease pool. Checked that both the entry set and the are deallocated by the
autorelease pool when not holding a strong reference to the entry set. Did this
test with the following constructions to cover all entry set implementations:
- ImmutableMap.of(<2 entries>)
- ImmutableMap.copyOf(<EnumMap with 2 entries>)
- ImmutableSortedMap.of(<2 entries>)
- ImmutableBiMap.of(<2 entries>).inverse()

RELNOTES=J2ObjC: Fixes crashes from use of ImmutableMap.entrySet().

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=237300208
  • Loading branch information...
kstanger authored and ronshapiro committed Mar 7, 2019
1 parent 8e174e7 commit 74fc49faf283f106302794f7af82c7ab1fcb5412
@@ -415,7 +415,6 @@ void sortEntries() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySetImpl extends ImmutableMapEntrySet<K, V> {
@Override
ImmutableMap<K, V> map() {
@@ -524,7 +523,7 @@ public final V getOrDefault(@NullableDecl Object key, @NullableDecl V defaultVal
return (result != null) ? result : defaultValue;
}

@LazyInit private transient ImmutableSet<Entry<K, V>> entrySet;
@LazyInit @RetainedWith private transient ImmutableSet<Entry<K, V>> entrySet;

/**
* Returns an immutable set of the mappings in this map. The iteration order is specified by the
@@ -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 java.util.Map.Entry;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
@@ -32,7 +31,7 @@
@GwtCompatible(emulated = true)
abstract class ImmutableMapEntrySet<K, V> extends ImmutableSet<Entry<K, V>> {
static final class RegularEntrySet<K, V> extends ImmutableMapEntrySet<K, V> {
@Weak private final transient ImmutableMap<K, V> map;
private final transient ImmutableMap<K, V> map;
private final transient ImmutableList<Entry<K, V>> entries;

RegularEntrySet(ImmutableMap<K, V> map, Entry<K, V>[] entries) {
@@ -24,7 +24,6 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.j2objc.annotations.WeakOuter;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Comparator;
@@ -582,7 +581,6 @@ boolean isPartialView() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySet extends ImmutableMapEntrySet<K, V> {
@Override
public UnmodifiableIterator<Entry<K, V>> iterator() {
@@ -492,7 +492,6 @@ private void ensureCapacity(int minCapacity) {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySetImpl extends ImmutableMapEntrySet<K, V> {
@Override
ImmutableMap<K, V> map() {
@@ -705,7 +704,7 @@ public final V getOrDefault(@Nullable Object key, @Nullable V defaultValue) {
return (result != null) ? result : defaultValue;
}

@LazyInit private transient ImmutableSet<Entry<K, V>> entrySet;
@LazyInit @RetainedWith private transient ImmutableSet<Entry<K, V>> entrySet;

/**
* Returns an immutable set of the mappings in this map. The iteration order is specified by the
@@ -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 java.util.Map.Entry;
import java.util.Spliterator;
@@ -34,7 +33,7 @@
@GwtCompatible(emulated = true)
abstract class ImmutableMapEntrySet<K, V> extends ImmutableSet<Entry<K, V>> {
static final class RegularEntrySet<K, V> extends ImmutableMapEntrySet<K, V> {
@Weak private final transient ImmutableMap<K, V> map;
private final transient ImmutableMap<K, V> map;
private final transient ImmutableList<Entry<K, V>> entries;

RegularEntrySet(ImmutableMap<K, V> map, Entry<K, V>[] entries) {
@@ -24,7 +24,6 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.j2objc.annotations.WeakOuter;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Comparator;
@@ -606,7 +605,6 @@ boolean isPartialView() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySet extends ImmutableMapEntrySet<K, V> {
@Override
public UnmodifiableIterator<Entry<K, V>> iterator() {
@@ -28,7 +28,6 @@
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.function.BiConsumer;
import java.util.function.Consumer;
@@ -234,7 +233,6 @@ public K get(@Nullable Object value) {
return new InverseEntrySet();
}

@WeakOuter
final class InverseEntrySet extends ImmutableMapEntrySet<V, K> {
@Override
ImmutableMap<V, K> map() {

0 comments on commit 74fc49f

Please sign in to comment.
You can’t perform that action at this time.