I really need to call a Kotlin method with reified...
# announcements
p
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
You can make a helper method with the specific type. (Maybe a compiler plugin)
r
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
Good idea, to just make a type-specific helper method to call.
@Roukanken unfortunately it’s not my method
e
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
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
if it is not, then it is not
😁 1