Created
May 17, 2022 18:42
-
-
Save T8RIN/d4bab7d9aba08fa83f05bfdd8446d85b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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