GWT LinkedHashMap.clone() 问题
最近遇到一个有趣的问题。我在执行此代码时捕获了 ClassCastException:
LinkedHashMap<Tag, Boolean> tags = new LinkedHashMap<Tag, Boolean>();
...
LinkedHashMap<Tag, Boolean> tagsCopy = (LinkedHashMap<Tag, Boolean>)tags.clone();//exception on this line
在开发模式下它工作得很好,但在生产中却失败了。通过手动创建浅拷贝解决了这个问题。但我仍然对导致这种行为的原因感兴趣。 有什么想法吗?
UPD忘了提,我使用java.util.LinkedHashMap。
Faced an interesting issue recently. I've catched ClassCastException while executing this code:
LinkedHashMap<Tag, Boolean> tags = new LinkedHashMap<Tag, Boolean>();
...
LinkedHashMap<Tag, Boolean> tagsCopy = (LinkedHashMap<Tag, Boolean>)tags.clone();//exception on this line
In development mode it works just fine, but it fails in production somewhy. Solved it by creating a shallow copy manually. But I'm still interested in what caused such a behaviour.
Any ideas?
UPD forgot to mention, I use java.util.LinkedHashMap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GWT 不支持
克隆
,请参阅clone
is not supported by GWT, see issue 1843 on the GWT issue tracker. It does work in development mode as in that mode plain Java code is executed, while in production the generated JavaScript is executed, for which no working implementation of clone is generated. In issue 1843 are some suggestions for creating a GWT compatible version, but afaik those suggestions are not in the GWT implemented.在 GWT 2.4 中,
LinkedHashMap.clone()
返回一个HashMap
。尝试使用Map<...> = (Map<...>) anyOtherMap.clone();
在一般情况下可以避免此类问题。With GWT 2.4,
LinkedHashMap.clone()
returns aHashMap
. Try usingMap<...> = (Map<...>) anyOtherMap.clone();
in the general case to avoid such issues.