diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt index 1d0c98a5f99..d2604d7c59c 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt @@ -137,6 +137,16 @@ internal suspend fun Stage.await() { currentKotlinPluginLifecycle().await(this) } + +/** + * See [newProperty] + */ +internal inline fun Project.newKotlinPluginLifecycleAwareProperty( + finaliseIn: Stage = Stage.FinaliseDsl, initialValue: T? = null +): LifecycleAwareProperty { + return kotlinPluginLifecycle.newProperty(T::class.java, finaliseIn, initialValue) +} + /** * See [LifecycleAwareProperty] * Will create a new [LifecycleAwareProperty] which is going to finalise its value in stage [finaliseIn] @@ -152,10 +162,10 @@ internal suspend fun Stage.await() { * } * ``` */ -internal inline fun Project.newKotlinPluginLifecycleAwareProperty( +internal inline fun KotlinPluginLifecycle.newProperty( finaliseIn: Stage = Stage.FinaliseDsl, initialValue: T? = null ): LifecycleAwareProperty { - return kotlinPluginLifecycle.newLifecycleAwareProperty(T::class.java, finaliseIn, initialValue) + return newProperty(T::class.java, finaliseIn, initialValue) } /** @@ -323,7 +333,7 @@ internal interface KotlinPluginLifecycle { suspend fun await(stage: Stage) - fun newLifecycleAwareProperty( + fun newProperty( type: Class, finaliseIn: Stage, initialValue: T? ): LifecycleAwareProperty @@ -419,7 +429,7 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP override fun enqueue(stage: Stage, action: KotlinPluginLifecycle.() -> Unit) { if (stage < this.stage) { - throw IllegalLifecycleException("Cannot enqueue Action for stage '${this.stage}' in current stage '${this.stage}'") + throw IllegalLifecycleException("Cannot enqueue Action for stage '${stage}' in current stage '${this.stage}'") } /* @@ -463,10 +473,11 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP } } - override fun newLifecycleAwareProperty(type: Class, finaliseIn: Stage, initialValue: T?): LifecycleAwareProperty { + override fun newProperty(type: Class, finaliseIn: Stage, initialValue: T?): LifecycleAwareProperty { val property = project.objects.property(type) if (initialValue != null) property.set(initialValue) - enqueue(finaliseIn) { property.finalizeValue() } + if (finaliseIn <= stage) property.finalizeValue() + else enqueue(finaliseIn) { property.finalizeValue() } val lifecycleAwareProperty = LifecycleAwarePropertyImpl(finaliseIn, property) properties[property] = WeakReference(lifecycleAwareProperty) return lifecycleAwareProperty diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt index ce3a6c52a34..e56f7b8baf4 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt @@ -61,4 +61,26 @@ class LifecycleAwarePropertyTest { assertFailsWith { property.set(2) } } } + + @Test + fun `test - creating a property - after finaliseIn stage already passed`() = project.runLifecycleAwareTest { + launchInStage(KotlinPluginLifecycle.Stage.last) { + val property by project.newKotlinPluginLifecycleAwareProperty(Companion.first) + + /* Property was finalised immediately */ + assertFailsWith { property.set("Expected to fail") } + assertNull(property.orNull) + } + } + + @Test + fun `test - creating a property - in finaliseIn stage`() = project.runLifecycleAwareTest { + launchInStage(KotlinPluginLifecycle.Stage.FinaliseDsl) { + val property by project.newKotlinPluginLifecycleAwareProperty(FinaliseDsl) + + /* Property was finalised immediately */ + assertFailsWith { property.set("Expected to fail") } + assertNull(property.orNull) + } + } }