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

<lambda>null1 package shark
2 
3 import shark.HeapObject.HeapInstance
4 
5 /**
6  * Inspector that automatically marks instances of the provided class names as not leaking
7  * because they're app wide singletons.
8  */
9 class AppSingletonInspector(private vararg val singletonClasses: String) : ObjectInspector {
10   override fun inspect(
11     reporter: ObjectReporter
12   ) {
13     if (reporter.heapObject is HeapInstance) {
14       reporter.heapObject.instanceClass
15         .classHierarchy
16         .forEach { heapClass ->
17           if (heapClass.name in singletonClasses) {
18             reporter.notLeakingReasons += "${heapClass.name} is an app singleton"
19           }
20         }
21     }
22   }
23 }