toType

fun <T> String.toType(): Class<T>

Converts a fully qualified class name string to its corresponding Class object. The result is cached for future lookups to improve performance.

Return

the Class object corresponding to the class name

Parameters

T

the type to cast the resulting Class to

Throws

if the class cannot be found

Example usage:

val stringClass = "java.lang.String".toType<String>() // returns String::class.java
val listClass = "java.util.List".toType<List<*>>() // returns List::class.java