How to get method's return type in OkHttp Interceptor?

Viewed 290

Via okhttp3.Request#tag(Invocation::class.java), we can get methodName, annotations etc. Is there any way to get the method's return type using Invocation class.

I've tried

  • request.tag(Invocation::class.java)!!.method().returnType
  • request.tag(Invocation::class.java)!!.method().genericReturnType

but both of them simply return class java.lang.Object, even it's a List.

Expected Output

@GET("products")
suspend fun getProducts(): List<Product>  // I need to get at least `List` as a String for this method

@GET("product")
suspend fun getProduct(): Product // I need to get at least `Object` as a String for this method
1 Answers

It's a suspend function so the return type of the java.lang reflect.Method is not the return type you declared. Look a the decompiled bytecode and you'll see the Java method and where the declared return type ends up in the signature.

Related