diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/CachedValueProperty.kt b/compiler/util/src/org/jetbrains/kotlin/utils/CachedValueProperty.kt index a0f01ef9d5e..872eb35d922 100644 --- a/compiler/util/src/org/jetbrains/kotlin/utils/CachedValueProperty.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/CachedValueProperty.kt @@ -16,16 +16,16 @@ package org.jetbrains.kotlin.utils -import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KProperty public class CachedValueProperty( private val calculator: () -> TValue, private val timestampCalculator: () -> TTimestamp -) : ReadOnlyProperty { +) { private var value: TValue? = null private var timestamp: TTimestamp? = null - public override fun getValue(thisRef: Any?, desc: PropertyMetadata): TValue { + operator fun getValue(thisRef: Any?, property: KProperty<*>): TValue { val currentTimestamp = timestampCalculator() if (value == null || timestamp != currentTimestamp) { value = calculator() diff --git a/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt b/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt index 8b5523f04f4..1069a5f6014 100644 --- a/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt +++ b/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt @@ -1,5 +1,5 @@ // "Create class 'Foo'" "true" -// ERROR: Class 'Foo' must be declared abstract or implement abstract member
public abstract operator fun getValue(thisRef: A<T>, property: kotlin.PropertyMetadata): B defined in kotlin.properties.ReadOnlyProperty +// ERROR: Class 'Foo' must be declared abstract or implement abstract member
public abstract operator fun getValue(thisRef: A<T>, property: kotlin.reflect.KProperty<*>): B defined in kotlin.properties.ReadOnlyProperty open class B diff --git a/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt.after b/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt.after index 037d0fd5f5c..bb06b8bc864 100644 --- a/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt.after +++ b/idea/testData/quickfix/createFromUsage/createClass/callExpression/callInMemberValDelegateRuntime.kt.after @@ -1,7 +1,7 @@ import kotlin.properties.ReadOnlyProperty // "Create class 'Foo'" "true" -// ERROR: Class 'Foo' must be declared abstract or implement abstract member
public abstract operator fun getValue(thisRef: A<T>, property: kotlin.PropertyMetadata): B defined in kotlin.properties.ReadOnlyProperty +// ERROR: Class 'Foo' must be declared abstract or implement abstract member
public abstract operator fun getValue(thisRef: A<T>, property: kotlin.reflect.KProperty<*>): B defined in kotlin.properties.ReadOnlyProperty open class B diff --git a/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/lazy.kt b/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/lazy.kt index 521e1b771cf..32f35186007 100644 --- a/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/lazy.kt +++ b/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/lazy.kt @@ -2,4 +2,4 @@ import kotlin.properties.Delegates val x: Int by Delegates.lazy {1} -// REF: (in kotlin.properties.ReadOnlyProperty).getValue(R,kotlin.PropertyMetadata) \ No newline at end of file +// REF: (in kotlin.properties.ReadOnlyProperty).getValue(R,kotlin.reflect.KProperty<*>) diff --git a/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/notNull.kt b/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/notNull.kt index 05e54cff5f9..662a7bbbe5f 100644 --- a/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/notNull.kt +++ b/idea/testData/resolve/references/delegatedPropertyAccessors/inStandardLibrary/notNull.kt @@ -3,5 +3,5 @@ import kotlin.properties.Delegates var x: Int by Delegates.notNull() // MULTIRESOLVE -// REF: (in kotlin.properties.ReadWriteProperty).getValue(R,kotlin.PropertyMetadata) -// REF: (in kotlin.properties.ReadWriteProperty).setValue(R,kotlin.PropertyMetadata,T) \ No newline at end of file +// REF: (in kotlin.properties.ReadWriteProperty).getValue(R,kotlin.reflect.KProperty<*>) +// REF: (in kotlin.properties.ReadWriteProperty).setValue(R,kotlin.reflect.KProperty<*>,T) diff --git a/libraries/stdlib/src/kotlin/properties/Delegation.kt b/libraries/stdlib/src/kotlin/properties/Delegation.kt index 24f4f3e2fd7..3c9710192ea 100644 --- a/libraries/stdlib/src/kotlin/properties/Delegation.kt +++ b/libraries/stdlib/src/kotlin/properties/Delegation.kt @@ -1,7 +1,7 @@ package kotlin.properties import java.util.NoSuchElementException - +import kotlin.reflect.KProperty /** * Standard property delegates. @@ -49,9 +49,9 @@ public object Delegates { * @param onChange the callback which is called after the change of the property is made. The value of the property * has already been changed when this callback is invoked. */ - public inline fun observable(initialValue: T, crossinline onChange: (property: PropertyMetadata, oldValue: T, newValue: T) -> Unit): + public inline fun observable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit): ReadWriteProperty = object : ObservableProperty(initialValue) { - override fun afterChange(property: PropertyMetadata, oldValue: T, newValue: T) = onChange(property, oldValue, newValue) + override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = onChange(property, oldValue, newValue) } /** @@ -63,9 +63,9 @@ public object Delegates { * If the callback returns `true` the value of the property is being set to the new value, * and if the callback returns `false` the new value is discarded and the property remains its old value. */ - public inline fun vetoable(initialValue: T, crossinline onChange: (property: PropertyMetadata, oldValue: T, newValue: T) -> Boolean): + public inline fun vetoable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Boolean): ReadWriteProperty = object : ObservableProperty(initialValue) { - override fun beforeChange(property: PropertyMetadata, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) + override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) } /** @@ -115,20 +115,20 @@ public object Delegates { private class NotNullVar() : ReadWriteProperty { private var value: T? = null - public override fun getValue(thisRef: Any?, property: PropertyMetadata): T { + public override fun getValue(thisRef: Any?, property: KProperty<*>): T { return value ?: throw IllegalStateException("Property ${property.name} should be initialized before get.") } - public override fun setValue(thisRef: Any?, property: PropertyMetadata, value: T) { + public override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { this.value = value } } @Deprecated("Use Delegates.vetoable() instead or construct implementation of abstract ObservableProperty", ReplaceWith("Delegates.vetoable(initialValue, onChange)")) -public fun ObservableProperty(initialValue: T, onChange: (property: PropertyMetadata, oldValue: T, newValue: T) -> Boolean): ObservableProperty = +public fun ObservableProperty(initialValue: T, onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Boolean): ObservableProperty = object : ObservableProperty(initialValue) { - override fun beforeChange(property: PropertyMetadata, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) + override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) } /** @@ -144,19 +144,19 @@ public abstract class ObservableProperty(initialValue: T) : ReadWriteProperty * If the callback returns `true` the value of the property is being set to the new value, * and if the callback returns `false` the new value is discarded and the property remains its old value. */ - protected open fun beforeChange(property: PropertyMetadata, oldValue: T, newValue: T): Boolean = true + protected open fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean = true /** * The callback which is called after the change of the property is made. The value of the property * has already been changed when this callback is invoked. */ - protected open fun afterChange (property: PropertyMetadata, oldValue: T, newValue: T): Unit {} + protected open fun afterChange (property: KProperty<*>, oldValue: T, newValue: T): Unit {} - public override fun getValue(thisRef: Any?, property: PropertyMetadata): T { + public override fun getValue(thisRef: Any?, property: KProperty<*>): T { return value } - public override fun setValue(thisRef: Any?, property: PropertyMetadata, value: T) { + public override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { val oldValue = this.value if (!beforeChange(property, oldValue, value)) { return @@ -179,7 +179,7 @@ private fun unescape(value: Any?): Any? { private class LazyVal(private val initializer: () -> T) : ReadOnlyProperty { private var value: Any? = null - public override fun getValue(thisRef: Any?, property: PropertyMetadata): T { + public override fun getValue(thisRef: Any?, property: KProperty<*>): T { if (value == null) { value = escape(initializer()) } @@ -191,7 +191,7 @@ private class BlockingLazyVal(lock: Any?, private val initializer: () -> T) : private val lock = lock ?: this @Volatile private var value: Any? = null - public override fun getValue(thisRef: Any?, property: PropertyMetadata): T { + public override fun getValue(thisRef: Any?, property: KProperty<*>): T { val _v1 = value if (_v1 != null) { return unescape(_v1) as T @@ -226,20 +226,20 @@ public abstract class MapVal() : ReadOnlyProperty { /** * Returns the map key used to store the values of the given property. - * @param desc the property for which the key is requested. + * @param property the property for which the key is requested. */ - protected abstract fun key(desc: PropertyMetadata): K + protected abstract fun key(property: KProperty<*>): K /** * Returns the property value to be used when the map does not contain the corresponding key. * @param ref the object instance for which the value was requested. * @param property the property for which the value was requested. */ - protected open fun default(ref: T, property: PropertyMetadata): V { + protected open fun default(ref: T, property: KProperty<*>): V { throw NoSuchElementException("The value for property ${property.name} is missing in $ref.") } - public override fun getValue(thisRef: T, property: PropertyMetadata) : V { + public override fun getValue(thisRef: T, property: KProperty<*>) : V { val map = map(thisRef) val key = key(property) return map.getOrElse(key, { default(thisRef, property) }) as V @@ -255,17 +255,17 @@ public abstract class MapVal() : ReadOnlyProperty { public abstract class MapVar() : MapVal(), ReadWriteProperty { protected abstract override fun map(ref: T): MutableMap - public override fun setValue(thisRef: T, property: PropertyMetadata, value: V) { + public override fun setValue(thisRef: T, property: KProperty<*>, value: V) { val map = map(thisRef) map.put(key(property), value) } - override fun getValue(thisRef: T, property: PropertyMetadata): V { + override fun getValue(thisRef: T, property: KProperty<*>): V { return super.getValue(thisRef, property) } } -private val propertyNameSelector: (PropertyMetadata) -> String = {it.name} +private val propertyNameSelector: (KProperty<*>) -> String = {it.name} private val throwKeyNotFound: (Any?, Any?) -> Nothing = {thisRef, key -> throw NoSuchElementException("The value for key $key is missing from $thisRef.") } /** @@ -275,18 +275,20 @@ private val throwKeyNotFound: (Any?, Any?) -> Nothing = {thisRef, key -> throw N * @param key the function to calculate the map key from a property metadata object. * @param default the function returning the value of the property for a given object if it's missing from the given map. */ -public open class FixedMapVal(private val map: Map, - private val key: (PropertyMetadata) -> K, - private val default: (ref: T, key: K) -> V = throwKeyNotFound) : MapVal() { +public open class FixedMapVal( + private val map: Map, + private val key: (KProperty<*>) -> K, + private val default: (ref: T, key: K) -> V = throwKeyNotFound +) : MapVal() { protected override fun map(ref: T): Map { return map } - protected override fun key(desc: PropertyMetadata): K { - return (key)(desc) + protected override fun key(property: KProperty<*>): K { + return (key)(property) } - protected override fun default(ref: T, property: PropertyMetadata): V { + protected override fun default(ref: T, property: KProperty<*>): V { return (default)(ref, key(property)) } } @@ -298,18 +300,20 @@ public open class FixedMapVal(private val map: Map, * @param key the function to calculate the map key from a property metadata object. * @param default the function returning the value of the property for a given object if it's missing from the given map. */ -public open class FixedMapVar(private val map: MutableMap, - private val key: (PropertyMetadata) -> K, - private val default: (ref: T, key: K) -> V = throwKeyNotFound) : MapVar() { +public open class FixedMapVar( + private val map: MutableMap, + private val key: (KProperty<*>) -> K, + private val default: (ref: T, key: K) -> V = throwKeyNotFound +) : MapVar() { protected override fun map(ref: T): MutableMap { return map } - protected override fun key(desc: PropertyMetadata): K { + protected override fun key(desc: KProperty<*>): K { return (key)(desc) } - protected override fun default(ref: T, property: PropertyMetadata): V { + protected override fun default(ref: T, property: KProperty<*>): V { return (default)(ref, key(property)) } } diff --git a/libraries/stdlib/src/kotlin/properties/Interfaces.kt b/libraries/stdlib/src/kotlin/properties/Interfaces.kt index 9e862b20bfe..b8034216340 100644 --- a/libraries/stdlib/src/kotlin/properties/Interfaces.kt +++ b/libraries/stdlib/src/kotlin/properties/Interfaces.kt @@ -1,5 +1,6 @@ package kotlin.properties +import kotlin.reflect.KProperty /** * Base interface that can be used for implementing property delegates of read-only properties. @@ -17,7 +18,7 @@ public interface ReadOnlyProperty { * @param property the metadata for the property. * @return the property value. */ - public operator fun getValue(thisRef: R, property: PropertyMetadata): T + public operator fun getValue(thisRef: R, property: KProperty<*>): T } /** @@ -36,7 +37,7 @@ public interface ReadWriteProperty { * @param property the metadata for the property. * @return the property value. */ - public operator fun getValue(thisRef: R, property: PropertyMetadata): T + public operator fun getValue(thisRef: R, property: KProperty<*>): T /** * Sets the value of the property for the given object. @@ -44,5 +45,5 @@ public interface ReadWriteProperty { * @param property the metadata for the property. * @param value the value to set. */ - public operator fun setValue(thisRef: R, property: PropertyMetadata, value: T) + public operator fun setValue(thisRef: R, property: KProperty<*>, value: T) } diff --git a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt index 7d76e586b4a..82ca5ac3c3d 100644 --- a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt +++ b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt @@ -1,7 +1,7 @@ @file:kotlin.jvm.JvmName("MapAccessorsKt") package kotlin.properties -// extensions for Map and MutableMap +import kotlin.reflect.KProperty /** * Returns the value of the property for the given object from this read-only map. @@ -11,7 +11,7 @@ package kotlin.properties * * @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]). */ -public operator fun Map.getValue(thisRef: Any?, property: PropertyMetadata): V1 = getOrImplicitDefault(property.name) as V1 +public operator fun Map.getValue(thisRef: Any?, property: KProperty<*>): V1 = getOrImplicitDefault(property.name) as V1 /** * Returns the value of the property for the given object from this mutable map. @@ -22,7 +22,7 @@ public operator fun Map.getValue(thisRef: Any?, * @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]). */ @kotlin.jvm.JvmName("getVar") -public operator fun MutableMap.getValue(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V +public operator fun MutableMap.getValue(thisRef: Any?, property: KProperty<*>): V = getOrImplicitDefault(property.name) as V /** * Stores the value of the property for the given object in this mutable map. @@ -30,6 +30,6 @@ public operator fun MutableMap.getValue(thisRef: Any?, prop * @param property the metadata for the property, used to get the name of property and store the value associated with that name in the map. * @param value the value to set. */ -public operator fun MutableMap.setValue(thisRef: Any?, property: PropertyMetadata, value: V) { +public operator fun MutableMap.setValue(thisRef: Any?, property: KProperty<*>, value: V) { this.put(property.name, value) -} \ No newline at end of file +} diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index 12be348cb95..27ca7f624ff 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -3,7 +3,7 @@ package kotlin import java.io.Serializable - +import kotlin.reflect.KProperty /** * Represents a value with lazy initialization. @@ -74,7 +74,7 @@ public fun lazy(lock: Any?, initializer: () -> T): Lazy = SynchronizedLazy * This extension allows to use instances of Lazy for property delegation: * `val property: String by lazy { initializer }` */ -public operator fun Lazy.getValue(thisRef: Any?, property: PropertyMetadata): T = value +public operator fun Lazy.getValue(thisRef: Any?, property: KProperty<*>): T = value /** * Specifies how a [Lazy] instance synchronizes access among multiple threads. diff --git a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt index 4de5a6f8989..1d4b51ca4f3 100644 --- a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt @@ -3,6 +3,7 @@ package test.properties.delegation.map import org.junit.Test as test import java.util.HashMap import kotlin.properties.* +import kotlin.reflect.KProperty import kotlin.test.* data class B(val a: Int) @@ -125,15 +126,14 @@ class MapPropertyFunctionTest() { } } -// TODO Remove explicit types when KT-9072 will be fixed -val mapVal: MapVal = object: MapVal() { +val mapVal = object : MapVal() { override fun map(ref: MapPropertyCustomTest) = ref.map - override fun key(desc: PropertyMetadata) = "${desc.name}Desc" + override fun key(property: KProperty<*>) = "${property.name}Desc" } -val mapVar: MapVar = object : MapVar() { +val mapVar = object : MapVar() { override fun map(ref: MapPropertyCustomTest) = ref.map - override fun key(desc: PropertyMetadata) = "${desc.name}Desc" + override fun key(property: KProperty<*>) = "${property.name}Desc" } class MapPropertyCustomTest() { @@ -148,18 +148,18 @@ class MapPropertyCustomTest() { } } -val mapValWithDefault: MapVal = object : MapVal() { +val mapValWithDefault = object : MapVal() { override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map - override fun key(desc: PropertyMetadata) = desc.name + override fun key(property: KProperty<*>) = property.name - override fun default(ref: MapPropertyCustomWithDefaultTest, key: PropertyMetadata) = "default" + override fun default(ref: MapPropertyCustomWithDefaultTest, key: KProperty<*>) = "default" } -val mapVarWithDefault: MapVar = object : MapVar() { +val mapVarWithDefault = object : MapVar() { override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map - override fun key(desc: PropertyMetadata) = desc.name + override fun key(property: KProperty<*>) = property.name - override fun default(ref: MapPropertyCustomWithDefaultTest, key: PropertyMetadata) = "default" + override fun default(ref: MapPropertyCustomWithDefaultTest, key: KProperty<*>) = "default" } class MapPropertyCustomWithDefaultTest() {