Skip to content

Commit 852a7d3

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedMay 22, 2023
Under j2cl, make Maps.immutableEnumMap and toImmutableEnumMap work. Previously, they were present but failed at runtime.
RELNOTES=`collect`: Under j2cl, made `Maps.immutableEnumMap` and `toImmutableEnumMap` work. PiperOrigin-RevId: 534145451
1 parent 56dc928 commit 852a7d3

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed
 

‎android/guava/src/com/google/common/collect/Maps.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
2323
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
2424
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
25+
import static java.util.Collections.singletonMap;
2526
import static java.util.Objects.requireNonNull;
2627

2728
import com.google.common.annotations.GwtCompatible;
@@ -163,9 +164,8 @@ public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
163164
K key1 = entry1.getKey();
164165
V value1 = entry1.getValue();
165166
checkEntryNotNull(key1, value1);
166-
Class<K> clazz = key1.getDeclaringClass();
167-
EnumMap<K, V> enumMap = new EnumMap<>(clazz);
168-
enumMap.put(key1, value1);
167+
// Do something that works for j2cl, where we can't call getDeclaredClass():
168+
EnumMap<K, V> enumMap = new EnumMap<>(singletonMap(key1, value1));
169169
while (entryItr.hasNext()) {
170170
Entry<K, ? extends V> entry = entryItr.next();
171171
K key = entry.getKey();

‎guava/src/com/google/common/collect/CollectCollectors.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.common.collect;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static java.util.Collections.singletonMap;
2021
import static java.util.stream.Collectors.collectingAndThen;
2122

2223
import com.google.common.annotations.GwtCompatible;
@@ -326,9 +327,10 @@ private static class EnumMapAccumulator<K extends Enum<K>, V> {
326327

327328
void put(K key, V value) {
328329
if (map == null) {
329-
map = new EnumMap<>(key.getDeclaringClass());
330+
map = new EnumMap<>(singletonMap(key, value));
331+
} else {
332+
map.merge(key, value, mergeFunction);
330333
}
331-
map.merge(key, value, mergeFunction);
332334
}
333335

334336
EnumMapAccumulator<K, V> combine(EnumMapAccumulator<K, V> other) {

‎guava/src/com/google/common/collect/Maps.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
2323
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
2424
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
25+
import static java.util.Collections.singletonMap;
2526
import static java.util.Objects.requireNonNull;
2627

2728
import com.google.common.annotations.GwtCompatible;
@@ -170,9 +171,8 @@ public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
170171
K key1 = entry1.getKey();
171172
V value1 = entry1.getValue();
172173
checkEntryNotNull(key1, value1);
173-
Class<K> clazz = key1.getDeclaringClass();
174-
EnumMap<K, V> enumMap = new EnumMap<>(clazz);
175-
enumMap.put(key1, value1);
174+
// Do something that works for j2cl, where we can't call getDeclaredClass():
175+
EnumMap<K, V> enumMap = new EnumMap<>(singletonMap(key1, value1));
176176
while (entryItr.hasNext()) {
177177
Entry<K, ? extends V> entry = entryItr.next();
178178
K key = entry.getKey();

0 commit comments

Comments
 (0)
Please sign in to comment.