我如何深入复制GWT中的任意对象? [英] How can I deep copy an arbitrary object in GWT?
问题描述
我有一个标记为Serializable的任意对象。它有各种可变的子对象,代表货币量,集合等。我希望能够克隆这个对象,所以如果用户修改它,然后恢复他们的行动,我可以用一个新的克隆替换克隆。
这意味着我需要一个深层副本,因为我不希望用户对原始对象上的子对象进行更改(例如添加/插入来自集合的项目)。
这些对象没有实现Cloneable,因为它们是自动生成的,我也不能添加这样的方法。
无论如何,在GWT中完成相同的手写深层克隆?如果这是Java,我可以考虑将该对象序列化为一个字节数组流并反序列化一个新副本。
我无法在GWT中做到这一点。可能我可以利用RPC,因为稍后将对象发送给servlet。我只是不知道这是否容易做到。
任何人都知道一个简单的方法,我可以做一个深层次的复制?
如果您有机会向您的域类添加接口或注释,则可以使用 gwt -ent 进行反射,您可以通过反射轻松地编写自己的深层复制语义。我已经使用这种技术来提高dev模式下的gwt序列化性能。
作为另一种解决方案,您可以使用gwt默认序列化机制进行深层复制,但不幸的是我没有任何线索可以实现此目标。 b $ b
I have an arbitrary object tagged as Serializable. It has various mutable child objects representing monetary amounts, collections etc. I want to be able to clone this object so if the user modifies it and then reverts their action, I can just replace the clone with a fresh clone.
That implies I need a deep copy because I don't want the users changes to child objects (e.g. adding / inserting items from the collection) appearing on the original.
The objects don't implement Cloneable and as they're autogenerated I can't add such a method either.
Short of painfully handwriting a deep clone is there anyway to accomplish the same in GWT? If this were Java I could consider serializing the object to a byte array stream and deserializing a fresh copy.
I can't do that in GWT. Potentially I could harness RPC because the object is sent to a servlet later. I just don't know if that is easy to do.
Anyone know a simple way I can do a deep copy?
If you have a chance to add an interface or annotation to your domain classes, you can use gwt-ent for reflection, and you can easily write your own deep copy semantic by means of reflection. I have used this technique to improve gwt serialization performance in dev mode.
As another solution, you can use gwt default serialization mechanism for deep copy, but unfotunately I dont have any clue how you could accomplish this goal..
这篇关于我如何深入复制GWT中的任意对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!