main

square/leakcanary

Last updated at: 29/12/2023 09:38

Objects.kt

TLDR

This file provides a utility function noOpDelegate() which creates a no-op delegate for a given class.

Methods

noOpDelegate()

The noOpDelegate() method is an inline function that creates a no-op delegate for a given class. It takes a reified type parameter T which represents the class of the delegate. The method returns an instance of the delegate.

Classes

None

package leakcanary.internal

import java.lang.reflect.InvocationHandler
import java.lang.reflect.Proxy

internal inline fun <reified T : Any> noOpDelegate(): T {
  val javaClass = T::class.java
  return Proxy.newProxyInstance(
    javaClass.classLoader, arrayOf(javaClass), NO_OP_HANDLER
  ) as T
}

private val NO_OP_HANDLER = InvocationHandler { _, _, _ ->
  // no op
}