@@ -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
@@ -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