diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt b/common/src/main/kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt index 8144eda52..b68afee42 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt @@ -4,6 +4,7 @@ import android.view.LayoutInflater import androidx.annotation.MainThread import androidx.databinding.ViewDataBinding import androidx.viewbinding.ViewBinding +import java.lang.reflect.Method import java.lang.reflect.ParameterizedType internal sealed interface IBinding { @@ -12,13 +13,26 @@ internal sealed interface IBinding { companion object { @Suppress("UNCHECKED_CAST") internal fun IBinding<*>.inflateBinding(inflater: LayoutInflater): T { - return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments - .asSequence() - .filterIsInstance>() - .first { it.simpleName.endsWith("Binding") } - .getDeclaredMethod("inflate", LayoutInflater::class.java) - .also { it.isAccessible = true } - .invoke(null, inflater) as T + var method: Method? + var clazz: Class<*> = javaClass + while (clazz.superclass != null) { + method = clazz.filterBindingMethod() + if (method == null) { + clazz = clazz.superclass + } else { + return method.invoke(null, inflater) as T + } + } + error("No Binding type argument found.") + } + + private fun Class<*>.filterBindingMethod(): Method? { + return (genericSuperclass as? ParameterizedType)?.actualTypeArguments + ?.asSequence() + ?.filterIsInstance>() + ?.firstOrNull { it.simpleName.endsWith("Binding") } + ?.getDeclaredMethod("inflate", LayoutInflater::class.java) + ?.also { it.isAccessible = true } } } }