Introduce KProperty{1,2}.getExtensionDelegate
These two functions are supposed to fix an inconvenience in the design of
KProperty{1,2}.getDelegate for extension properties, where you have to pass an
instance of the extension receiver which is going to be completely ignored
#KT-8384 Fixed
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
// IGNORE_BACKEND: JS
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty2
|
||||
import kotlin.reflect.jvm.isAccessible
|
||||
import kotlin.reflect.full.getExtensionDelegate
|
||||
import kotlin.test.*
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(instance: Any?, property: KProperty<*>) = true
|
||||
}
|
||||
|
||||
class Foo {
|
||||
val member: Boolean by Delegate
|
||||
val String.memberExtension: Boolean by Delegate
|
||||
}
|
||||
|
||||
val Foo.extension: Boolean by Delegate
|
||||
|
||||
fun box(): String {
|
||||
// Top level extension
|
||||
assertEquals(Delegate, Foo::extension.apply { isAccessible = true }.getExtensionDelegate())
|
||||
|
||||
// Member extension
|
||||
val me = Foo::class.members.single { it.name == "memberExtension" } as KProperty2<Foo, String, Boolean>
|
||||
assertEquals(Delegate, me.apply { isAccessible = true }.getExtensionDelegate(Foo()))
|
||||
|
||||
// Member (should fail)
|
||||
try {
|
||||
Foo::member.apply { isAccessible = true }.getExtensionDelegate()
|
||||
return "Fail: getExtensionDelegate() should fail on a non-extension property"
|
||||
} catch (e: Exception) {
|
||||
// OK
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
@kotlin.Metadata
|
||||
public final class Delegate {
|
||||
public final static field INSTANCE: Delegate
|
||||
private method <init>(): void
|
||||
public final method getValue(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.reflect.KProperty): boolean
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class Foo {
|
||||
private synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[]
|
||||
private final @org.jetbrains.annotations.NotNull field member$delegate: Delegate
|
||||
private final @org.jetbrains.annotations.NotNull field memberExtension$delegate: Delegate
|
||||
public method <init>(): void
|
||||
public final method getMember(): boolean
|
||||
public final method getMemberExtension(@org.jetbrains.annotations.NotNull p0: java.lang.String): boolean
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class GetExtensionDelegateKt {
|
||||
private synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[]
|
||||
private final static @org.jetbrains.annotations.NotNull field extension$delegate: Delegate
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method getExtension(@org.jetbrains.annotations.NotNull p0: Foo): boolean
|
||||
}
|
||||
Reference in New Issue
Block a user