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

1k views Asked by At

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?

1

There are 1 answers

0
sksamuel On BEST ANSWER

KtClass implemenents the KtNamedDeclaration interface which provides the fqName method.

FqName getFqName();

Which will give you what you want. So:

klass.fqName.asString()