From 000e7493a19e73f2b92615dd6cb9cfce4d18f97c Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 7 Sep 2018 05:53:38 +0300 Subject: [PATCH] =?UTF-8?q?Introduce=20common=20KCallable=20and=20K(Mutabl?= =?UTF-8?q?e)Property(=C3=B8,0,1,2)=20interfaces=20#KT-25935?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/builtins/src/kotlin/reflect/KCallable.kt | 5 +- core/builtins/src/kotlin/reflect/KProperty.kt | 30 ++--- .../stdlib/src/kotlin/reflect/KCallable.kt | 23 ++++ .../stdlib/src/kotlin/reflect/KProperty.kt | 119 ++++++++++++++++++ 4 files changed, 160 insertions(+), 17 deletions(-) create mode 100644 libraries/stdlib/src/kotlin/reflect/KCallable.kt create mode 100644 libraries/stdlib/src/kotlin/reflect/KProperty.kt diff --git a/core/builtins/src/kotlin/reflect/KCallable.kt b/core/builtins/src/kotlin/reflect/KCallable.kt index e3086721c50..78a6db9ace7 100644 --- a/core/builtins/src/kotlin/reflect/KCallable.kt +++ b/core/builtins/src/kotlin/reflect/KCallable.kt @@ -3,6 +3,7 @@ * that can be found in the license/LICENSE.txt file. */ +@file:Suppress("ACTUAL_WITHOUT_EXPECT") // for building kotlin-runtime package kotlin.reflect /** @@ -10,7 +11,7 @@ package kotlin.reflect * * @param R return type of the callable. */ -public interface KCallable : KAnnotatedElement { +public actual interface KCallable : KAnnotatedElement { /** * The name of this callable as it was declared in the source code. * If the callable has no name, a special invented name is created. @@ -19,7 +20,7 @@ public interface KCallable : KAnnotatedElement { * - property accessors: the getter for a property named "foo" will have the name "", * the setter, similarly, will have the name "". */ - public val name: String + public actual val name: String /** * Parameters required to make a call to this callable. diff --git a/core/builtins/src/kotlin/reflect/KProperty.kt b/core/builtins/src/kotlin/reflect/KProperty.kt index 0fb3c3f1397..444593e428c 100644 --- a/core/builtins/src/kotlin/reflect/KProperty.kt +++ b/core/builtins/src/kotlin/reflect/KProperty.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -@file:Suppress("IMPLEMENTING_FUNCTION_INTERFACE") +@file:Suppress("IMPLEMENTING_FUNCTION_INTERFACE", /* for building kotlin-runtime */ "ACTUAL_WITHOUT_EXPECT") package kotlin.reflect /** @@ -26,7 +26,7 @@ package kotlin.reflect * * @param R the type of the property. */ -public interface KProperty : KCallable { +public actual interface KProperty : KCallable { /** * `true` if this property is `lateinit`. * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties) @@ -67,7 +67,7 @@ public interface KProperty : KCallable { /** * Represents a property declared as a `var`. */ -public interface KMutableProperty : KProperty { +public actual interface KMutableProperty : KProperty { /** The setter of this mutable property, used to change the value of the property. */ public val setter: Setter @@ -83,11 +83,11 @@ public interface KMutableProperty : KProperty { * Such property is either originally declared in a receiverless context such as a package, * or has the receiver bound to it. */ -public interface KProperty0 : KProperty, () -> R { +public actual interface KProperty0 : KProperty, () -> R { /** * Returns the current value of the property. */ - public fun get(): R + public actual fun get(): R /** * Returns the value of the delegate if this is a delegated property, or `null` if this property is not delegated. @@ -110,13 +110,13 @@ public interface KProperty0 : KProperty, () -> R { /** * Represents a `var`-property without any kind of receiver. */ -public interface KMutableProperty0 : KProperty0, KMutableProperty { +public actual interface KMutableProperty0 : KProperty0, KMutableProperty { /** * Modifies the value of the property. * * @param value the new value to be assigned to this property. */ - public fun set(value: R) + public actual fun set(value: R) override val setter: Setter @@ -135,7 +135,7 @@ public interface KMutableProperty0 : KProperty0, KMutableProperty { * @param T the type of the receiver which should be used to obtain the value of the property. * @param R the type of the property. */ -public interface KProperty1 : KProperty, (T) -> R { +public actual interface KProperty1 : KProperty, (T) -> R { /** * Returns the current value of the property. * @@ -143,7 +143,7 @@ public interface KProperty1 : KProperty, (T) -> R { * For example, it should be a class instance if this is a member property of that class, * or an extension receiver if this is a top level extension property. */ - public fun get(receiver: T): R + public actual fun get(receiver: T): R /** * Returns the value of the delegate if this is a delegated property, or `null` if this property is not delegated. @@ -175,7 +175,7 @@ public interface KProperty1 : KProperty, (T) -> R { /** * Represents a `var`-property, operations on which take one receiver as a parameter. */ -public interface KMutableProperty1 : KProperty1, KMutableProperty { +public actual interface KMutableProperty1 : KProperty1, KMutableProperty { /** * Modifies the value of the property. * @@ -184,7 +184,7 @@ public interface KMutableProperty1 : KProperty1, KMutableProperty * or an extension receiver if this is a top level extension property. * @param value the new value to be assigned to this property. */ - public fun set(receiver: T, value: R) + public actual fun set(receiver: T, value: R) override val setter: Setter @@ -207,7 +207,7 @@ public interface KMutableProperty1 : KProperty1, KMutableProperty * the type of the extension receiver. * @param R the type of the property. */ -public interface KProperty2 : KProperty, (D, E) -> R { +public actual interface KProperty2 : KProperty, (D, E) -> R { /** * Returns the current value of the property. In case of the extension property in a class, * the instance of the class should be passed first and the instance of the extension receiver second. @@ -215,7 +215,7 @@ public interface KProperty2 : KProperty, (D, E) -> R { * @param receiver1 the instance of the first receiver. * @param receiver2 the instance of the second receiver. */ - public fun get(receiver1: D, receiver2: E): R + public actual fun get(receiver1: D, receiver2: E): R /** * Returns the value of the delegate if this is a delegated property, or `null` if this property is not delegated. @@ -247,7 +247,7 @@ public interface KProperty2 : KProperty, (D, E) -> R { /** * Represents a `var`-property, operations on which take two receivers as parameters. */ -public interface KMutableProperty2 : KProperty2, KMutableProperty { +public actual interface KMutableProperty2 : KProperty2, KMutableProperty { /** * Modifies the value of the property. * @@ -255,7 +255,7 @@ public interface KMutableProperty2 : KProperty2, KMutablePrope * @param receiver2 the instance of the second receiver. * @param value the new value to be assigned to this property. */ - public fun set(receiver1: D, receiver2: E, value: R) + public actual fun set(receiver1: D, receiver2: E, value: R) override val setter: Setter diff --git a/libraries/stdlib/src/kotlin/reflect/KCallable.kt b/libraries/stdlib/src/kotlin/reflect/KCallable.kt new file mode 100644 index 00000000000..134e13b42b1 --- /dev/null +++ b/libraries/stdlib/src/kotlin/reflect/KCallable.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package kotlin.reflect + +/** + * Represents a callable entity, such as a function or a property. + * + * @param R return type of the callable. + */ +public expect interface KCallable { + /** + * The name of this callable as it was declared in the source code. + * If the callable has no name, a special invented name is created. + * Nameless callables include: + * - constructors have the name "", + * - property accessors: the getter for a property named "foo" will have the name "", + * the setter, similarly, will have the name "". + */ + public val name: String +} diff --git a/libraries/stdlib/src/kotlin/reflect/KProperty.kt b/libraries/stdlib/src/kotlin/reflect/KProperty.kt new file mode 100644 index 00000000000..aaae094ab0f --- /dev/null +++ b/libraries/stdlib/src/kotlin/reflect/KProperty.kt @@ -0,0 +1,119 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +@file:Suppress("IMPLEMENTING_FUNCTION_INTERFACE") +package kotlin.reflect + +/** + * Represents a property, such as a named `val` or `var` declaration. + * Instances of this class are obtainable by the `::` operator. + * + * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/reflection.html) + * for more information. + * + * @param R the type of the property. + */ +public expect interface KProperty : KCallable { +} + +/** + * Represents a property declared as a `var`. + */ +public expect interface KMutableProperty : KProperty { +} + + +/** + * Represents a property without any kind of receiver. + * Such property is either originally declared in a receiverless context such as a package, + * or has the receiver bound to it. + */ +public expect interface KProperty0 : KProperty, () -> R { + /** + * Returns the current value of the property. + */ + public fun get(): R +} + +/** + * Represents a `var`-property without any kind of receiver. + */ +public expect interface KMutableProperty0 : KProperty0, KMutableProperty { + /** + * Modifies the value of the property. + * + * @param value the new value to be assigned to this property. + */ + public fun set(value: R) +} + + +/** + * Represents a property, operations on which take one receiver as a parameter. + * + * @param T the type of the receiver which should be used to obtain the value of the property. + * @param R the type of the property. + */ +public expect interface KProperty1 : KProperty, (T) -> R { + /** + * Returns the current value of the property. + * + * @param receiver the receiver which is used to obtain the value of the property. + * For example, it should be a class instance if this is a member property of that class, + * or an extension receiver if this is a top level extension property. + */ + public fun get(receiver: T): R +} + +/** + * Represents a `var`-property, operations on which take one receiver as a parameter. + */ +public expect interface KMutableProperty1 : KProperty1, KMutableProperty { + /** + * Modifies the value of the property. + * + * @param receiver the receiver which is used to modify the value of the property. + * For example, it should be a class instance if this is a member property of that class, + * or an extension receiver if this is a top level extension property. + * @param value the new value to be assigned to this property. + */ + public fun set(receiver: T, value: R) +} + + +/** + * Represents a property, operations on which take two receivers as parameters, + * such as an extension property declared in a class. + * + * @param D the type of the first receiver. In case of the extension property in a class this is + * the type of the declaring class of the property, or any subclass of that class. + * @param E the type of the second receiver. In case of the extension property in a class this is + * the type of the extension receiver. + * @param R the type of the property. + */ +public expect interface KProperty2 : KProperty, (D, E) -> R { + /** + * Returns the current value of the property. In case of the extension property in a class, + * the instance of the class should be passed first and the instance of the extension receiver second. + * + * @param receiver1 the instance of the first receiver. + * @param receiver2 the instance of the second receiver. + */ + public fun get(receiver1: D, receiver2: E): R +} + +/** + * Represents a `var`-property, operations on which take two receivers as parameters. + */ +public expect interface KMutableProperty2 : KProperty2, KMutableProperty { + /** + * Modifies the value of the property. + * + * @param receiver1 the instance of the first receiver. + * @param receiver2 the instance of the second receiver. + * @param value the new value to be assigned to this property. + */ + public fun set(receiver1: D, receiver2: E, value: R) +}