Greg Rynkowski
01/26/2022, 10:07 AM// all T - java classes implementing ViewDataBinding, each providing a static `bind` method
// ViewDataBinding - a Java abstract class (with no declaration of `bind`)
interface BindingAware<T : ViewDataBinding> {
var f: T
fun bind(v: View) {
// f = T.bind(a) // <= how to call static method provided by class behind T?
}
}
How to call static method of T, having only T?Ruckus
01/26/2022, 4:27 PMimplementing, each providing a staticViewDataBinding
methodbind
Unfortunately, that static method is not part of the interface definition, so there's no way for the compiler to resolve.
Ruckus
01/26/2022, 4:36 PMT
with any knowledge of the associated class statics. If all subclasses are known, you could use a lookup table, otherwise you'll likely need some reflection magic (or pass a reference to the static method in as a member of any implementation of BindingAware
, but that seems to defeat the purpose).Greg Rynkowski
01/27/2022, 2:18 PMinterface ViewBindingAware<T : ViewDataBinding> {
var b: T
}
inline fun <reified T : ViewDataBinding> ViewBindingAware<T>.bindView(v: View): T {
val bindMethod = T::class.java.getMethod("bind", View::class.java)
b = bindMethod.invoke(null, v) as T
return b
}
and it works but I don't like too much reflection-based solutions and I left it on a side for now