Search code examples
kotlinabstract-syntax-tree

How to get the type information of the ktExpression using kotlin PSI?


class Baz {

}

class Foo {

    fun anotherAction() : Baz {
        return Baz()
    }
}

class Bar {

    fun doAction() : Foo {
        return Foo()
    }
}

fun main() {
    Bar()
      .doAction()
      .doAnotherAction()
} 

Is there a way using kotlin PSI to get info about the type of the Bar().doAction() expression? I need to figure out that return type of Bar().doAction() or doAction() is Foo or that doAnotherAction is a function of the class Foo. Basically, I'm trying to get the types of the KtExpression-s

I have BindingContext generated (even though with some warnings) but trying to access the type info of the expression always returns null. I tried to use getResolvedCall(BindingContext) and getCall(bindingContext) but it also always returns null.

Also, accessing references and reference in the the above code expressions: KtDotQualifiedExpression and KtCallExpression also returns null.


Solution

  • BindingContext was generated properly, I was using the wrong file with psi Visitor that wasn't analyzed and so wasn't included in the BindingContext I used.