Skip to content
Advertisement

deep copy using reflection java

I can’t get a container from the class field using reflection. I tried the method below, but got an exception:

JavaScript
JavaScript

I don’t want to use methods of copying via serialization, I’m interested in reflection.

Advertisement

Answer

You are doing this for training I hope? If not then use some opensource library, it’s a lot harder than you think – check this.

Your problem is that you are adding to the to list, and the to list is an implementation that does not support adding (btw then you are ignoring the result). I suggest creating a new list and reassiging it, instead of adding to the existing one.

JavaScript

Similar thing with Set – your current code for Set doesn’t make any sense, it operates on Class objects.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement