Repository: square/leakcanary
File: shark/src/main/java/shark/internal/PathFinder.kt (Kotlin)

Code before:
  }

  private fun HeapInstance.readAllNonNullFieldsOfReferenceType(): MutableList<HeapField> {
    // Assigning to local variable to avoid repeated lookup and cast:
    // HeapInstance.graph casts HeapInstance.hprofGraph to HeapGraph in its getter
    val hprofGraph = graph
    var fieldReader: FieldValuesReader? = null
    val result = mutableListOf<HeapField>()
    var skipBytesCount = 0
    for (heapClass in instanceClass.classHierarchy.toList()) {
      for (fieldRecord in heapClass.readRecord().fields) {
        if (fieldRecord.type != PrimitiveType.REFERENCE_HPROF_TYPE) {
          // Skip all fields that are not references. Track how many bytes to skip
          skipBytesCount += hprofGraph.getRecordSize(fieldRecord)
        } else {
          // Initialize field reader if it's not yet initialized. Replaces `lazy` without synchronization
          if (fieldReader == null) {
            fieldReader = hprofGraph.createFieldValuesReader(readRecord())
          }

          // Skip the accumulated bytes offset
          fieldReader.skipBytes(skipBytesCount)
          skipBytesCount = 0

          val fieldValue = fieldReader.readValue(fieldRecord) as ReferenceHolder
          if (!fieldValue.isNull) {
            val fieldName = heapClass.fieldName(fieldRecord)
            result.add(HeapField(heapClass, fieldName, HeapValue(hprofGraph, fieldValue)))
          }
        }
      }
    }
    return result
  }

  private fun HeapGraph.getRecordSize(field: FieldRecord) =
    when (field.type) {
      PrimitiveType.REFERENCE_HPROF_TYPE -> identifierByteSize
      BOOLEAN.hprofType -> 1
      CHAR.hprofType -> 2
      FLOAT.hprofType -> 4
      DOUBLE.hprofType -> 8
      BYTE.hprofType -> 1
      SHORT.hprofType -> 2
      INT.hprofType -> 4
      LONG.hprofType -> 8
      else -> throw IllegalStateException("Unknown type ${field.type}")
    }

  private fun State.visitObjectArray(
    objectArray: HeapObjectArray,

Review comment (bug):
I wrote a big comment and GitHub lost it. Damn.

Anyway: as it stands the change exposes the wrong APIs. They were internal because their design wasn't quite right. We shouldn't make them public.

Is there a way to get the same benefits as we have here, with an alternate to HeapInstance.readFields() that's more manual maybe and doesn't involve exposing all those badly internal APIs?}



Code after:
            refFromParentType = INSTANCE_FIELD,
            refFromParentName = name
        )
        is LibraryLeakReferenceMatcher ->
          LibraryLeakChildNode(
              objectId = objectId,
              parent = parent,
              refFromParentType = INSTANCE_FIELD,
              refFromParentName = name,
              matcher = referenceMatcher
          )
        is IgnoredReferenceMatcher -> null
      }
      if (node != null) {
        enqueue(node)
      }
    }
  }

  private fun HeapInstance.readAllNonNullFieldsOfReferenceType(): MutableList<LongObjectPair<String>> {
    // Assigning to local variable to avoid repeated lookup and cast:
    // HeapInstance.graph casts HeapInstance.hprofGraph to HeapGraph in its getter
    val hprofGraph = graph
    var fieldReader: FieldIdReader? = null
    val result = mutableListOf<LongObjectPair<String>>()
    var skipBytesCount = 0

    for (heapClass in instanceClass.classHierarchyWithoutJavaLangObject()) {
      for (fieldRecord in heapClass.readRecord().fields) {
        if (fieldRecord.type != PrimitiveType.REFERENCE_HPROF_TYPE) {
          // Skip all fields that are not references. Track how many bytes to skip
          skipBytesCount += hprofGraph.getRecordSize(fieldRecord)
        } else {
          // Initialize id reader if it's not yet initialized. Replaces `lazy` without synchronization
          if (fieldReader == null) {
            fieldReader = FieldIdReader(readRecord(), hprofGraph.identifierByteSize)
          }

          // Skip the accumulated bytes offset
          fieldReader.skipBytes(skipBytesCount)
          skipBytesCount = 0

          val objectId = fieldReader.readId()
          if (objectId != 0L) {
            result.add(objectId to heapClass.instanceFieldName(fieldRecord))
          }
        }
      }
    }
    return result
  }