Hello, I’m trying have interoperability with java ...
# ktor
a
Hello, I’m trying have interoperability with java and Im using the ktor client and which to receive the body of a response but since
receive
method is an inline reified I cannot call it from java? Is there any other solution?
I came up with this
Copy code
suspend fun <T> HttpResponse.receive(`class`: Class<T>): T =
    call.receive(typeInfo(`class`)) as T

@OptIn(ExperimentalStdlibApi::class)
fun <T> typeInfo(`class`: Class<T>): TypeInfo {
    val kClass = Reflection.createKotlinClass(`class`)
    val kType = kClass.createType()
    val reifiedType = kType.javaType
    return typeInfoImpl(reifiedType, kClass, kType)
}
But not sure if I can just create a Kotlin class like that 😛
Somehow it worked ✌️ (still not sure if its the best way tho)
s
There is also
.kotlin
extension on
Class<T>
in Kotlin Std. Can you not rely on this? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/java.lang.-class/
I am wondering how you call this from Java? Just by providing a
Continuation
callback?
a
I just wrap it into a mono or call
runBlocking
using
kotlinx-coroutines-reactor
s
I can access it fine 🤔 I don't even need a import for it.
Copy code
val x: Class<String> = String::class.java
val y: KClass<String> = x.kotlin
I am using
implementation("org.jetbrains.kotlin:kotlin-stdlib")
with plugin
org.jetbrains.kotlin.jvm