Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 541
theapache64 Avatar asked Apr 16 '26 11:04

theapache64


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.

like image 142
Jesse Wilson Avatar answered Apr 18 '26 00:04

Jesse Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!