@@ -15,8 +15,8 @@ import org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException
1515import org.jetbrains.kotlinx.jupyter.repl.ContextUpdater
1616import org.jetbrains.kotlinx.jupyter.repl.InternalEvalResult
1717import 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
2020import kotlin.reflect.full.declaredMemberProperties
2121import kotlin.script.experimental.api.ResultValue
2222import kotlin.script.experimental.api.ResultWithDiagnostics
@@ -159,16 +159,21 @@ internal class InternalEvaluatorImpl(
159159 val kClass = target.scriptClass ? : return emptyMap()
160160 val cellClassInstance = target.scriptInstance!!
161161
162- val fields = kClass.declaredMemberProperties
162+ val fields = kClass.java.declaredFields
163+ // ignore implementation details of top level like script instance and result value
164+ val memberKPropertiesNames = kClass.declaredMemberProperties.map {
165+ it.name
166+ }.toHashSet()
163167 val ans = mutableMapOf<String , VariableStateImpl >()
164168 fields.forEach { property ->
165- @Suppress(" UNCHECKED_CAST" )
166- property as KProperty1 <Any , * >
169+ // if (property.name.startsWith("script$")) return@forEach
170+ if (! memberKPropertiesNames.contains(property.name)) return @forEach
171+
167172 val state = VariableStateImpl (property, cellClassInstance)
168173 variablesWatcher.addDeclaration(cellId, property.name)
169174
170175 // it was val, now it's var
171- if (property is KMutableProperty1 ) {
176+ if (isValField( property) ) {
172177 variablesHolder.remove(property.name)
173178 } else {
174179 variablesHolder[property.name] = state
@@ -180,6 +185,10 @@ internal class InternalEvaluatorImpl(
180185 return ans
181186 }
182187
188+ private fun isValField (property : Field ): Boolean {
189+ return property.modifiers and Modifier .FINAL != 0
190+ }
191+
183192 private fun updateDataAfterExecution (lastExecutionCellId : Int , resultValue : ResultValue ) {
184193 variablesWatcher.ensureStorageCreation(lastExecutionCellId)
185194 variablesHolder + = getVisibleVariables(resultValue, lastExecutionCellId)
0 commit comments