@@ -28,6 +28,24 @@ data class ProcessedDescriptorsState(
28
28
val instancesPerState : MutableMap <SerializedVariablesState , Any ?> = mutableMapOf()
29
29
)
30
30
31
+ // TODO: add as a key map
32
+ data class RuntimeObjectWrapper (
33
+ val objectInstance : Any?
34
+ ) {
35
+ override fun equals (other : Any? ): Boolean {
36
+ if (other == null ) return objectInstance == null
37
+ if (objectInstance == null ) return false
38
+ if (other is RuntimeObjectWrapper ) return objectInstance == = other.objectInstance
39
+ return objectInstance == = other
40
+ }
41
+
42
+ override fun hashCode (): Int {
43
+ return objectInstance?.hashCode() ? : 0
44
+ }
45
+ }
46
+
47
+ fun Any?.toObjectWrapper (): RuntimeObjectWrapper = RuntimeObjectWrapper (this )
48
+
31
49
class VariablesSerializer (private val serializationStep : Int = 2 , private val serializationLimit : Int = 10000 ) {
32
50
33
51
/* *
@@ -36,7 +54,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
36
54
* Second Key: actual value
37
55
* Value: serialized VariableState
38
56
*/
39
- private val seenObjectsPerCell: MutableMap <Int , MutableMap <Any , SerializedVariablesState >> = mutableMapOf ()
57
+ private val seenObjectsPerCell: MutableMap <Int , MutableMap <RuntimeObjectWrapper , SerializedVariablesState >> = mutableMapOf ()
40
58
41
59
private var currentSerializeCount: Int = 0
42
60
@@ -96,15 +114,15 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
96
114
97
115
private fun serializeVariableState (cellId : Int , name : String , property : Field ? , value : Any? , isOverride : Boolean = true): SerializedVariablesState {
98
116
val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
99
- return doActualSerialization(cellId, processedData, value, isOverride)
117
+ return doActualSerialization(cellId, processedData, value.toObjectWrapper() , isOverride)
100
118
}
101
119
102
120
private fun serializeVariableState (cellId : Int , name : String , property : KProperty <* >, value : Any? , isOverride : Boolean = true): SerializedVariablesState {
103
121
val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
104
- return doActualSerialization(cellId, processedData, value, isOverride)
122
+ return doActualSerialization(cellId, processedData, value.toObjectWrapper() , isOverride)
105
123
}
106
124
107
- private fun doActualSerialization (cellId : Int , processedData : ProcessedSerializedVarsState , value : Any? , isOverride : Boolean = true): SerializedVariablesState {
125
+ private fun doActualSerialization (cellId : Int , processedData : ProcessedSerializedVarsState , value : RuntimeObjectWrapper , isOverride : Boolean = true): SerializedVariablesState {
108
126
fun isCanBeComputed (fieldDescriptors : MutableMap <String , SerializedVariablesState ?>): Boolean {
109
127
return (fieldDescriptors.isEmpty() || (fieldDescriptors.isNotEmpty() && fieldDescriptors.entries.first().value?.fieldDescriptor!! .isEmpty()))
110
128
}
@@ -120,7 +138,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
120
138
currentCellDescriptors!! .processedSerializedVarsToKProperties[serializedVersion] = processedData.propertiesData
121
139
// currentCellDescriptors.processedSerializedVarsToJvmFields[serializedVersion] = processedData.jvmOnlyFields
122
140
123
- if (value != null ) {
141
+ if (value.objectInstance != null ) {
124
142
seenObjectsPerCell[cellId]!! .putIfAbsent(value, serializedVersion)
125
143
}
126
144
if (serializedVersion.isContainer) {
@@ -129,14 +147,14 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
129
147
val previouslySerializedState = seenObjectsPerCell[cellId]!! [value] ? : return processedData.serializedVariablesState
130
148
serializedVersion.fieldDescriptor + = previouslySerializedState.fieldDescriptor
131
149
if (isCanBeComputed(serializedVersion.fieldDescriptor)) {
132
- iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
150
+ iterateThroughContainerMembers(cellId, value.objectInstance , serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
133
151
}
134
152
} else {
135
153
// add jvm descriptors
136
154
processedData.jvmOnlyFields?.forEach {
137
155
serializedVersion.fieldDescriptor[it.name] = serializeVariableState(cellId, it.name, it, value)
138
156
}
139
- iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
157
+ iterateThroughContainerMembers(cellId, value.objectInstance , serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
140
158
}
141
159
}
142
160
return processedData.serializedVariablesState
@@ -159,17 +177,17 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
159
177
}
160
178
it as KProperty1 <Any , * >
161
179
val name = it.name
162
- val value = tryGetValueFromProperty(it, callInstance)
180
+ val value = tryGetValueFromProperty(it, callInstance).toObjectWrapper()
163
181
164
182
if (! seenObjectsPerCell!! .containsKey(value)) {
165
183
serializedIteration[name] = createSerializeVariableState(name, getSimpleTypeNameFrom(it, value), value)
166
184
descriptor[name] = serializedIteration[name]!! .serializedVariablesState
167
185
}
168
186
if (descriptor[name] != null ) {
169
- instancesPerState[descriptor[name]!! ] = value
187
+ instancesPerState[descriptor[name]!! ] = value.objectInstance
170
188
}
171
189
172
- if (value != null && ! seenObjectsPerCell.containsKey(value)) {
190
+ if (! seenObjectsPerCell.containsKey(value)) {
173
191
if (descriptor[name] != null ) {
174
192
seenObjectsPerCell[value] = descriptor[name]!!
175
193
}
@@ -217,7 +235,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
217
235
else -> {
218
236
null
219
237
}
220
- }
238
+ }.toObjectWrapper()
221
239
if (isArrayType) {
222
240
if (callInstance is List <* >) {
223
241
callInstance.forEach { arrayElem ->
@@ -249,14 +267,14 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
249
267
it.value.jvmOnlyFields?.forEach { field ->
250
268
serializedVariablesState.fieldDescriptor[field.name] = serializeVariableState(cellId, field.name, field, neededCallInstance)
251
269
val properInstance = serializedVariablesState.fieldDescriptor[field.name]
252
- instancesPerState[properInstance!! ] = neededCallInstance
253
- seenObjectsPerCell?.set(neededCallInstance!! , serializedVariablesState)
270
+ instancesPerState[properInstance!! ] = neededCallInstance.objectInstance
271
+ seenObjectsPerCell?.set(neededCallInstance, serializedVariablesState)
254
272
}
255
273
computedDescriptorsPerCell[cellId]!! .instancesPerState + = instancesPerState
256
274
// computedDescriptorsPerCell[cellId]!!.processedSerializedVarsToJvmFields[serializedVariablesState] = it.value.jvmOnlyFields
257
275
iterateThroughContainerMembers(
258
276
cellId,
259
- neededCallInstance,
277
+ neededCallInstance.objectInstance ,
260
278
serializedVariablesState.fieldDescriptor,
261
279
it.value.propertiesData,
262
280
currentDepth + 1
@@ -289,6 +307,14 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
289
307
}
290
308
291
309
private fun createSerializeVariableState (name : String , simpleTypeName : String? , value : Any? ): ProcessedSerializedVarsState {
310
+ return doCreateSerializedVarsState(simpleTypeName, value)
311
+ }
312
+
313
+ private fun createSerializeVariableState (name : String , simpleTypeName : String? , value : RuntimeObjectWrapper ): ProcessedSerializedVarsState {
314
+ return doCreateSerializedVarsState(simpleTypeName, value.objectInstance)
315
+ }
316
+
317
+ private fun doCreateSerializedVarsState (simpleTypeName : String? , value : Any? ): ProcessedSerializedVarsState {
292
318
// make it exception-safe
293
319
val membersProperties = try {
294
320
if (value != null ) value::class .declaredMemberProperties else null
0 commit comments