XStream - 解组 - XML 中指定的类型不可见

发布于 2024-12-12 23:59:10 字数 1096 浏览 2 评论 0原文

我不久前通过 XStream 存储了一些 XML 文件,它们包含对 RandomAccessSubList 的引用,该类在包级别之外不可见,并且没有默认构造函数,因此 XStream 抛出此错误:

com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor : Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor

---- Debugging information ----
message             : Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor*

这是 XML:

<customTimes class="java.util.RandomAccessSubList">
  <l class="list">
    <long>1302174300146</long>
    <long>1302174305231</long>
    <long>1302174310312</long>

等等。

我可以不只是为 RandomAccessSubList 编写一个转换器,因为它在 util 包之外不可见。如何告诉 XStream 忽略此属性的类,或者如何为我无法引用的类指定转换器?

提前致谢!

I have some XML files stored by XStream a while ago, and they include references to RandomAccessSubList, a class which is not visible beyond the package level and has no default constructor so XStream throws this error:

com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor : Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor

---- Debugging information ----
message             : Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct java.util.RandomAccessSubList as it does not have a no-args constructor*

and this is the XML:

<customTimes class="java.util.RandomAccessSubList">
  <l class="list">
    <long>1302174300146</long>
    <long>1302174305231</long>
    <long>1302174310312</long>

etc.

I can't just write a converter for RandomAccessSubList because it's not visible outside the util package. How can I tell XStream to ignore the class for this attribute or how can I specify a converter for a class I can't reference?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2024-12-19 23:59:10

我深入了解了它 - 事实证明 xstream 应该处理该 xml (它不需要无参数构造函数),出现问题是因为我将 jdk 7 与旧版本的 xstream (1.3.1) 一起使用。请参阅此处 http://code.google.com/p/pitestrunner/issues/detail?id= 4.。回到 jdk 6 解决了这个问题(由于各种原因我无法升级)。

在我意识到我确实编写了一个适用于 RandomAccessSubList 的转换器(如果有人需要它)之前:

public class RandomAccessSubListConverter extends CollectionConverter {

public RandomAccessSubListConverter(Mapper mapper) {
    super(mapper); 
} 

@Override
public boolean canConvert(Class arg0) {     
    return arg0.getName().equals("java.util.RandomAccessSubList");
}

@Override
public Object unmarshal(HierarchicalStreamReader reader,
        UnmarshallingContext context) {        
    reader.moveDown();
    ArrayList arrayList = new ArrayList();
    populateCollection(reader, context, arrayList);
    reader.moveUp();
    return arrayList;
}

感谢任何为我寻找的人!

I got to the bottom of it - turns out xstream should handle that xml (it doesn't need a no-args constructor), the issue arose because I was using jdk 7 with an older version of xstream (1.3.1). See here http://code.google.com/p/pitestrunner/issues/detail?id=4. Moving back to jdk 6 fixed the issue (for various reasons i can't upgrade).

Before I realised that I did write a converter that worked for RandomAccessSubList if anyone needs it:

public class RandomAccessSubListConverter extends CollectionConverter {

public RandomAccessSubListConverter(Mapper mapper) {
    super(mapper); 
} 

@Override
public boolean canConvert(Class arg0) {     
    return arg0.getName().equals("java.util.RandomAccessSubList");
}

@Override
public Object unmarshal(HierarchicalStreamReader reader,
        UnmarshallingContext context) {        
    reader.moveDown();
    ArrayList arrayList = new ArrayList();
    populateCollection(reader, context, arrayList);
    reader.moveUp();
    return arrayList;
}

Thanks to anyone who was looking into for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文