mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 08:31:26 +00:00
Add MutableMap.remove(K, V) as built-in declaration
Use PlatformDependent annotation to guarantee it's only be available for JDK8 Also adjust type-safe bridges and mutable collection stubs generation
This commit is contained in:
45
compiler/testData/codegen/java8/box/mapRemove/readOnlyMap.kt
vendored
Normal file
45
compiler/testData/codegen/java8/box/mapRemove/readOnlyMap.kt
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
// FULL_JDK
|
||||
// WITH_RUNTIME
|
||||
|
||||
// FILE: A.java
|
||||
public class A {
|
||||
public static void foo(java.util.Map<String, String> x) {
|
||||
x.remove("abc", "cde");
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
|
||||
class ReadOnlyMap<K, V>(val x: K, val y: V) : Map<K, V> {
|
||||
override val entries: Set<Map.Entry<K, V>>
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val keys: Set<K>
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val size: Int
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val values: Collection<V>
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun containsKey(key: K) = key == x
|
||||
|
||||
override fun containsValue(value: V) = value == y
|
||||
|
||||
override fun get(key: K): V? = if (key == x) y else null
|
||||
|
||||
override fun isEmpty() = false
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
A.foo(ReadOnlyMap("abc", "cde"))
|
||||
return "fail 1"
|
||||
} catch (e: UnsupportedOperationException) { }
|
||||
|
||||
try {
|
||||
// Default Map 'remove' implenetation actually does remove iff entry exists
|
||||
A.foo(ReadOnlyMap("abc", "123"))
|
||||
return "fail 2"
|
||||
} catch (e: UnsupportedOperationException) { }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
68
compiler/testData/codegen/java8/box/mapRemove/typeSafeBridge.kt
vendored
Normal file
68
compiler/testData/codegen/java8/box/mapRemove/typeSafeBridge.kt
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// FULL_JDK
|
||||
// WITH_RUNTIME
|
||||
|
||||
class A : MutableMap<String, String> {
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<String, String>>
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val keys: MutableSet<String>
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val values: MutableCollection<String>
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun clear() {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun put(key: String, value: String): String? {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun putAll(from: Map<out String, String>) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun remove(key: String): String? {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun containsKey(key: String): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun containsValue(value: String): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun get(key: String): String? {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun remove(key: String, value: String): Boolean {
|
||||
val h = key.hashCode() + value.hashCode()
|
||||
if (h != ("abc".hashCode() + "cde".hashCode())) return false
|
||||
return key == "abc" && value == "cde"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
if (!a.remove("abc", "cde")) return "fail 1"
|
||||
if (a.remove("abc", "123")) return "fail 2"
|
||||
|
||||
val mm = a as MutableMap<Any?, Any?>
|
||||
if (!a.remove("abc", "cde")) return "fail 3"
|
||||
if (a.remove("abc", "123")) return "fail 4"
|
||||
if (a.remove(1, "cde")) return "fail 5"
|
||||
if (a.remove(null, "cde")) return "fail 6"
|
||||
if (a.remove("abc", null)) return "fail 7"
|
||||
if (a.remove(null, null)) return "fail 7"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
68
compiler/testData/codegen/java8/box/mapRemove/typeSafeBridgeNotNullAny.kt
vendored
Normal file
68
compiler/testData/codegen/java8/box/mapRemove/typeSafeBridgeNotNullAny.kt
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// FULL_JDK
|
||||
// WITH_RUNTIME
|
||||
|
||||
class A : MutableMap<Any, Any> {
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<Any, Any>>
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val keys: MutableSet<Any>
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val values: MutableCollection<Any>
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun clear() {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun put(key: Any, value: Any): Any? {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun putAll(from: Map<out Any, Any>) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun remove(key: Any): Any? {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun containsKey(key: Any): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun containsValue(value: Any): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun get(key: Any): Any? {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun remove(key: Any, value: Any): Boolean {
|
||||
val h = key.hashCode() + value.hashCode()
|
||||
if (h != ("abc".hashCode() + "cde".hashCode())) return false
|
||||
return key == "abc" && value == "cde"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
if (!a.remove("abc", "cde")) return "fail 1"
|
||||
if (a.remove("abc", "123")) return "fail 2"
|
||||
|
||||
val mm = a as MutableMap<Any?, Any?>
|
||||
if (!a.remove("abc", "cde")) return "fail 3"
|
||||
if (a.remove("abc", "123")) return "fail 4"
|
||||
if (a.remove(1, "cde")) return "fail 5"
|
||||
if (a.remove(null, "cde")) return "fail 6"
|
||||
if (a.remove("abc", null)) return "fail 7"
|
||||
if (a.remove(null, null)) return "fail 7"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
25
compiler/testData/codegen/java8/writeSignature/mutableMapRemove.kt
vendored
Normal file
25
compiler/testData/codegen/java8/writeSignature/mutableMapRemove.kt
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
class KotlinMap1<K, V> : java.util.AbstractMap<K, V>() {
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun remove(x: K, y: V) = true
|
||||
}
|
||||
|
||||
// method: KotlinMap1::remove
|
||||
// jvm signature: (Ljava/lang/Object;Ljava/lang/Object;)Z
|
||||
// generic signature: null
|
||||
|
||||
class KotlinMap2 : java.util.AbstractMap<String, Int>() {
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<String, Int>>
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun remove(x: String, y: Int) = true
|
||||
}
|
||||
|
||||
// method: KotlinMap2::remove
|
||||
// jvm signature: (Ljava/lang/Object;Ljava/lang/Object;)Z
|
||||
// generic signature: null
|
||||
|
||||
// method: KotlinMap2::remove
|
||||
// jvm signature: (Ljava/lang/String;Ljava/lang/Integer;)Z
|
||||
// generic signature: null
|
||||
Reference in New Issue
Block a user