like I hava a kotlin function:
object KotlinFunc {
@JvmStatic
fun create(mycls: KClass<*>) {...}
}
I want to invoke this function in java code, but this not working:
public void javaFunc() {
KotlinFunc.create(MyClass.class)
}
It is possible by using calling the getKotlinClass
function in Java:
kotlin.jvm.JvmClassMappingKt.getKotlinClass(MyClass.class);
you can invoke your function like this:
kotlin.reflect.KClass<MyClass> kClass = kotlin.jvm.JvmClassMappingKt.getKotlinClass(MyClass.class);
KotlinFunc.create(kClass);
and make sure that kotlin-reflect
is in your classpath, e.g.:
with Gradle Kotlin:
dependencies {
...
implementation(kotlin("reflect"))
...
}
with Gradle Groovy:
dependencies {
...
implementation 'org.jetbrains.kotlin:kotlin-reflect'
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With