XStream - 无法构造java.util.Collection:java.util.Collection

时间:2014-04-10 18:30:14

标签: java xstream

我需要使用java提取XML的摘要信息。我正在尝试XStream,但我遇到了问题。这是第一次使用xStream,我不知道最近会发生什么。我赞成任何帮助。


 run:
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.Collection : java.util.Collection : Cannot construct java.util.Collection : java.util.Collection
---- Debugging information ----
message             : Cannot construct java.util.Collection : java.util.Collection
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct java.util.Collection : java.util.Collection
class               : java.util.Collection
required-type       : java.util.Collection
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /annotation/object
class[1]            : doutorado.imagem.annotation
version             : 1.4.7
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:474)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:406)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:257)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1185)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
    at doutorado.ImportXML.lerXml(ImportXML.java:50)
    at doutorado.ImportXML.main(ImportXML.java:28)
Caused by: com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Cannot construct java.util.Collection : java.util.Collection
    at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:80)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:553)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:256)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 16 more
Caused by: java.lang.InstantiationException: java.util.Collection
    at sun.misc.Unsafe.allocateInstance(Native Method)
    at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:76)
    ... 19 more
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo)

这是XML的一个示例:

    <annotation>
<filename>sun_ajrhlpqweporzeoa.jpg</filename>
<folder>
users/antonio/static_sun_database/c/coast
</folder>
<source>
<sourceImage>
Aude Oliva and Antonio Torralba, IJCV 2001, vol 42(3).
</sourceImage>
<sourceAnnotation>
LabelMe Webtool
</sourceAnnotation>
</source>
<object>
<name>
sky
</name>
<deleted>
0
</deleted>
<verified>
0
</verified>
<date>
06-Oct-2006 10:01:51
</date>
<id>
0
</id>
<polygon>
<username>
anonymous
</username>
<pt>
<x>
2
</x>
<y>
40
</y>
</pt>
<pt>
<x>
2
</x>
<y>
2
</y>
</pt>
<pt>
<x>
71
</x>
<y>
69
</y>
</pt>
<pt>
<x>
65
</x>
<y>
58
</y>
</pt>
<pt>
<x>
56
</x>
<y>
55
</y>
</pt>
<pt>
<x>
43
</x>
<y>
50
</y>
</pt>
<pt>
<x>
27
</x>
<y>
46
</y>
</pt>
<pt>
<x>
17
</x>
<y>
43
</y>
</pt>
<pt>
<x>
8
</x>
<y>
44
</y>
</pt>
</polygon>
</object>

</annotation>

这是我的代码:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import doutorado.imagem.annotation;
import doutorado.imagem.imagesize;
import doutorado.imagem.object;
import doutorado.imagem.polygon;
import doutorado.imagem.pt;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.util.ArrayList;
import java.util.Collection;


public class ImportXML {

    public static void main(String[] args) {
        lerXml();
    }

    private static void lerXml() {
        FileReader reader = null;
        try {

            reader = new FileReader(
                    "Imagens\\coast1.xml"
            );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        XStream xStream = new XStream(new DomDriver());

        xStream.alias("annotation", doutorado.imagem.annotation.class);
        xStream.processAnnotations(annotation.class);

        annotation imagem = (annotation) xStream.fromXML(reader);
        System.out.println(imagem.toString());
    }
}


@XStreamAlias("annotation")
public class annotation {
    private String filename;
    private String folder;
    @XStreamAlias("source")
    private source fonte;
    @XStreamAlias("object")
    private Collection<object> objetos = new ArrayList<>();
    @XStreamAlias("imagesize")
    private imagesize tamanho;

    //...{Getters and Setters}
}

@XStreamAlias("object")
public class object {
    private String name;
    private int deleted;
    private int verified;
    private String date;
    private int id;
    @XStreamAlias("polygon")
    private Collection<polygon> poligonos = new ArrayList<>();

    //...{Getters and Setters}
}

@XStreamAlias("imagesize")
public class imagesize {
    private int nrows;
    private int ncols;

    //...{Getters and Setters}
}

@XStreamAlias("polygon")
public class polygon {

    private String username;
    @XStreamAlias("pt")
    private Collection<pt> pontos = new ArrayList<>();

    //...{Getters and Setters}
}

@XStreamAlias("pt")
public class pt {
    private int x;
    private int y;

    //...{Getters and Setters}
}

@XStreamAlias("source")
public class source {
    private String sourceImage;
    private String sourceAnnotation;

    //...{Getters and Setters}
}

1 个答案:

答案 0 :(得分:0)

问题是XStream会查看您的Java类,并发现对象字段是java.util.Collection,它无法实例化,因为它是一个接口。最简单的解决方案是将类型更改为java.util.ArrayList。如果需要,setter和getter仍然可以是Collection类型,因为XStream默认直接使用这些字段。