Created
August 16, 2021 11:41
-
-
Save Haoxiqiang/451986420d135ef613145a57779a52e2 to your computer and use it in GitHub Desktop.
Copy object's all fields by reflect
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
inline fun <reified T : Any> mergeFields(from: T, to: T, acceptNULL: Boolean = false) { | |
from::class.java | |
.declaredFields | |
.forEach { field -> | |
val isLocked = field.isAccessible | |
field.isAccessible = true | |
val value = field.get(from) | |
if (acceptNULL || value != null) { | |
field.set(to, field.get(from)) | |
} | |
field.isAccessible = isLocked | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment