0
Follow
2
View

How to get the relative class name of data classes

duanyu11111 注册会员
2023-01-24 22:00

This is possible using the kotlin-reflect artifact as a dependency. https://kotlinlang.org/docs/reflection.html#jvm-dependency

In my Maven project I add a new dependency:


  
    org.jetbrains.kotlin
    kotlin-reflect
    1.7.0
  

I didn't use Gradle for this, but I think you'd use

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.0")
}

to include it.

I made a data class similar to yours in a package named innerclass:

package innerclass

data class MyClass(
    val inner: MyInnerClass
) {
    data class MyInnerClass(val foo:String)
    data class AnotherInnerClass(val bar:String)
}

Then my Main.kt looks like this:

import innerclass.MyClass
import kotlin.reflect.KClass

fun main(args: Array) {
    println("Using class type as reference: ${MyClass::class.qualifiedName}")
    printRelativeClassNames(MyClass::class.nestedClasses)

    val test = MyClass(MyClass.MyInnerClass("foo"))
    println("\nUsing instance as reference: ${test::class.qualifiedName}")
    printRelativeClassNames(test::class.nestedClasses)
}

private val KClass<*>.packageFqName: String?
    get() {
        return java.`package`.name
    }

private val KClass<*>.relativeClassName: String?
    get() {
        return qualifiedName?.removePrefix("${packageFqName}.")
    }

fun printRelativeClassNames(nestedClasses: Collection>) {
    nestedClasses.forEach {
        println("Actual Kotlin class: $it")
        println(it.relativeClassName)
    }
}

When I run the program, it outputs:

Using class type as reference: innerclass.MyClass
Actual Kotlin class: class innerclass.MyClass$AnotherInnerClass
MyClass.AnotherInnerClass
Actual Kotlin class: class innerclass.MyClass$MyInnerClass
MyClass.MyInnerClass

Using instance as reference: innerclass.MyClass
Actual Kotlin class: class innerclass.MyClass$AnotherInnerClass
MyClass.AnotherInnerClass
Actual Kotlin class: class innerclass.MyClass$MyInnerClass
MyClass.MyInnerClass

Using reflection (and a little string manipulation) I can get the Kotlin class as well as print out the relative name of the class.

Let me know if you hit any issues.

About the Author

Question Info

Publish Time
2023-01-24 22:00
Update Time
2023-01-24 22:00