Converting Kotlin's KClass to regular Class in Java

Viewed 4847

I am trying call a regular Java method in a Java code as follows:

public <T> T proxy(KClass<T> kClass) { 
    // unfortunately nothing like getJavaClass() exists
    return (T) proxy(kClass.getJavaClass()); 
}

public <T> T proxy(Class<T> jClass) {
    return (T) context.getBean(jClass);
}

In Kotlin, you can call .java on each KClass. This is not the case here and I am unable to extract the Java Class object from KClass. Is there a way to do it?

EDIT: This is trivial in Kotlin, but I am looking for solution in Java code.

2 Answers

You can try to use javaObjectType on your KClass

The explanation:

Returns a Java [Class] instance corresponding to the given [KClass] instance. In case of primitive types it returns corresponding wrapper classes.

E.g.

Boolean::class.javaObjectType

Related