https://kotlinlang.org logo
#ktor
Title
# ktor
a

André Martins

06/28/2022, 8:23 AM
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

simon.vergauwen

06/28/2022, 11:39 AM
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

André Martins

06/28/2022, 2:06 PM
I just wrap it into a mono or call
runBlocking
using
kotlinx-coroutines-reactor
s

simon.vergauwen

06/28/2022, 2:44 PM
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
3 Views