Skip to content

Commit

Permalink
Update code for forthcoming nullness annotations for JDK classes.
Browse files Browse the repository at this point in the history
The Guava changes include further annotations [from "On Leveraging Tests to Infer Nullable Annotations."](google/guava#6510)

PiperOrigin-RevId: 559605577
  • Loading branch information
cpovirk authored and Copybara-Service committed Aug 24, 2023
1 parent 4a92e96 commit ef39af3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
12 changes: 6 additions & 6 deletions j2kt/jre/java/common/javaemul/lang/JavaMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ interface JavaMap<K, V> : MutableMap<K, V> {
// TODO(b/243046587): Rewrite to handle case in which t is not mutable
override fun putAll(t: Map<out K, V>) = java_putAll(t as MutableMap<K, V>)

fun java_compute(key: K, remappingFunction: BiFunction<in K, in V?, out V>): V? =
fun java_compute(key: K, remappingFunction: BiFunction<in K, in V?, out V?>): V? =
default_compute(key, remappingFunction)

fun java_computeIfAbsent(key: K, mappingFunction: Function<in K, out V>): V =
default_computeIfAbsent(key, mappingFunction)

fun java_computeIfPresent(key: K, remappingFunction: BiFunction<in K, in V, out V>): V? =
fun java_computeIfPresent(key: K, remappingFunction: BiFunction<in K, in V, out V?>): V? =
default_computeIfPresent(key, remappingFunction)

fun java_containsKey(key: Any?): Boolean
Expand Down Expand Up @@ -97,7 +97,7 @@ fun <K, V> MutableMap<K, V>.java_remove(key: Any?): V? = remove(key as K)

fun <K, V> MutableMap<K, V>.java_compute(
key: K,
mappingFunction: BiFunction<in K, in V?, out V>
mappingFunction: BiFunction<in K, in V?, out V?>
): V? =
if (this is JavaMap) java_compute(key, mappingFunction) else default_compute(key, mappingFunction)

Expand All @@ -110,7 +110,7 @@ fun <K, V> MutableMap<K, V>.java_computeIfAbsent(

fun <K, V> MutableMap<K, V>.java_computeIfPresent(
key: K,
mappingFunction: BiFunction<in K, in V, out V>
mappingFunction: BiFunction<in K, in V, out V?>
): V? =
if (this is JavaMap) java_computeIfPresent(key, mappingFunction)
else default_computeIfPresent(key, mappingFunction)
Expand Down Expand Up @@ -150,7 +150,7 @@ private fun <K, V> MutableMap<K, V>.default_forEach(action: BiConsumer<in K, in

private fun <K, V> MutableMap<K, V>.default_compute(
key: K,
remappingFunction: BiFunction<in K, in V?, out V>
remappingFunction: BiFunction<in K, in V?, out V?>
): V? {
val oldValue = this[key]
val newValue = remappingFunction.apply(key, oldValue)
Expand Down Expand Up @@ -184,7 +184,7 @@ private fun <K, V> MutableMap<K, V>.default_computeIfAbsent(

private fun <K, V> MutableMap<K, V>.default_computeIfPresent(
key: K,
remappingFunction: BiFunction<in K, in V, out V>
remappingFunction: BiFunction<in K, in V, out V?>
): V? {
val oldValue = this[key]
if (oldValue != null) {
Expand Down
13 changes: 7 additions & 6 deletions j2kt/jre/javatests/smoke/CollectionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ public boolean containsValue(@Nullable Object value) {
// The following overrides are only there to check that the override (with the Java signatures
// of the method) compiles.
@Override
@SuppressWarnings("nullness:override.param") // Checker expects @PolyNull (missing in JSpecify)
public V compute(
K key, BiFunction<? super K, ? super @Nullable V, ? extends V> remappingFunction) {
@SuppressWarnings("nullness:override") // Checker expects @PolyNull (missing in JSpecify)
public @Nullable V compute(
K key,
BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
return super.compute(key, remappingFunction);
}

Expand All @@ -260,9 +261,9 @@ public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction
}

@Override
@SuppressWarnings("nullness:override.param") // Checker expects @PolyNull (missing in JSpecify)
public V computeIfPresent(
K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
@SuppressWarnings("nullness:override") // Checker expects @PolyNull (missing in JSpecify)
public @Nullable V computeIfPresent(
K key, BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
return super.computeIfPresent(key, remappingFunction);
}

Expand Down

0 comments on commit ef39af3

Please sign in to comment.