I really need to call a Kotlin method with reified type parameter from Java. Is there no workaround? I am willing to add utilities or even use reflection if needed.
d
Dominaezzz
04/20/2021, 8:25 PM
You can make a helper method with the specific type. (Maybe a compiler plugin)
r
Roukanken
04/20/2021, 8:28 PM
if it's your reified method, you could probably refactor it to call a non-reified overload, but with extra
Class<T>
parameter
if not, it might have that overload anyways
p
poohbar
04/20/2021, 8:28 PM
Good idea, to just make a type-specific helper method to call.
poohbar
04/20/2021, 8:28 PM
@Roukanken unfortunately itโs not my method
e
ephemient
04/20/2021, 9:14 PM
an
inline fun
(which is the only way
<reified T>
is allowed) is not callable on JVM. it is compiled to a chunk of bytecode which stored in the
@kotlin.Metadata
of the containing class. when a Kotlin caller calls it, the Kotlin compiler copy-pastes that bytecode at the callsite. there is no way to do that in Java, so you'd have to replicate the function in Java instead.
๐ 1
ephemient
04/20/2021, 9:16 PM
if all the
inline fun <reified T>
function does is call
T::class.java
then pass it on to another (non-inline) function, then it is easy to replicate the function in Java