@@ -26,7 +26,7 @@ import androidx.activity.ComponentActivity
26
26
import androidx.databinding.ViewDataBinding
27
27
import androidx.fragment.app.Fragment
28
28
import androidx.viewbinding.ViewBinding
29
- import java.lang.ClassCastException
29
+ import java.lang.reflect.InvocationTargetException
30
30
import java.lang.reflect.ParameterizedType
31
31
32
32
@@ -65,29 +65,22 @@ fun <VB : ViewBinding> Any.bindViewWithGeneric(view: View): VB =
65
65
}
66
66
67
67
private fun <VB : ViewBinding > withGenericBindingClass (any : Any , block : (Class <VB >) -> VB ): VB {
68
- any.allParameterizedType.forEach { parameterizedType ->
69
- parameterizedType.actualTypeArguments.forEach {
70
- try {
71
- return block.invoke(it as Class <VB >)
72
- } catch (e: NoSuchMethodException ) {
73
- } catch (e: ClassCastException ) {
68
+ var genericSuperclass = any.javaClass.genericSuperclass
69
+ var superclass = any.javaClass.superclass
70
+ while (superclass != null ) {
71
+ if (genericSuperclass is ParameterizedType ) {
72
+ genericSuperclass.actualTypeArguments.forEach {
73
+ try {
74
+ return block.invoke(it as Class <VB >)
75
+ } catch (e: NoSuchMethodException ) {
76
+ } catch (e: ClassCastException ) {
77
+ } catch (e: InvocationTargetException ) {
78
+ throw e.targetException
79
+ }
74
80
}
75
81
}
82
+ genericSuperclass = superclass.genericSuperclass
83
+ superclass = superclass.superclass
76
84
}
77
85
throw IllegalArgumentException (" There is no generic of ViewBinding." )
78
86
}
79
-
80
- private val Any .allParameterizedType: List <ParameterizedType >
81
- get() {
82
- val genericParameterizedType = mutableListOf<ParameterizedType >()
83
- var genericSuperclass = javaClass.genericSuperclass
84
- var superclass = javaClass.superclass
85
- while (superclass != null ) {
86
- if (genericSuperclass is ParameterizedType ) {
87
- genericParameterizedType.add(genericSuperclass)
88
- }
89
- genericSuperclass = superclass.genericSuperclass
90
- superclass = superclass.superclass
91
- }
92
- return genericParameterizedType
93
- }
0 commit comments