mardi 28 mai 2019

Combining reflection, enums and generics in Kotlin, type inference fails, but I don't know it as well

I'm using reflection to load an Enum class I'm using to integrate with an existing Java application. In Java this is written as (in a class ValueUtil):

static <E extends Enum<E>> Class<E> getEnumType(String typeAsString) { ..reflection stuff.. }

In Kotlin I try to use the result of this method.

private fun createEnumParameter(type: String, value: String, id: String?): EnumParameter<*> {
    val clazz = getEnumType(type)
    return createParam(clazz, value, id)
}

private fun <E: Enum<E>> getEnumType(type: String): Class<E> {
    return ValueParseUtil.getEnumType<E>(type)
}

private fun createParam(...): ... { ... }

However, the compiler states: Type inference error

So I try as best as I can to specify what I know (Enum<*>): Enum<*> error

This fails due to enum recursion in the generics (which I/Kotlin/Java need(s) by the way).

Is reflection + enums + generics a recipe for compiler errors?

Any suggestions how to fix this?

Best regards,

Michiel





Aucun commentaire:

Enregistrer un commentaire