Kotlin: Equivalent of getClass() for KClass

JavaClassKotlin

Java Problem Overview


In Java we can resolve a variable's class through getClass() like something.getClass(). In Kotlin I am aware of something.javaClass which is nice but I want to be able to get the KClass in a similar way. I've seen the Something::class syntax but this is not what I need. I need to get the KClass of a variable. Does such functionality exist?

Java Solutions


Solution 1 - Java

The easiest way to achieve this since Kotlin 1.1 is the class reference syntax:

something::class

If you use Kotlin 1.0, you can convert the obtained Java class to a KClass instance by calling the .kotlin extension property:

something.javaClass.kotlin

Solution 2 - Java

EDIT: See comments, below, and answer from Alexander, above. This advice was originally for Kotlin 1.0 and it seems is now obsolete.

Since the language doesn't support a direct way to get this yet, consider defining an extension method for now.

fun<T: Any> T.getClass(): KClass<T> {
    return javaClass.kotlin
}

val test = 0
println("Kotlin type: ${test.getClass()}")

Or, if you prefer a property:

val<T: Any> T.kClass: KClass<T>
    get() = javaClass.kotlin

val test = 0
println("Kotlin type: ${test.kClass}")

Solution 3 - Java

Here's my solution

val TAG = javaClass.simpleName

With javaClass.simpleName you can obtain your class name. Also the above example is very useful for android developers to declare on top of the class as an instance variable for logging purposes.

Solution 4 - Java

Here are different Implementations to get class names. You can utilize it as per your requirements.

import kotlin.reflect.KClass

val <T : Any > T.kClassName: KClass<out T>
get() {
    return javaClass.kotlin
}

Here we can get the class name in kotlin

val <T : Any > T.classNameKotlin: String?
get() {
    return javaClass.kotlin.simpleName
}

Here we can get the class name in kotlin

val <T : Any > T.classNameJava: String
get() {
    return javaClass.simpleName
}

Here are the outputs to the following operations.

fun main(){

val userAge = 0

println(userAge.kClassName) 
Output: class java.lang.Integer (Kotlin reflection is not available)

println(userAge.classNameKotlin)
Output: Int

println(userAge.classNameJava)
Output: Integer
}

Solution 5 - Java

In Kotlin:

val className = serviceClass.javaClass.name

Solution 6 - Java

Since Kotlin 1.5.21 (org.jetbrains.kotlin:kotlin-stdlib:1.5.21)

val TAG = javaClass.simpleName will work no more!

Error "Not enough information to infer type variable T"

/**
 * Returns the runtime Java class of this object.
 */
public inline val <T : Any> T.javaClass: Class<T>
    @Suppress("UsePropertyAccessSyntax")
    get() = (this as java.lang.Object).getClass() as Class<T>

@Deprecated("Use 'java' property to get Java class corresponding to this Kotlin class or cast this instance to Any if you really want to get the runtime Java class of this implementation of KClass.", ReplaceWith("(this as Any).javaClass"), level = DeprecationLevel.ERROR)
public inline val <T : Any> KClass<T>.javaClass: Class<KClass<T>>
    @JvmName("getRuntimeClassOfKClassInstance")
    @Suppress("UsePropertyAccessSyntax")
    get() = (this as java.lang.Object).getClass() as Class<KClass<T>>

You can try now YourClassName::class.java.simpleName

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJireView Question on Stackoverflow
Solution 1 - JavaAlexander UdalovView Answer on Stackoverflow
Solution 2 - JavadhermanView Answer on Stackoverflow
Solution 3 - JavaKashif AnwaarView Answer on Stackoverflow
Solution 4 - JavaMuhammad MaqsoodView Answer on Stackoverflow
Solution 5 - JavaАлексей СуздаленкоView Answer on Stackoverflow
Solution 6 - JavaCryptoCodeView Answer on Stackoverflow