Converting Kotlin's KClass to regular Class in Java

The functionality does exist, just not where it seems to, as java is an extension property.

Use the method JvmClassMappingKt.getJavaClass.

In Kotlin, extension methods (and property getters/setters) are implemented as static methods of their containing class. If you look at the source for .java (Ctrl+Q), you can see that it is implemented in JvmClassMapping.kt.

As the function is package-level and does not have a containing object, it is simply placed into the file [Filename]Kt which in this case is JvmClassMappingKt.

Here is the source of this extension property:

@Suppress("UPPER_BOUND_VIOLATED")
public val <T> KClass<T>.java: Class<T>
    @JvmName("getJavaClass")
    get() = (this as ClassBasedDeclarationContainer).jClass as Class<T>

As you can see, the method's name is renamed on the JVM to getJavaClass.

In your case, you can try:

public <T> T proxy(KClass<T> kClass) { 
    return (T) proxy(JvmClassMappingKt.getJavaClass(kClass)); 
}

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

Tags:

Kotlin