https://kotlinlang.org logo
#arrow-meta
Title
# arrow-meta
n

Nathan Bedell

09/26/2021, 5:27 PM
Alright, I know this isn't an arrow-meta question specifically, but I figured the folks here might be able to help: Does anyone know how to grab the package path where a class is located from a
KtClass
? Closest I've been able to get so far is
ktClass.parent
, which (assuming the
KtClass
sits at top level), I believe gives me a
PsiElement
that represents the file that the class sits in. However, even there, I can't see any way to get the package path where the class resides. I'm not seeing anything in the methods of either
KtClass
or
PsiElement
that would be helpful here. Any ideas?
For context here, I'm trying to make an arrow meta plugin that does some codegen with
Transform.newSources
-- and in the generated code, I need to be able to add an import statement for
KtClass
in question.
r

raulraja

09/26/2021, 7:43 PM
Hi @Nathan Bedell, if you use ClassDeclaration from the quote package you have access to a
descriptor
property in which you can get the package contained
If you are using plain KtClass you need to get the class descriptor with
org.jetbrains.kotlin.psi.KtPureElement.findClassDescriptor(bindingContext: org.jetbrains.kotlin.resolve.BindingContext): org.jetbrains.kotlin.descriptors.ClassDescriptor
or similar utility methods
n

Nathan Bedell

10/12/2021, 11:15 PM
Thanks @raulraja. I think I was actually doing the right thing before -- my issue was my test file didn't have a
package
declaration -- so of course all of the methods that should have returned a package were coming up empty!