[kotlin-tooling-core] Add extrasNullableLazyProperty API
^KT-56431 Verification Pending
This commit is contained in:
committed by
Space Team
parent
b646a43dab
commit
f21e08f6b9
@@ -98,12 +98,14 @@ public abstract interface class org/jetbrains/kotlin/tooling/core/ExtrasProperty
|
||||
public final class org/jetbrains/kotlin/tooling/core/ExtrasPropertyKt {
|
||||
public static final fun extrasFactoryProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/kotlin/tooling/core/ExtrasFactoryProperty;
|
||||
public static final fun extrasLazyProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlin/tooling/core/ExtrasLazyProperty;
|
||||
public static final fun extrasNullableLazyProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlin/tooling/core/NullableExtrasLazyProperty;
|
||||
public static final fun extrasReadProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;)Lorg/jetbrains/kotlin/tooling/core/ExtrasReadOnlyProperty;
|
||||
public static final fun extrasReadWriteProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;)Lorg/jetbrains/kotlin/tooling/core/ExtrasReadWriteProperty;
|
||||
public static final fun factoryProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/kotlin/tooling/core/ExtrasFactoryProperty;
|
||||
public static final fun getReadProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;)Lorg/jetbrains/kotlin/tooling/core/ExtrasReadOnlyProperty;
|
||||
public static final fun getReadWriteProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;)Lorg/jetbrains/kotlin/tooling/core/ExtrasReadWriteProperty;
|
||||
public static final fun lazyProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlin/tooling/core/ExtrasLazyProperty;
|
||||
public static final fun nullableLazyProperty (Lorg/jetbrains/kotlin/tooling/core/Extras$Key;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlin/tooling/core/NullableExtrasLazyProperty;
|
||||
}
|
||||
|
||||
public abstract interface class org/jetbrains/kotlin/tooling/core/ExtrasReadOnlyProperty : kotlin/properties/ReadOnlyProperty, org/jetbrains/kotlin/tooling/core/ExtrasProperty {
|
||||
@@ -223,6 +225,12 @@ public abstract interface class org/jetbrains/kotlin/tooling/core/NotNullExtrasR
|
||||
public fun setValue (Lorg/jetbrains/kotlin/tooling/core/HasMutableExtras;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V
|
||||
}
|
||||
|
||||
public abstract interface class org/jetbrains/kotlin/tooling/core/NullableExtrasLazyProperty : kotlin/properties/ReadOnlyProperty, org/jetbrains/kotlin/tooling/core/ExtrasProperty {
|
||||
public abstract fun getFactory ()Lkotlin/jvm/functions/Function1;
|
||||
public synthetic fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
|
||||
public fun getValue (Lorg/jetbrains/kotlin/tooling/core/HasMutableExtras;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
|
||||
}
|
||||
|
||||
public final class org/jetbrains/kotlin/tooling/core/TypeUtils {
|
||||
public static final fun renderReifiedTypeSignatureString (Lkotlin/reflect/KType;)Ljava/lang/String;
|
||||
}
|
||||
|
||||
+24
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.tooling.core
|
||||
|
||||
import javax.sound.midi.Receiver
|
||||
import java.util.*
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
@@ -22,6 +22,9 @@ fun <T : Any> Extras.Key<T>.factoryProperty(factory: () -> T) = extrasFactoryPro
|
||||
|
||||
fun <Receiver : HasMutableExtras, T : Any> Extras.Key<T>.lazyProperty(factory: Receiver.() -> T) = extrasLazyProperty(this, factory)
|
||||
|
||||
fun <Receiver : HasMutableExtras, T : Any> Extras.Key<Optional<T>>.nullableLazyProperty(factory: Receiver.() -> T?) =
|
||||
extrasNullableLazyProperty(this, factory)
|
||||
|
||||
fun <T : Any> extrasReadProperty(key: Extras.Key<T>): ExtrasReadOnlyProperty<T> = object : ExtrasReadOnlyProperty<T> {
|
||||
override val key: Extras.Key<T> = key
|
||||
}
|
||||
@@ -43,6 +46,14 @@ fun <Receiver : HasMutableExtras, T : Any> extrasLazyProperty(
|
||||
override val factory: Receiver.() -> T = factory
|
||||
}
|
||||
|
||||
fun <Receiver : HasMutableExtras, T : Any> extrasNullableLazyProperty(
|
||||
key: Extras.Key<Optional<T>>, factory: Receiver.() -> T?
|
||||
): NullableExtrasLazyProperty<Receiver, T> =
|
||||
object : NullableExtrasLazyProperty<Receiver, T> {
|
||||
override val key: Extras.Key<Optional<T>> = key
|
||||
override val factory: Receiver.() -> T? = factory
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> extrasReadWriteProperty(name: String? = null) =
|
||||
extrasReadWriteProperty(extrasKeyOf<T>(name))
|
||||
|
||||
@@ -55,6 +66,10 @@ inline fun <reified T : Any> extrasFactoryProperty(name: String? = null, noinlin
|
||||
inline fun <Receiver : HasMutableExtras, reified T : Any> extrasLazyProperty(name: String? = null, noinline factory: Receiver.() -> T) =
|
||||
extrasLazyProperty(extrasKeyOf(name), factory)
|
||||
|
||||
inline fun <Receiver : HasMutableExtras, reified T : Any> extrasNullableLazyProperty(
|
||||
name: String? = null, noinline factory: Receiver.() -> T?
|
||||
) = extrasNullableLazyProperty(extrasKeyOf(name), factory)
|
||||
|
||||
interface ExtrasReadOnlyProperty<T : Any> : ExtrasProperty<T>, ReadOnlyProperty<HasExtras, T?> {
|
||||
override fun getValue(thisRef: HasExtras, property: KProperty<*>): T? {
|
||||
return thisRef.extras[key]
|
||||
@@ -125,3 +140,11 @@ interface ExtrasLazyProperty<Receiver : HasMutableExtras, T : Any> : ExtrasPrope
|
||||
thisRef.extras[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
interface NullableExtrasLazyProperty<Receiver : HasMutableExtras, T : Any> : ExtrasProperty<Optional<T>>, ReadOnlyProperty<Receiver, T?> {
|
||||
val factory: Receiver.() -> T?
|
||||
|
||||
override fun getValue(thisRef: Receiver, property: KProperty<*>): T? {
|
||||
return thisRef.extras.getOrPut(key) { Optional.ofNullable(thisRef.factory()) }.let { if (it.isPresent) it.get() else null }
|
||||
}
|
||||
}
|
||||
|
||||
+35
@@ -37,6 +37,18 @@ class ExtrasPropertyTest {
|
||||
private val keySubjectList = extrasKeyOf<MutableList<Subject>>()
|
||||
private val Subject.lazyList: MutableList<Subject> by keySubjectList.lazyProperty { mutableListOf(this) }
|
||||
|
||||
private val lazyNullStringInvocations = mutableListOf<Subject>()
|
||||
private val Subject.lazyNullString: String? by extrasNullableLazyProperty("null") {
|
||||
lazyNullStringInvocations.add(this)
|
||||
null
|
||||
}
|
||||
|
||||
private val lazyNullableStringInvocations = mutableListOf<Subject>()
|
||||
private val Subject.lazyNullableString: String? by extrasNullableLazyProperty("not-null") {
|
||||
lazyNullableStringInvocations.add(this)
|
||||
"OK"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - readOnlyProperty`() {
|
||||
val subject = Subject()
|
||||
@@ -136,4 +148,27 @@ class ExtrasPropertyTest {
|
||||
assertSame(list, subject.lazyList)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - lazyNullableProperty`() {
|
||||
val subject1 = Subject()
|
||||
val subject2 = Subject()
|
||||
|
||||
assertNull(subject1.lazyNullString)
|
||||
assertNull(subject1.lazyNullString)
|
||||
assertEquals(listOf(subject1), lazyNullStringInvocations)
|
||||
|
||||
assertNull(subject2.lazyNullString)
|
||||
assertNull(subject2.lazyNullString)
|
||||
assertEquals(listOf(subject1, subject2), lazyNullStringInvocations)
|
||||
|
||||
|
||||
assertEquals("OK", subject1.lazyNullableString)
|
||||
assertEquals("OK", subject1.lazyNullableString)
|
||||
assertEquals(listOf(subject1), lazyNullableStringInvocations)
|
||||
|
||||
assertEquals("OK", subject2.lazyNullableString)
|
||||
assertEquals("OK", subject2.lazyNullableString)
|
||||
assertEquals(listOf(subject1, subject2), lazyNullableStringInvocations)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user