Repository: square/leakcanary
File: shark/shark-android/src/main/java/shark/AndroidReferenceMatchers.kt (Kotlin)

Code before:
    override fun add(references: MutableList<ReferenceMatcher>) {
      AndroidReferenceMatchers.nativeGlobalVariableLeak(
        className = "android.service.dreams.DreamService\$1",
        description = """
          DreamService leaks a binder stub.
          https://github.com/square/leakcanary/issues/2534
        """.trimIndent()
      ) { sdkInt >= 33 }
    }
  },

  UI_MODE_MANAGER {
    override fun add(
      references: MutableList<ReferenceMatcher>
    ) {
      references += instanceFieldLeak(
        "android.app.UiModeManager$1", "this$0",
        description = """
          UiModeManager$1 is an anonymous class of the IUiModeManagerCallback.Stub that is
          stored in memory native code. `this$0` is an instance of the UiModeManager that
          contains the `mContext` field, which is why retain this reference.
          Introduced in Android 14.0.0_r11: https://cs.android.com/android/_/android/platform/frameworks/base/+/cbbc772a41d20645ae434d74c482f3f4ad377e2c
          Fixed in Android 14.0.0_r16: https://cs.android.com/android/_/android/platform/frameworks/base/+/2bc364179327022d0f60224a1f2420349074c5d2
        """.trimIndent()
      ) {
        sdkInt == 34
      }
    }
  },

  // ######## Manufacturer specific known leaks ########

  // SAMSUNG

  SPEN_GESTURE_MANAGER {
    override fun add(
      references: MutableList<ReferenceMatcher>
    ) {
      references += staticFieldLeak(
        "com.samsung.android.smartclip.SpenGestureManager", "mContext",
        description =
        "SpenGestureManager has a static mContext field that leaks a reference to the" + " activity. Yes, a STATIC mContext field."
      ) {
        manufacturer == SAMSUNG && sdkInt == 19
      }
    }
  },

  CLIPBOARD_UI_MANAGER__SINSTANCE {
    override fun add(
      references: MutableList<ReferenceMatcher>

Review comment (suggestion):
Thanks for this, however for these stub leaks we typically reference them as a global native ref to the instance directly, because the core issue is with stubs sticking around in memory long after they're needed, and that way any reference from the stub to elsewhere is also included in this.

Code after:
    override fun add(references: MutableList<ReferenceMatcher>) {
      AndroidReferenceMatchers.nativeGlobalVariableLeak(
        className = "android.service.dreams.DreamService\$1",
        description = """
          DreamService leaks a binder stub.
          https://github.com/square/leakcanary/issues/2534
        """.trimIndent()
      ) { sdkInt >= 33 }
    }
  },

  UI_MODE_MANAGER {
    override fun add(
      references: MutableList<ReferenceMatcher>
    ) {
      references += nativeGlobalVariableLeak(
        "android.app.UiModeManager\$1",
        description = """
          UiModeManager$1 is an anonymous class of the IUiModeManagerCallback.Stub that is
          stored in memory native code. `this$0` is an instance of the UiModeManager that
          contains the `mContext` field, which is why retain this reference.
          Introduced in Android 14.0.0_r11: https://cs.android.com/android/_/android/platform/frameworks/base/+/cbbc772a41d20645ae434d74c482f3f4ad377e2c
          Fixed in Android 14.0.0_r16: https://cs.android.com/android/_/android/platform/frameworks/base/+/2bc364179327022d0f60224a1f2420349074c5d2
        """.trimIndent()
      ) {
        sdkInt == 34
      }
    }
  },

  // ######## Manufacturer specific known leaks ########

  // SAMSUNG

  SPEN_GESTURE_MANAGER {
    override fun add(
      references: MutableList<ReferenceMatcher>
    ) {
      references += staticFieldLeak(
        "com.samsung.android.smartclip.SpenGestureManager", "mContext",
        description =
        "SpenGestureManager has a static mContext field that leaks a reference to the" + " activity. Yes, a STATIC mContext field."
      ) {
        manufacturer == SAMSUNG && sdkInt == 19
      }
    }
  },

  CLIPBOARD_UI_MANAGER__SINSTANCE {
    override fun add(
      references: MutableList<ReferenceMatcher>