xref: /aosp_15_r20/external/leakcanary2/shark/src/main/java/shark/ReferencePattern.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package shark
2 
3 import java.io.Serializable
4 
5 /**
6  * A pattern that will match references for a given [ReferenceMatcher].
7  */
8 sealed class ReferencePattern : Serializable {
9 
10   /**
11    * Matches local references held in the stack of frames of a given thread, identified by its name.
12    */
13   data class JavaLocalPattern(
14     val threadName: String
15   ) : ReferencePattern() {
toStringnull16     override fun toString() = "local variable on thread $threadName"
17 
18     companion object {
19       private const val serialVersionUID: Long = -8985446122829543654
20     }
21   }
22 
23   /**
24    * Matches static field references, identified by [className] and [fieldName].
25    */
26   data class StaticFieldPattern(
27     val className: String,
28     val fieldName: String
29   ) : ReferencePattern() {
toStringnull30     override fun toString() = "static field $className#$fieldName"
31 
32     companion object {
33       private const val serialVersionUID: Long = 7656908128775899611
34     }
35   }
36 
37   /**
38    * Matches instances field references, identified by [className] and [fieldName].
39    *
40    * Note: If [fieldName] is declared in a superclass it will still match for subclasses.
41    * This is to support overriding of rules for specific cases. If two [ReferenceMatcher] match for
42    * the same [fieldName] but for different [className] in a class hierarchy, then the closest
43    * class in the hierarchy wins.
44    */
45   data class InstanceFieldPattern(
46     val className: String,
47     val fieldName: String
48   ) : ReferencePattern() {
toStringnull49     override fun toString() = "instance field $className#$fieldName"
50 
51     companion object {
52       private const val serialVersionUID: Long = 6649791455204159802
53     }
54   }
55 
56   /**
57    * Matches native global variables (also known as jni global gc roots) that reference
58    * Java objects. The class name will match against classes, instances and object arrays with
59    * a matching class name.
60    */
61   data class NativeGlobalVariablePattern(val className: String) : ReferencePattern() {
toStringnull62     override fun toString() = "native global variable referencing $className"
63 
64     companion object {
65       private const val serialVersionUID: Long = -2651328076202244933
66     }
67   }
68 
69   companion object {
70     private const val serialVersionUID: Long = -5113635523713591133
71   }
72 }