main

square/leakcanary

Last updated at: 29/12/2023 09:38

DelegatingObjectReferenceReader.kt

TLDR

The DelegatingObjectReferenceReader class is an internal class that implements the ReferenceReader interface. It delegates the reading of object references to different reference readers based on the type of the object.

Classes

DelegatingObjectReferenceReader

The DelegatingObjectReferenceReader class is responsible for reading object references. It implements the ReferenceReader<HeapObject> interface. This class has the following properties:

  • classReferenceReader: A reference reader specifically for HeapClass objects.
  • instanceReferenceReader: A reference reader specifically for HeapInstance objects.
  • objectArrayReferenceReader: A reference reader specifically for HeapObjectArray objects.

The class has a single method:

read(source: HeapObject): Sequence<Reference>

This method takes a source object of type HeapObject and returns a sequence of Reference objects.

  • If the source object is of type HeapClass, the method delegates the reading of references to the classReferenceReader and returns the result.
  • If the source object is of type HeapInstance, the method delegates the reading of references to the instanceReferenceReader and returns the result.
  • If the source object is of type HeapObjectArray, the method delegates the reading of references to the objectArrayReferenceReader and returns the result.
  • If the source object is of type HeapPrimitiveArray, the method returns an empty sequence.
package shark

import shark.HeapObject.HeapClass
import shark.HeapObject.HeapInstance
import shark.HeapObject.HeapObjectArray
import shark.HeapObject.HeapPrimitiveArray

internal class DelegatingObjectReferenceReader(
  private val classReferenceReader: ReferenceReader<HeapClass>,
  private val instanceReferenceReader: ReferenceReader<HeapInstance>,
  private val objectArrayReferenceReader: ReferenceReader<HeapObjectArray>,
) : ReferenceReader<HeapObject> {
  override fun read(source: HeapObject): Sequence<Reference> {
    return when(source) {
      is HeapClass -> classReferenceReader.read(source)
      is HeapInstance -> instanceReferenceReader.read(source)
      is HeapObjectArray -> objectArrayReferenceReader.read(source)
      is HeapPrimitiveArray -> emptySequence()
    }
  }
}