From 852a7d3fe962f63f44b77e2a010b54e12dbd3696 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 22 May 2023 12:19:37 -0700 Subject: [PATCH] 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 --- android/guava/src/com/google/common/collect/Maps.java | 6 +++--- guava/src/com/google/common/collect/CollectCollectors.java | 6 ++++-- guava/src/com/google/common/collect/Maps.java | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/android/guava/src/com/google/common/collect/Maps.java b/android/guava/src/com/google/common/collect/Maps.java index 99042180cd68..4e9792057244 100644 --- a/android/guava/src/com/google/common/collect/Maps.java +++ b/android/guava/src/com/google/common/collect/Maps.java @@ -22,6 +22,7 @@ import static com.google.common.collect.CollectPreconditions.checkEntryNotNull; import static com.google.common.collect.CollectPreconditions.checkNonnegative; import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT; +import static java.util.Collections.singletonMap; import static java.util.Objects.requireNonNull; import com.google.common.annotations.GwtCompatible; @@ -163,9 +164,8 @@ public static , V> ImmutableMap immutableEnumMap( K key1 = entry1.getKey(); V value1 = entry1.getValue(); checkEntryNotNull(key1, value1); - Class clazz = key1.getDeclaringClass(); - EnumMap enumMap = new EnumMap<>(clazz); - enumMap.put(key1, value1); + // Do something that works for j2cl, where we can't call getDeclaredClass(): + EnumMap enumMap = new EnumMap<>(singletonMap(key1, value1)); while (entryItr.hasNext()) { Entry entry = entryItr.next(); K key = entry.getKey(); diff --git a/guava/src/com/google/common/collect/CollectCollectors.java b/guava/src/com/google/common/collect/CollectCollectors.java index ae943ba1b020..e0982ab0f7c7 100644 --- a/guava/src/com/google/common/collect/CollectCollectors.java +++ b/guava/src/com/google/common/collect/CollectCollectors.java @@ -17,6 +17,7 @@ package com.google.common.collect; import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.Collections.singletonMap; import static java.util.stream.Collectors.collectingAndThen; import com.google.common.annotations.GwtCompatible; @@ -326,9 +327,10 @@ private static class EnumMapAccumulator, V> { void put(K key, V value) { if (map == null) { - map = new EnumMap<>(key.getDeclaringClass()); + map = new EnumMap<>(singletonMap(key, value)); + } else { + map.merge(key, value, mergeFunction); } - map.merge(key, value, mergeFunction); } EnumMapAccumulator combine(EnumMapAccumulator other) { diff --git a/guava/src/com/google/common/collect/Maps.java b/guava/src/com/google/common/collect/Maps.java index 95fcae3a4b16..942e21cc8443 100644 --- a/guava/src/com/google/common/collect/Maps.java +++ b/guava/src/com/google/common/collect/Maps.java @@ -22,6 +22,7 @@ import static com.google.common.collect.CollectPreconditions.checkEntryNotNull; import static com.google.common.collect.CollectPreconditions.checkNonnegative; import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT; +import static java.util.Collections.singletonMap; import static java.util.Objects.requireNonNull; import com.google.common.annotations.GwtCompatible; @@ -170,9 +171,8 @@ public static , V> ImmutableMap immutableEnumMap( K key1 = entry1.getKey(); V value1 = entry1.getValue(); checkEntryNotNull(key1, value1); - Class clazz = key1.getDeclaringClass(); - EnumMap enumMap = new EnumMap<>(clazz); - enumMap.put(key1, value1); + // Do something that works for j2cl, where we can't call getDeclaredClass(): + EnumMap enumMap = new EnumMap<>(singletonMap(key1, value1)); while (entryItr.hasNext()) { Entry entry = entryItr.next(); K key = entry.getKey();