https://kotlinlang.org logo
Title
x

xenoterracide

04/08/2019, 6:28 PM
is there a reflective way to copy A to B in kotlin (where A and B are different instances of different classes but share the same attributes)
d

dalexander

04/08/2019, 6:29 PM
You could use java.lang.reflect.* to do that, but I recommend just assigning the attributes manually unless there are a lot of them for some reason.
x

xenoterracide

04/08/2019, 6:32 PM
yeah, wasn’t sure if maybe kotlin had something similar to a ReflecionUtils.copyProperties. I probably should just do it by hand.
b

Burkhard

04/08/2019, 6:44 PM
Also doing something like this with reflection can easily break if one property of a class is renamed, leading to very obscure bugs
1
x

xenoterracide

04/09/2019, 3:27 PM
yes, I know, I’ve had something like this break before when spring changed its implementation. ah well looks like idea was able to generate a constructor call for me somehow… I only wish it could generate all the setter calls for me.