From 37994d87bdd6380b7b7da234c0427b12a59a04f8 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Mar 2023 21:00:02 +0800 Subject: [PATCH 1/4] Filter all binding type arguments --- .../demoapp/common/base/binding/IBinding.kt | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) 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..af6a4b809 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,25 @@ 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 + } + } + throw IllegalStateException("No binding found.") + } + + private fun Class<*>.filterBindingMethod(): Method? { + return (genericSuperclass as? ParameterizedType)?.actualTypeArguments + ?.filterIsInstance>() + ?.firstOrNull { it.simpleName.endsWith("Binding") } + ?.getDeclaredMethod("inflate", LayoutInflater::class.java) + ?.also { it.isAccessible = true } } } } From 4846ee1b36a1d5dff15b5d73568e704c707d30b2 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Mar 2023 21:52:22 +0800 Subject: [PATCH 2/4] Use error --- .../kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 af6a4b809..83b9bffe0 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 @@ -23,7 +23,7 @@ internal sealed interface IBinding { return method.invoke(null, inflater) as T } } - throw IllegalStateException("No binding found.") + error("No binding found.") } private fun Class<*>.filterBindingMethod(): Method? { From 48c15d5354e7f4b1f4b4defcf8ace695d5c745b0 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 30 Mar 2023 12:05:37 +0800 Subject: [PATCH 3/4] Refine error --- .../kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 83b9bffe0..47ba50d50 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 @@ -23,7 +23,7 @@ internal sealed interface IBinding { return method.invoke(null, inflater) as T } } - error("No binding found.") + error("No Binding type argument found.") } private fun Class<*>.filterBindingMethod(): Method? { From f81127e8c098d75b2f43e7b3cc68aa03fd1e22df Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 30 Mar 2023 12:06:38 +0800 Subject: [PATCH 4/4] Use Sequence --- .../kotlin/io/goooler/demoapp/common/base/binding/IBinding.kt | 1 + 1 file changed, 1 insertion(+) 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 47ba50d50..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 @@ -28,6 +28,7 @@ internal sealed interface IBinding { private fun Class<*>.filterBindingMethod(): Method? { return (genericSuperclass as? ParameterizedType)?.actualTypeArguments + ?.asSequence() ?.filterIsInstance>() ?.firstOrNull { it.simpleName.endsWith("Binding") } ?.getDeclaredMethod("inflate", LayoutInflater::class.java)