How do I get a method return type?

Skip to first unread message

Mz Zz

Oct 18, 2021, 10:10:42 AM10/18/21
to lint-dev

I'm trying to get method return type in a way which works for both Kotlin and Java. I'm overriding visitMethod but when I call uMethod.returnType.canonicalText then it works in Java but for Kotlin code it always returns java.lang.Object. I tried many different things but nothing worked except for this:

 (((uMethod.sourcePsi as KtNamedFunction).typeReference as KtTypeReference)
.typeElement as KtUserType)

But it feels wrong. What is the proper way to do it? I would like to get a fully qualified name. For example for code like this:
interface Foo {
   fun test(): String
I would like to get java.lang.String.


Mz Zz

Oct 26, 2021, 10:38:13 AM10/26/21
to lint-dev
Ok never mind. I noticed that the code I copy pasted to my unit test had a suspend keyword. So after the compilation the return type is moved to a generic parameter of a Continuation which is added as an additional parameter to my method. For non suspending methods uMethod.returnType works just fine. 
Reply all
Reply to author
0 new messages