xref: /aosp_15_r20/external/leakcanary2/shark/src/main/java/shark/LeakTraceReference.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package shark
2 
3 import shark.LeakTraceReference.ReferenceType.ARRAY_ENTRY
4 import shark.LeakTraceReference.ReferenceType.INSTANCE_FIELD
5 import shark.LeakTraceReference.ReferenceType.LOCAL
6 import shark.LeakTraceReference.ReferenceType.STATIC_FIELD
7 import shark.internal.lastSegment
8 import java.io.Serializable
9 
10 /**
11  * A [LeakTraceReference] represents an origin [LeakTraceObject] and either a reference from that
12  * object to the [LeakTraceObject] in the next [LeakTraceReference] in [LeakTrace.referencePath],
13  * or to [LeakTrace.leakingObject] if this is the last [LeakTraceReference] in
14  * [LeakTrace.referencePath].
15  */
16 data class LeakTraceReference(
17   val originObject: LeakTraceObject,
18 
19   val referenceType: ReferenceType,
20 
21   val owningClassName: String,
22 
23   val referenceName: String
24 
25 ) : Serializable {
26 
27   enum class ReferenceType {
28     INSTANCE_FIELD,
29     STATIC_FIELD,
30     LOCAL,
31     ARRAY_ENTRY
32   }
33 
34   /**
35    * Returns {@link #className} without the package, ie stripped of any string content before the
36    * last period (included).
37    */
38   val owningClassSimpleName: String get() = owningClassName.lastSegment('.')
39 
40   val referenceDisplayName: String
41     get() {
42       return when (referenceType) {
43         ARRAY_ENTRY -> "[$referenceName]"
44         STATIC_FIELD, INSTANCE_FIELD -> referenceName
45         LOCAL -> "<Java Local>"
46       }
47     }
48 
49   val referenceGenericName: String
50     get() {
51       return when (referenceType) {
52         // The specific array index in a leak rarely matters, this improves grouping.
53         ARRAY_ENTRY -> "[x]"
54         STATIC_FIELD, INSTANCE_FIELD -> referenceName
55         LOCAL -> "<Java Local>"
56       }
57     }
58 
59   companion object {
60     private const val serialVersionUID = 1L
61   }
62 }
63