Getting class type from KtClass
I'd like to get the class type from a KtClass instance, but calling `
KtExpression.getType`
always returns null. I've been getting class type in other places successfully. I'm pretty new to this area of Kotlin and the PSI. I don't really understand the BindingContext, so I've just been trying different values, but nothing seems to work.
Given this code
class A {
val b = B("arst")
fun backDirect() = haha {
b.s
}
}
In the analysis code, I have a KtProperty that represents "val b". From that I want to get the type for "class A".
I run the following:
val property: KtProperty = //Got this earlier and it appears valid. Should be 'val b'
val containingClass = property.containingClass()
val classType = containingClass?.getType(containingClass.analyze())
"containingClass" is not null, and looking at "containingClass.text", I see the code I expect to see. However, "classType" is always null.
I've tried creating the binding context from a number of different "KtElement" values and resolve modes. None have worked. As mentioned, I don't really understand how all of that works, so I'm just guessing.
Any help would be appreciated. Especially if there's some sort of "intro to Kotlin's PSI model" documentation. I've found bits, but no real solid overview.
Thanks in advance!
Please sign in to leave a comment.
OK, learning as I go. I won't say I entirely understand how everything is routed, but I did figure this out. "resolveToDescriptorIfAny()" gets what I'm looking for.