我有一些 JAXB 生成的 bean,它们处于层次结构中,例如一个 bean 包含其他 bean 的列表。现在我想扩展一些子元素以及包含扩展子元素的父元素。
我的 ParentEx
实现一些其他接口(interface) IParent
预计会返回 Collection<IChild>
.我的ChildEx
工具 IChild
.我可以返回吗 (Collection<IChild>)super.getChild()
什么时候super.getChild()
返回 List<Child>
?或者有更好的方法吗?
-
Child
和Parent
是 JAXB 生成的 bean -
ChildEx
和ParentEx
是我自己的 bean,用于将 JAXB bean 映射到给定的接口(interface)。两个 bean 都覆盖了ObjectFactory
-
IChild
和IParent
是否需要其他库的接口(interface)
编辑: Eclipse 甚至不允许我从 List<Child>
进行转换至 List<ChildEx>
所以我必须添加一些丑陋的中间通配符转换 (List<ChildEx>)(List<?>)super.getChild()
最佳答案
这应该有效:
return new ArrayList<IChild>( childExList );
或(不漂亮但避免通配符转换):
return Arrays.asList( childExList.toArray(new IChild[]{}) );
关于java - JAXB 使用扩展对象工厂扩展生成的代码 - 显式转换好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9664152/