I solved the above problem in Kotlin that works fine for me for my Android Apps Development:
object FieldMapper {
fun <T:Any> copy(to: T, from: T) {
try {
val fromClass = from.javaClass
val fromFields = getAllFields(fromClass)
fromFields?.let {
for (field in fromFields) {
try {
field.isAccessible = true
field.set(to, field.get(from))
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun getAllFields(paramClass: Class<*>): List<Field> {
var theClass:Class<*>? = paramClass
val fields = ArrayList<Field>()
try {
while (theClass != null) {
Collections.addAll(fields, *theClass?.declaredFields)
theClass = theClass?.superclass
}
}catch (e:Exception){
e.printStackTrace()
}
return fields
}
}