Skip to content

Commit be2cb41

Browse files
authored
Merge pull request fwcd#47 from fwcd/better-cyclic-representation
Reuse variable references for better representation of cyclic references
2 parents c987ae2 + 8d61588 commit be2cb41

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

adapter/src/main/kotlin/org/javacs/ktda/core/scope/VariableTreeNode.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.javacs.ktda.core.scope
22

3+
import org.javacs.ktda.util.Identifiable
4+
35
/**
46
* A descriptor for a collection of child variables.
57
* (usually a scope or a variable's fields)
68
*/
7-
interface VariableTreeNode {
9+
interface VariableTreeNode : Identifiable {
810
val name: String
911
val value: String?
1012
get() = null

adapter/src/main/kotlin/org/javacs/ktda/jdi/scope/JDIVariable.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import com.sun.jdi.Type
1111

1212
class JDIVariable(
1313
override val name: String,
14-
jdiValue: Value?,
14+
private val jdiValue: Value?,
1515
jdiType: Type? = null
1616
) : VariableTreeNode {
1717
override val value: String = jdiValue?.toString() ?: "null" // TODO: Better string representation
1818
override val type: String = (jdiType?.name() ?: jdiValue?.type()?.name()) ?: "Unknown type"
1919
override val childs: List<VariableTreeNode>? by lazy { jdiValue?.let(::childrenOf) }
20+
override val id: Long? = (jdiValue as? ObjectReference)?.uniqueID() ?: (jdiValue as? ArrayReference)?.uniqueID()
2021

2122
private fun childrenOf(jdiValue: Value): List<VariableTreeNode> {
2223
val jdiType = jdiValue.type()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.javacs.ktda.util
2+
3+
public interface Identifiable {
4+
val id: Long?
5+
get() = null
6+
}

adapter/src/main/kotlin/org/javacs/ktda/util/ObjectPool.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.javacs.ktda.util
22

3+
import org.javacs.ktda.util.Identifiable
4+
35
private data class ObjectKey<O>(
46
val id: Long,
57
val owner: O
@@ -32,16 +34,14 @@ class ObjectPool<O, V> {
3234

3335
/** Stores an object and returns its (unique) id */
3436
fun store(owner: O, value: V): Long {
35-
val id = currentID
37+
val id = (value as? Identifiable)?.id ?: nextID()
3638
val key = ObjectKey(id, owner)
3739
val mapping = ObjectMapping(key, value)
3840

3941
mappingsByID[id] = mapping
4042
mappingsByOwner.putIfAbsent(owner, mutableSetOf())
4143
mappingsByOwner[owner]!!.add(mapping)
4244

43-
currentID += 1
44-
4545
return id
4646
}
4747

@@ -74,4 +74,14 @@ class ObjectPool<O, V> {
7474
.orEmpty()
7575

7676
fun containsID(id: Long) = mappingsByID.contains(id)
77+
78+
private fun nextID(): Long {
79+
var id = currentID
80+
81+
while (containsID(id)) {
82+
id += 1
83+
}
84+
85+
return id
86+
}
7787
}

0 commit comments

Comments
 (0)