Skip to content

Commit

Permalink
Use J2ObjC's @RetainedWith annotation instead of @weak on
Browse files Browse the repository at this point in the history
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 8, 2019
1 parent 8e174e7 commit 74fc49f
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 14 deletions.
Expand Up @@ -415,7 +415,6 @@ ImmutableSet<K> createKeySet() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySetImpl extends ImmutableMapEntrySet<K, V> {
@Override
ImmutableMap<K, V> map() {
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -582,7 +581,6 @@ public ImmutableSet<Entry<K, V>> entrySet() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySet extends ImmutableMapEntrySet<K, V> {
@Override
public UnmodifiableIterator<Entry<K, V>> iterator() {
Expand Down
3 changes: 1 addition & 2 deletions guava/src/com/google/common/collect/ImmutableMap.java
Expand Up @@ -492,7 +492,6 @@ ImmutableSet<K> createKeySet() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySetImpl extends ImmutableMapEntrySet<K, V> {
@Override
ImmutableMap<K, V> map() {
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions guava/src/com/google/common/collect/ImmutableSortedMap.java
Expand Up @@ -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;
Expand Down Expand Up @@ -606,7 +605,6 @@ public ImmutableSet<Entry<K, V>> entrySet() {

@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
@WeakOuter
class EntrySet extends ImmutableMapEntrySet<K, V> {
@Override
public UnmodifiableIterator<Entry<K, V>> iterator() {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -234,7 +233,6 @@ ImmutableSet<Entry<V, K>> createEntrySet() {
return new InverseEntrySet();
}

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

0 comments on commit 74fc49f

Please sign in to comment.