@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class IntentExtra(val key: String)
private fun handleIntentExtraAnnotation() {
val extras = intent.extras ?: return
this.javaClass.declaredFields.forEach { field ->
val intentExtra = field.getAnnotation(IntentExtra::class.java) ?: return@forEach
if (!extras.containsKey(intentExtra.key)) return@forEach
field.isAccessible = true
when (field.type) {
Int::class.java -> field.setInt(this, extras.getInt(intentExtra.key, 0))
Boolean::class.java -> field.setBoolean(
this, extras.getBoolean(intentExtra.key, false)
)
String::class.java -> field.set(this, extras.getString(intentExtra.key))
Long::class.java -> field.setLong(
this, extras.getLong(intentExtra.key, 0L)
)
}
field.isAccessible = false
}
}