I have the following inline function with a reified parameter to generalize an http resource get:
inline fun <reified N> getResources(): ResponseEntity<List<N>> {
val httpEntity = HttpEntity(null, httpHeaders)
val resourceStr = getResourceString<N>()
return rest.exchange(testContext.baseUrl + "/api/v1/$resourceStr", HttpMethod.GET,
httpEntity, typeRef<List<N>>())
}
And I am building the ParameterizedTypeReference with a typeRef support function as answered here:
inline fun <reified T : Any> typeRef(): ParameterizedTypeReference<T>{
return object : ParameterizedTypeReference<T>() {}
}
When calling getResources<Employee>(), the ParameterizedTypeReference which is built has ParameterizedTypeReference.type.actualTypeArguments containing java.util.List<? extends N> instead of java.util.List<Employee>.
Notice that I am passing typeRef<List<N>>, where N is reified, from getResources() to the reified type T expected by typeRef() but it doesn't seem to be able to build its type properly.
Why isn't this working? Any work around?
UPDATE:
I've refactored the code to directly build the ParameterizedTypeReferencein the first inline function but I'm still getting it as java.util.List<? extends N>.
private inline fun <reified N> getResources(): ResponseEntity<List<N>> {
val httpEntity = HttpEntity(null, httpHeaders)
val resourceStr = getResourceString<N>()
return rest.exchange(testContext.baseUrl + "/api/v1/$resourceStr", HttpMethod.GET,
httpEntity, object : ParameterizedTypeReference<List<N>>() {})
}