Another enum generics issue


I’ve just come across another enum issue that I can’t seem to solve. That is, the reflective creation of enum instances of unknown (at compile time) type.

Basically, I have a class that I reflectively determine is a subtype of Enum. I have a string serialization/name of the specific value I need. The below Java shows how it works in Java

public static Enum<?> enumValueOf(Class<? extends Enum<?>> clazz, String label) {
    return Enum.valueOf((Class) clazz, label);

I know, even in Java I have to go to raw types to make this work. I’d like to know if there is a possibility to do this in Kotlin such that the following using code works:

val clazz: Class<Enum<*>> = paramType as Class<Enum<*>>
val label:String = "AnEnumLabel"
val enumValue: Enum<*> = enumValueOf(clazz, label)


I generally use this to get an instance of an Enum with a given name from a Class object, note the returned enum is nullable

enumClass.enumConstants.find { it.toString().equals(enumLabel, true) }