All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.shark.ObjectReporter.kt Maven / Gradle / Ivy

package shark

import shark.HeapObject.HeapInstance
import kotlin.reflect.KClass

/**
 * Enables [ObjectInspector] implementations to provide insights on [heapObject], which is
 * an object (class, instance or array) found in the heap.
 *
 * A given [ObjectReporter] only maps to one object in the heap, but is shared to many
 * [ObjectInspector] implementations and accumulates insights.
 */
class ObjectReporter constructor(val heapObject: HeapObject) {

  /**
   * Labels that will be visible on the corresponding [heapObject] in the leak trace.
   */
  val labels = linkedSetOf()

  /**
   * Reasons for which this object is expected to be unreachable (ie it's leaking).
   */
  val leakingReasons = mutableSetOf()

  /**
   * Reasons for which this object is expected to be reachable (ie it's not leaking).
   */
  val notLeakingReasons = mutableSetOf()

  /**
   * Runs [block] if [ObjectReporter.heapObject] is an instance of [expectedClass].
   */
  fun whenInstanceOf(
    expectedClass: KClass,
    block: ObjectReporter.(HeapInstance) -> Unit
  ) {
    whenInstanceOf(expectedClass.java.name, block)
  }

  /**
   * Runs [block] if [ObjectReporter.heapObject] is an instance of [expectedClassName].
   */
  fun whenInstanceOf(
    expectedClassName: String,
    block: ObjectReporter.(HeapInstance) -> Unit
  ) {
    val heapObject = heapObject
    if (heapObject is HeapInstance && heapObject instanceOf expectedClassName) {
      block(heapObject)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy