我之前有一些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忽略该属性的类,或者如何为我无法引用的类指定转换器?
提前致谢!
答案 0 :(得分:8)
我到底了 - 结果xstream应该处理那个xml(它不需要一个no-args构造函数),问题出现了,因为我使用的是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;
}
感谢所有为我着想的人!