[scripting] Make properties from destructing declarations available with reflection

This commit is contained in:
Ilya Muradyan
2021-10-05 16:40:59 +03:00
committed by teamcityserver
parent 1c16f2f8c9
commit 58831eacca
5 changed files with 65 additions and 1 deletions
@@ -6,8 +6,11 @@
package org.jetbrains.kotlin.scripting.resolve
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider
@@ -53,6 +56,20 @@ class LazyScriptClassMemberScope(
override fun createPropertiesFromPrimaryConstructorParameters(name: Name, result: MutableSet<PropertyDescriptor>) {
}
override fun collectDescriptorsFromDestructingDeclaration(
result: MutableSet<DeclarationDescriptor>,
declaration: KtDestructuringDeclaration,
nameFilter: (Name) -> Boolean,
location: LookupLocation
) {
for (entry in declaration.entries) {
val name = entry.nameAsSafeName
if (nameFilter(name) && name.identifierOrNullIfSpecial != "_") {
result.addAll(getContributedVariables(name, location))
}
}
}
companion object {
const val IMPLICIT_RECEIVER_PARAM_NAME_PREFIX = "\$\$implicitReceiver"
const val IMPORTED_SCRIPT_PARAM_NAME_PREFIX = "\$\$importedScript"
@@ -9,6 +9,9 @@ import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.full.createInstance
import kotlin.reflect.full.declaredMembers
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
@@ -46,6 +49,31 @@ class ScriptCompilerTest : TestCase() {
assertEquals("Clazz", nestedClasses[0].simpleName)
}
fun testDestructingDeclarations() {
val res = compileToClass(
"""
val c = 3
val (a, b) = 1 to 2
val (_, d, _) = listOf('1', '2', '3')
""".trimIndent().toScriptSource()
)
val kClass = res.valueOrThrow()
val scriptInstance = kClass.createInstance()
val members = kClass.declaredMembers
val namesToMembers = members.associateBy { it.name }
fun prop(name: String) = namesToMembers[name]!! as KProperty<*>
fun propValue(name: String) = prop(name).call(scriptInstance)
fun propType(name: String) = prop(name).returnType.classifier as KClass<*>
assertEquals(1, propValue("a"))
assertEquals(Int::class, propType("b"))
assertEquals(3, propValue("c"))
assertEquals(Char::class, propType("d"))
assertNull(namesToMembers["_"])
}
fun compile(
script: SourceCode,
cfgBody: ScriptCompilationConfiguration.Builder.() -> Unit