Override toString function in stdlib delegates

Co-authored-by: ilya-g <ilya.gorbunov@jetbrains.com>
This commit is contained in:
Iaroslav Postovalov
2021-01-02 18:51:37 +07:00
committed by Space Team
parent cfa06dbf74
commit fb80c0cb0d
7 changed files with 22 additions and 3 deletions
@@ -23,4 +23,7 @@ private class ThreadLocalDelegate<T>(private val initializer: () -> T) : ReadWri
override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
map[Thread.currentThread()] = value map[Thread.currentThread()] = value
} }
override fun toString(): String =
"ThreadLocalDelegate(${map.entries.joinToString { "#${it.key.id}=>${it.value}" }})"
} }
@@ -16,6 +16,8 @@ public abstract class ObservableProperty<V> : kotlin.properties.ReadWritePropert
public open override operator fun getValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): V public open override operator fun getValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): V
public open override operator fun setValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>, value: V): kotlin.Unit public open override operator fun setValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>, value: V): kotlin.Unit
public open override fun toString(): kotlin.String
} }
@kotlin.SinceKotlin(version = "1.4") @kotlin.SinceKotlin(version = "1.4")
@@ -16,6 +16,8 @@ public abstract class ObservableProperty<V> : kotlin.properties.ReadWritePropert
public open override operator fun getValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): V public open override operator fun getValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): V
public open override operator fun setValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>, value: V): kotlin.Unit public open override operator fun setValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>, value: V): kotlin.Unit
public open override fun toString(): kotlin.String
} }
@kotlin.SinceKotlin(version = "1.4") @kotlin.SinceKotlin(version = "1.4")
@@ -65,5 +65,8 @@ private class NotNullVar<T : Any>() : ReadWriteProperty<Any?, T> {
public override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { public override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
this.value = value this.value = value
} }
public override fun toString(): String =
"NotNullProperty(${if (value != null) "value=$value" else "value not initialized yet"})"
} }
@@ -40,4 +40,6 @@ public abstract class ObservableProperty<V>(initialValue: V) : ReadWriteProperty
this.value = value this.value = value
afterChange(property, oldValue, value) afterChange(property, oldValue, value)
} }
override fun toString(): String = "ObservableProperty(value=$value)"
} }
@@ -16,30 +16,36 @@ class NotNullVarTest() {
private class NotNullVarTestGeneric<T : Any>(val a1: String, val b1: T) { private class NotNullVarTestGeneric<T : Any>(val a1: String, val b1: T) {
var a: String by Delegates.notNull() var a: String by Delegates.notNull()
var b by Delegates.notNull<T>() val bDelegate = Delegates.notNull<T>()
var b by bDelegate
public fun doTest() { public fun doTest() {
assertEquals("NotNullProperty(value not initialized yet)", bDelegate.toString())
a = a1 a = a1
b = b1 b = b1
assertTrue(a == "a", "fail: a should be a, but was $a") assertTrue(a == "a", "fail: a should be a, but was $a")
assertTrue(b == "b", "fail: b should be b, but was $b") assertTrue(b == "b", "fail: b should be b, but was $b")
assertEquals("NotNullProperty(value=$b)", bDelegate.toString())
} }
} }
class ObservablePropertyTest { class ObservablePropertyTest {
var result = false var result = false
var b: Int by Delegates.observable(1, { property, old, new -> val bDelegate = Delegates.observable(1) { property, old, new ->
assertEquals("b", property.name) assertEquals("b", property.name)
if (!result) assertEquals(1, old) if (!result) assertEquals(1, old)
result = true result = true
assertEquals(new, b, "New value has already been set") assertEquals(new, b, "New value has already been set")
}) }
var b: Int by bDelegate
@Test fun doTest() { @Test fun doTest() {
b = 4 b = 4
assertTrue(b == 4, "fail: b != 4") assertTrue(b == 4, "fail: b != 4")
assertTrue(result, "fail: result should be true") assertTrue(result, "fail: result should be true")
assertEquals("ObservableProperty(value=$b)", bDelegate.toString())
} }
} }
@@ -4426,6 +4426,7 @@ public abstract class kotlin/properties/ObservableProperty : kotlin/properties/R
protected fun beforeChange (Lkotlin/reflect/KProperty;Ljava/lang/Object;Ljava/lang/Object;)Z protected fun beforeChange (Lkotlin/reflect/KProperty;Ljava/lang/Object;Ljava/lang/Object;)Z
public fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object; public fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
public fun setValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V public fun setValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V
public fun toString ()Ljava/lang/String;
} }
public abstract interface class kotlin/properties/PropertyDelegateProvider { public abstract interface class kotlin/properties/PropertyDelegateProvider {