简体   繁体   English

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

[英]XStream - Cannot construct java.util.Collection : java.util.Collection

I need extract informations of a XML arquive with java. 我需要使用Java提取XML存档的信息。 I am trying XStream, but I'm facing a problem. 我正在尝试XStream,但遇到了问题。 It is the first time a work with xStream, and I dont't know whats going on. 这是xStream的首次使用,我不知道发生了什么。 I apreciate any help. 我非常感谢您的帮助。


 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)

This is a sample of the XML: 这是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>

This is my code: 这是我的代码:

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}
}

The problem is that XStream looks at your Java class and sees that the object field is a java.util.Collection which it cannot instantiate because it is an interface. 问题是XStream查看您的Java类,并看到对象字段是java.util.Collection ,因为它是一个接口,所以无法实例化。 Simplest solution is to change the type to java.util.ArrayList . 最简单的解决方案是将类型更改为java.util.ArrayList The setters and getters can still be of type Collection , if you want that, since XStream uses the fields directly by default. 如果需要的话,setter和getter仍可以是Collection类型,因为XStream默认情况下直接使用字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM