xref: /aosp_15_r20/external/leakcanary2/shark/src/main/java/shark/ReferenceMatcher.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)

<lambda>null1 package shark
2 
3 /**
4  * Used to pattern match known patterns of references in the heap, either to ignore them
5  * ([IgnoredReferenceMatcher]) or to mark them as library leaks ([LibraryLeakReferenceMatcher]).
6  */
7 sealed class ReferenceMatcher {
8 
9   /** The pattern that references will be matched against. */
10   abstract val pattern: ReferencePattern
11 }
12 
13 /**
14  * [LibraryLeakReferenceMatcher] should be used to match references in library code that are
15  * known to create leaks and are beyond your control. The shortest path finder will only go
16  * through matching references after it has exhausted references that don't match, prioritizing
17  * finding an application leak over a known library leak. Library leaks will be reported as
18  * [LibraryLeak] instead of [ApplicationLeak].
19  */
20 data class LibraryLeakReferenceMatcher(
21   override val pattern: ReferencePattern,
22   /**
23    * A description that conveys what we know about this library leak.
24    */
25   val description: String = "",
26   /**
27    * Whether the identified leak may exist in the provided [HeapGraph]. Defaults to true. If
28    * the heap dump comes from a VM that runs a different version of the library that doesn't
29    * have the leak, then this should return false.
30    */
<lambda>null31   val patternApplies: (HeapGraph) -> Boolean = { true }
32 ) : ReferenceMatcher() {
toStringnull33   override fun toString() = "library leak: $pattern"
34 }
35 
36 /**
37  * [IgnoredReferenceMatcher] should be used to match references that cannot ever create leaks. The
38  * shortest path finder will never go through matching references.
39  */
40 class IgnoredReferenceMatcher(override val pattern: ReferencePattern) : ReferenceMatcher() {
41   override fun toString() = "ignored ref: $pattern"
42 }
43 
filterFornull44 internal fun Iterable<ReferenceMatcher>.filterFor(graph: HeapGraph): List<ReferenceMatcher> {
45   return filter { matcher ->
46     (matcher is IgnoredReferenceMatcher ||
47       (matcher is LibraryLeakReferenceMatcher && matcher.patternApplies(graph)))
48   }
49 }
50