Search code examples
kotlindetektintellij-platform-psi

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?


Solution

  • KtClass implemenents the KtNamedDeclaration interface which provides the fqName method.

    FqName getFqName();

    Which will give you what you want. So:

    klass.fqName.asString()