0

There is a function. It is shown below.

fun <E, T : Collection<E>> constructCollectionType(collectionClass: Class<T>, elementClass: Class<E>): JavaType {
    return mapper.typeFactory.constructCollectionType(collectionClass, elementClass)
}

I call the function above in other function. The code is shown below.

val type = constructCollectionType<Sku, MutableList<Sku>>(MutableList<Sku>::class, Sku::class.java)

Then, kotlin compile give me an error.

Only classes are allowed on the left hand side of a class literal.

The only way I found is Reified type parameters. The code is shown below.

inline fun <reified E, reified T: Collection<E>> constructCollectionType():JavaType {
        return mapper.typeFactory.constructCollectionType(T::class.java, E::class.java)
} 

Then, The call-site code is shown below.

jsonMapper.constructCollectionType<Sku, MutableList<Sku>>()

Is there any other solution ?

wujunbin
  • 341
  • 1
  • 3
  • 16
  • Are you asking this: https://stackoverflow.com/questions/37016058 ? – Jorn Vernee Aug 23 '17 at 09:27
  • @JornVernee thank you, the link you gave me supplies additional way, as seen below. `val type = jsonMapper.constructCollectionType>((MutableList::class.java as Class>), Sku::class.java)` – wujunbin Aug 23 '17 at 09:40

0 Answers0