Skip to content

Instantly share code, notes, and snippets.

@sanogueralorenzo
Created December 12, 2018 18:27
Show Gist options
  • Save sanogueralorenzo/fe840d5459538da4b8683b48cb40d8a9 to your computer and use it in GitHub Desktop.
Save sanogueralorenzo/fe840d5459538da4b8683b48cb40d8a9 to your computer and use it in GitHub Desktop.
private val classMap = mutableMapOf<String, Class<*>>()
private inline fun <reified T : Any> Any.castOrNull() = this as? T
internal fun <T> loadClassOrNull(className: String): Class<out T>? {
return classMap.getOrPut(className) {
try {
Class.forName(className)
} catch (e: ClassNotFoundException) {
return null
}
}.castOrNull()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment