Skip to content

Instantly share code, notes, and snippets.

@Drabu
Forked from albodelu/Navigation.kt
Created June 21, 2023 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drabu/6ff01a79111d4e20c7c97cc3c6e766f3 to your computer and use it in GitHub Desktop.
Save Drabu/6ff01a79111d4e20c7c97cc3c6e766f3 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