InternalSharkCollectionsHelper.kt
TLDR
The InternalSharkCollectionsHelper.kt
file in the shark.internal
package provides methods for working with collections in the Shark library.
Methods
arrayListValues(heapInstance: HeapInstance): Sequence<String>
This method takes a HeapInstance
object as input and returns a sequence of strings representing the values in an ArrayList. It uses various readers to determine if the input object is an ArrayList and reads the values accordingly.
className(graphObject: HeapObject): String
This private method takes a HeapObject
as input and returns its class name as a string. It handles different types of HeapObject
including HeapClass
, HeapInstance
, HeapObjectArray
, and HeapPrimitiveArray
.
package shark.internal
import shark.ApacheHarmonyInstanceRefReaders
import shark.HeapObject
import shark.HeapObject.HeapClass
import shark.HeapObject.HeapInstance
import shark.HeapObject.HeapObjectArray
import shark.HeapObject.HeapPrimitiveArray
import shark.OpenJdkInstanceRefReaders
/**
* INTERNAL
*
* This class is public to be accessible from other LeakCanary modules but shouldn't be
* called directly, the API may break at any point.
*/
object InternalSharkCollectionsHelper {
fun arrayListValues(heapInstance: HeapInstance): Sequence<String> {
val graph = heapInstance.graph
val arrayListReader = OpenJdkInstanceRefReaders.ARRAY_LIST.create(graph)
?: ApacheHarmonyInstanceRefReaders.ARRAY_LIST.create(graph)
?: return emptySequence()
if (!arrayListReader.matches(heapInstance)) {
return emptySequence()
}
return arrayListReader.read(heapInstance).map { reference ->
val arrayListValue = graph.findObjectById(reference.valueObjectId)
val details = reference.lazyDetailsResolver.resolve()
"[${details.name}] = ${className(arrayListValue)}"
}
}
private fun className(graphObject: HeapObject): String {
return when (graphObject) {
is HeapClass -> {
graphObject.name
}
is HeapInstance -> {
graphObject.instanceClassName
}
is HeapObjectArray -> {
graphObject.arrayClassName
}
is HeapPrimitiveArray -> {
graphObject.arrayClassName
}
}
}
}