Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do a get a fully qualified domain name of class in Kotlin PSI?

I have a Kotlin data class:

package a.b.c

data class Example(
    …
)

I am analyzing it with detekt which provides access to the Kotlin PSI.

I'm trying to get the FQDN of my class:

println(klass.nameAsName?.identifier)

where, klass has a type of KtClass from Kotlin PSI. But that code prints just a short name of my class, like Example, whereas I want to get a.b.c.Example.

How do a get a fully qualified domain name of class in Kotlin PSI?

like image 521
madhead - StandWithUkraine Avatar asked Dec 19 '25 22:12

madhead - StandWithUkraine


1 Answers

KtClass implemenents the KtNamedDeclaration interface which provides the fqName method.

FqName getFqName();

Which will give you what you want. So:

klass.fqName.asString()

like image 126
sksamuel Avatar answered Dec 21 '25 15:12

sksamuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!