I'm trying to setup a mechanism for retrofit in order to fetch data from both local jsons and remote API.
@Provides
@LoginScope
open fun providesLoginService(retrofitAPI: RetrofitAPI) : LoginService {
return retrofitAPI.createService<LoginService>(LoginService::class.java)
}
For remote API, everything works fine. For local JSONs files I am using a proxy:
private fun <Service> createServiceDemo(mClass: Class<Service>): Service {
return Proxy.newProxyInstance(
mClass.classLoader,
arrayOf(mClass),
DemoInvocationHandler(mContext)
) as Service
}
fun <Service> createService(mClass: Class<Service>) : Service {
val service = if (abSession.isDemoMode() == true) {
createServiceDemo(mClass)
} else {
retrofit.create(mClass)
}
return service
}
This is the Invocation Handler class where I have the problem:
class DemoInvocationHandler(val context: Context) : InvocationHandler {
@Throws(Throwable::class)
override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any {
val returnType = method?.genericReturnType
val parameterizedType = returnType as ParameterizedType
val _classResponse = TypeToken.get(parameterizedType.actualTypeArguments[0]).type
val demo = method?.getAnnotation(Demo::class.java)
return CallFromJSON<Any?>(context, demo?.demoFile ?: "", _classResponse)
}
}
method?.genericReturnType always returns an Object instead of returning the Generic Type.
This is the thrown message: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
Once I remove the suspend keyword from loginOld method, the cast is working.
I guess I have a problem with reflection combined with coroutines.
Any help ? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire