Skip to content

Commit 8cfc15e

Browse files
Change main reflection to jvm Fields; use KReflection for built-ins
1 parent 89eb9bd commit 8cfc15e

File tree

6 files changed

+353
-162
lines changed

6 files changed

+353
-162
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package org.jetbrains.kotlinx.jupyter.api
22

3-
import kotlin.reflect.KProperty
4-
import kotlin.reflect.KProperty1
5-
import kotlin.reflect.jvm.isAccessible
3+
import java.lang.reflect.Field
64

75
interface VariableState {
8-
val property: KProperty<*>
6+
// val property: KProperty<*>
7+
val property: Field
98
val scriptInstance: Any?
109
val stringValue: String?
1110
val value: Any?
1211
}
1312

1413
data class VariableStateImpl(
15-
override val property: KProperty1<Any, *>,
14+
// override val property: KProperty1<Any, *>,
15+
override val property: Field,
1616
override val scriptInstance: Any,
1717
) : VariableState {
1818
private var cachedValue: Any? = null

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

+15-6
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
@@ -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

Comments
 (0)