Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<VB : ViewDataBinding> {
Expand All @@ -12,13 +13,26 @@ internal sealed interface IBinding<VB : ViewDataBinding> {
companion object {
@Suppress("UNCHECKED_CAST")
internal fun <T : ViewBinding> IBinding<*>.inflateBinding(inflater: LayoutInflater): T {
return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
.asSequence()
.filterIsInstance<Class<T>>()
.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<Class<*>>()
?.firstOrNull { it.simpleName.endsWith("Binding") }
?.getDeclaredMethod("inflate", LayoutInflater::class.java)
?.also { it.isAccessible = true }
}
}
}
Expand Down