简体   繁体   English

如何使用 kotlin PSI 获取 ktExpression 的类型信息?

[英]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?有没有办法使用 kotlin PSI 来获取有关Bar().doAction()表达式类型的信息? 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 .我需要弄清楚Bar().doAction()doAction()的返回类型是Foo或者doAnotherAction是类Foo的函数。 Basically, I'm trying to get the types of the KtExpression -s基本上,我正在尝试获取KtExpression -s 的类型

I have BindingContext generated (even though with some warnings) but trying to access the type info of the expression always returns null.我生成了BindingContext (即使有一些警告),但尝试访问表达式的类型信息总是返回 null。 I tried to use getResolvedCall(BindingContext) and getCall(bindingContext) but it also always returns null.我尝试使用getResolvedCall(BindingContext)getCall(bindingContext)但它也总是返回 null。

Also, accessing references and reference in the the above code expressions: KtDotQualifiedExpression and KtCallExpression also returns null.此外,访问上述代码表达式中的referencesreferenceKtDotQualifiedExpressionKtCallExpression也返回 null。

BindingContext生成正确,我使用了错误的文件和未分析的 psi 访问者,因此未包含在我使用的BindingContext中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM