LeakTraceReference.kt
TLDR
The LeakTraceReference.kt
file in the Demo Projects project defines a data class LeakTraceReference
that represents a reference from an origin LeakTraceObject
to another LeakTraceObject
in a leak trace. It also includes an enum class ReferenceType
that specifies the type of reference, and a few utility methods to get the class name, display name, and generic name of the reference.
Classes
LeakTraceReference
The LeakTraceReference
data class represents a reference from an origin LeakTraceObject
to another LeakTraceObject
in a leak trace. It has the following properties:
-
originObject
: The originLeakTraceObject
of the reference. -
referenceType
: The type of reference, specified using theReferenceType
enum class. -
owningClassName
: The name of the class that owns the reference. -
referenceName
: The name of the reference.
It also includes the following utility properties:
-
owningClassSimpleName
: Returns the simplified name of the owning class. -
referenceDisplayName
: Returns the display name of the reference based on its type. -
referenceGenericName
: Returns the generic name of the reference based on its type.
ReferenceType
The ReferenceType
enum class specifies the type of reference in a LeakTraceReference
. It has the following values:
-
INSTANCE_FIELD
: Reference to an instance field. -
STATIC_FIELD
: Reference to a static field. -
LOCAL
: Reference to a local variable. -
ARRAY_ENTRY
: Reference to an array entry.
END
package shark
import shark.LeakTraceReference.ReferenceType.ARRAY_ENTRY
import shark.LeakTraceReference.ReferenceType.INSTANCE_FIELD
import shark.LeakTraceReference.ReferenceType.LOCAL
import shark.LeakTraceReference.ReferenceType.STATIC_FIELD
import shark.internal.lastSegment
import java.io.Serializable
/**
* A [LeakTraceReference] represents an origin [LeakTraceObject] and either a reference from that
* object to the [LeakTraceObject] in the next [LeakTraceReference] in [LeakTrace.referencePath],
* or to [LeakTrace.leakingObject] if this is the last [LeakTraceReference] in
* [LeakTrace.referencePath].
*/
data class LeakTraceReference(
val originObject: LeakTraceObject,
val referenceType: ReferenceType,
val owningClassName: String,
val referenceName: String
) : Serializable {
enum class ReferenceType {
INSTANCE_FIELD,
STATIC_FIELD,
LOCAL,
ARRAY_ENTRY
}
/**
* Returns {@link #className} without the package, ie stripped of any string content before the
* last period (included).
*/
val owningClassSimpleName: String get() = owningClassName.lastSegment('.')
val referenceDisplayName: String
get() {
return when (referenceType) {
ARRAY_ENTRY -> "[$referenceName]"
STATIC_FIELD, INSTANCE_FIELD -> referenceName
LOCAL -> "<Java Local>"
}
}
val referenceGenericName: String
get() {
return when (referenceType) {
// The specific array index in a leak rarely matters, this improves grouping.
ARRAY_ENTRY -> "[x]"
STATIC_FIELD, INSTANCE_FIELD -> referenceName
LOCAL -> "<Java Local>"
}
}
companion object {
private const val serialVersionUID = 1L
}
}