Skip to content

Instantly share code, notes, and snippets.

@T8RIN
Created May 17, 2022 18:42
Show Gist options
  • Select an option

  • Save T8RIN/d4bab7d9aba08fa83f05bfdd8446d85b to your computer and use it in GitHub Desktop.

Select an option

Save T8RIN/d4bab7d9aba08fa83f05bfdd8446d85b to your computer and use it in GitHub Desktop.
package androidx.lifecycle
internal object ViewModelClearer {
fun ViewModel.clearViewModel() = clear()
fun <T : Any?> T.getPrivateProperty(variableName: String): Any? {
if (this == null) return null
return javaClass.getDeclaredField(variableName).let { field ->
field.isAccessible = true
return@let field.get(this)
}
}
fun <T : Any?> T.setAndReturnPrivateProperty(variableName: String, data: Any): Any? {
if (this == null) return null
return javaClass.getDeclaredField(variableName).let { field ->
field.isAccessible = true
field.set(this, data)
return@let field.get(this)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment