AppSingletonInspector.kt
TLDR
This file contains the implementation of the AppSingletonInspector
class, which is an inspector that automatically marks instances of the provided class names as not leaking because they are app-wide singletons.
Classes
AppSingletonInspector
The AppSingletonInspector
class is an implementation of the ObjectInspector
interface. It has a constructor that takes a variable number of class names for app singletons. The class provides an inspect
method that takes an ObjectReporter
parameter. If the heapObject
in the ObjectReporter
is an instance of HeapInstance
, the class hierarchy of the instance class is iterated over. If any of the class names provided in the constructor match the name of a class in the hierarchy, a not leaking reason is added to the ObjectReporter
indicating that the class is an app singleton.
package shark
import shark.HeapObject.HeapInstance
/**
* Inspector that automatically marks instances of the provided class names as not leaking
* because they're app wide singletons.
*/
class AppSingletonInspector(private vararg val singletonClasses: String) : ObjectInspector {
override fun inspect(
reporter: ObjectReporter
) {
if (reporter.heapObject is HeapInstance) {
reporter.heapObject.instanceClass
.classHierarchy
.forEach { heapClass ->
if (heapClass.name in singletonClasses) {
reporter.notLeakingReasons += "${heapClass.name} is an app singleton"
}
}
}
}
}