VirtualizingMatchingReferenceReaderFactory.kt
TLDR
The provided file, VirtualizingMatchingReferenceReaderFactory.kt
, contains the implementation of a class named VirtualizingMatchingReferenceReaderFactory
. This class is responsible for creating instances of ReferenceReader
that follow references from all HeapObject
s. It applies matching rules provided by referenceMatchers
and creates additional virtual instance references based on the list of VirtualInstanceReferenceReader
s created by virtualRefReadersFactory
.
Classes
VirtualizingMatchingReferenceReaderFactory
The VirtualizingMatchingReferenceReaderFactory
class creates instances of ReferenceReader
that follow references from all HeapObject
s. It takes in a list of ReferenceMatcher
s (referenceMatchers
) and a factory for creating VirtualInstanceReferenceReader
chains (virtualRefReadersFactory
). The createFor
method returns a DelegatingObjectReferenceReader
that delegates the reading of references to its internal readers, such as ClassReferenceReader
, ChainingInstanceReferenceReader
, and ObjectArrayReferenceReader
.
package shark
import shark.ChainingInstanceReferenceReader.VirtualInstanceReferenceReader
// TODO Move to shark-android once HeapAnalyzer is removed.
// TODO This should be configurable to provide your own ref readers. Maybe just
/**
* Creates [ReferenceReader] instances that will follow references from all [HeapObject]s,
* applying matching rules provided by [referenceMatchers], creating additional virtual instance
* reference based on the list of [VirtualInstanceReferenceReader] created by [virtualRefReadersFactory].
*/
class VirtualizingMatchingReferenceReaderFactory(
private val referenceMatchers: List<ReferenceMatcher>,
private val virtualRefReadersFactory: VirtualInstanceReferenceReader.ChainFactory
) : ReferenceReader.Factory<HeapObject> {
override fun createFor(heapGraph: HeapGraph): ReferenceReader<HeapObject> {
return DelegatingObjectReferenceReader(
classReferenceReader = ClassReferenceReader(heapGraph, referenceMatchers),
instanceReferenceReader = ChainingInstanceReferenceReader(
virtualRefReaders = virtualRefReadersFactory.createFor(heapGraph),
fieldRefReader = FieldInstanceReferenceReader(heapGraph, referenceMatchers)
),
objectArrayReferenceReader = ObjectArrayReferenceReader()
)
}
}