Skip to content

Commit a433627

Browse files
Change main reflection to jvm Fields; use KReflection for built-ins
1 parent a8b19e9 commit a433627

File tree

6 files changed

+352
-158
lines changed

6 files changed

+352
-158
lines changed

jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/VariableState.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package org.jetbrains.kotlinx.jupyter.api
22

3-
import kotlin.reflect.KProperty
43
import kotlin.reflect.KProperty1
54
import kotlin.reflect.full.declaredMemberProperties
65
import kotlin.reflect.jvm.isAccessible
6+
import java.lang.reflect.Field
77

88
interface VariableState {
9-
val property: KProperty<*>
9+
// val property: KProperty<*>
10+
val property: Field
1011
val scriptInstance: Any?
1112
val stringValue: String?
1213
val value: Result<Any?>
1314
}
1415

1516
data class VariableStateImpl(
16-
override val property: KProperty1<Any, *>,
17+
// override val property: KProperty1<Any, *>,
18+
override val property: Field,
1719
override val scriptInstance: Any,
1820
) : VariableState {
1921
private var cachedValue: Result<Any?> = Result.success(null)

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/InternalEvaluatorImpl.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException
1515
import org.jetbrains.kotlinx.jupyter.repl.ContextUpdater
1616
import org.jetbrains.kotlinx.jupyter.repl.InternalEvalResult
1717
import org.jetbrains.kotlinx.jupyter.repl.InternalEvaluator
18-
import kotlin.reflect.KMutableProperty1
19-
import kotlin.reflect.KProperty1
18+
import java.lang.reflect.Field
19+
import java.lang.reflect.Modifier
2020
import kotlin.reflect.full.declaredMemberProperties
2121
import kotlin.script.experimental.api.ResultValue
2222
import kotlin.script.experimental.api.ResultWithDiagnostics
@@ -157,16 +157,21 @@ internal class InternalEvaluatorImpl(
157157
val kClass = target.scriptClass ?: return emptyMap()
158158
val cellClassInstance = target.scriptInstance!!
159159

160-
val fields = kClass.declaredMemberProperties
160+
val fields = kClass.java.declaredFields
161+
// ignore implementation details of top level like script instance and result value
162+
val memberKPropertiesNames = kClass.declaredMemberProperties.map {
163+
it.name
164+
}.toHashSet()
161165
val ans = mutableMapOf<String, VariableStateImpl>()
162166
fields.forEach { property ->
163-
@Suppress("UNCHECKED_CAST")
164-
property as KProperty1<Any, *>
167+
// if (property.name.startsWith("script$")) return@forEach
168+
if (!memberKPropertiesNames.contains(property.name)) return@forEach
169+
165170
val state = VariableStateImpl(property, cellClassInstance)
166171
variablesWatcher.addDeclaration(cellId, property.name)
167172

168173
// it was val, now it's var
169-
if (property is KMutableProperty1) {
174+
if (isValField(property)) {
170175
variablesHolder.remove(property.name)
171176
} else {
172177
variablesHolder[property.name] = state
@@ -178,6 +183,10 @@ internal class InternalEvaluatorImpl(
178183
return ans
179184
}
180185

186+
private fun isValField(property: Field): Boolean {
187+
return property.modifiers and Modifier.FINAL != 0
188+
}
189+
181190
private fun updateDataAfterExecution(lastExecutionCellId: Int, resultValue: ResultValue) {
182191
variablesWatcher.ensureStorageCreation(lastExecutionCellId)
183192
variablesHolder += getVisibleVariables(resultValue, lastExecutionCellId)

0 commit comments

Comments
 (0)