Skip to content

Commit 67a00eb

Browse files
martinkretzschmarcopybara-github
authored andcommitted
Improve LinkedHashMap Java compatibility
- make it a subclass of `HashMap` ((accidentally?) relied upon by a Guava test) - make `HashMap` non-final (add one more bridge method so overrides will work as intended) - make `LinkedHashMap` non-final - add `LinkedHashMap#clone` PiperOrigin-RevId: 610402905
1 parent bf2ac8c commit 67a00eb

File tree

4 files changed

+54
-79
lines changed

4 files changed

+54
-79
lines changed

j2kt/jre/java/common/java/util/HashMap.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
import org.jspecify.nullness.NullMarked;
2121
import org.jspecify.nullness.Nullable;
2222

23-
// TODO(b/240106068): Native HashMap is final. But this class should be non-final.
24-
// We need to add bridges for java_get etc. before it is safe to remove final.
2523
/**
2624
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html">the official Java
2725
* API doc</a> for details.
2826
*/
2927
@KtNative(name = "java.util.HashMap")
3028
@NullMarked
31-
public final class HashMap<K extends @Nullable Object, V extends @Nullable Object>
29+
public class HashMap<K extends @Nullable Object, V extends @Nullable Object>
3230
extends AbstractMap<K, V> implements Cloneable, Serializable {
3331

3432
public HashMap() {}

j2kt/jre/java/common/java/util/LinkedHashMap.java

-75
This file was deleted.

j2kt/jre/java/native/java/util/HashMap.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ open class HashMap<K, V> private constructor(val ktHashMap: KotlinHashMap<K, V>)
6464

6565
@Suppress("UNCHECKED_CAST") override fun java_remove(key: Any?): V? = ktHashMap.remove(key as K)
6666

67-
override fun putAll(m: KotlinMap<out K, V>) = ktHashMap.putAll(m)
67+
override fun java_putAll(m: MutableMap<out K, out V>) = ktHashMap.putAll(m)
68+
69+
override fun putAll(m: KotlinMap<out K, V>) = java_putAll(m as MutableMap<out K, out V>)
6870

6971
override fun clear() = ktHashMap.clear()
7072

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2008 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package java.util;
17+
18+
import org.jspecify.nullness.NullMarked;
19+
import org.jspecify.nullness.Nullable;
20+
21+
// Note: This implementation relies on the fact that Kotlin Native's HashMap is linked.
22+
/**
23+
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html">the official
24+
* Java API doc</a> for details.
25+
*/
26+
@NullMarked
27+
public class LinkedHashMap<K extends @Nullable Object, V extends @Nullable Object>
28+
extends HashMap<K, V> implements Map<K, V>, Cloneable {
29+
30+
public LinkedHashMap() {
31+
super();
32+
}
33+
34+
public LinkedHashMap(int initialCapacity) {
35+
super(initialCapacity);
36+
}
37+
38+
public LinkedHashMap(int initialCapacity, float loadFactor) {
39+
super(initialCapacity, loadFactor);
40+
}
41+
42+
public LinkedHashMap(Map<? extends K, ? extends V> original) {
43+
super(original);
44+
}
45+
46+
@Override
47+
public Object clone() {
48+
return new LinkedHashMap<K, V>(this);
49+
}
50+
}

0 commit comments

Comments
 (0)