如何在 OkHttp Interceptor 中获取方法的返回类型? [英] How to get method's return type in OkHttp Interceptor?
问题描述
通过okhttp3.Request#tag(Invocation::class.java)
,我们可以得到methodName
、annotations
等.有没有使用 Invocation
类获取方法返回类型的方法.
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.
我试过了
request.tag(Invocation::class.java)!!.method().returnType
request.tag(Invocation::class.java)!!.method().genericReturnType
但它们都只是返回class java.lang.Object
,即使它是一个List
.
but both of them simply return class java.lang.Object
, even it's a List
.
@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
推荐答案
它是一个挂起函数,所以 java.lang reflect.Method 的返回类型不是你声明的返回类型.查看反编译的字节码,您将看到 Java 方法以及声明的返回类型在签名中的位置.
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.
这篇关于如何在 OkHttp Interceptor 中获取方法的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!