Hi Jörg,

Ok, I wrote a small self-contained class to illustrate the issue:

import java.io.EOFException;
import java.io.ObjectInputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.XStream;

public class TestXStreamReferencing
{
  //~ Statics
------------------------------------------------------------------

  static private final Logger LOG =
LoggerFactory.getLogger(TestXStreamReferencing.class);

  //~ Methods
------------------------------------------------------------------

  public static void main(String[] args)
  {
    try
    {
      String xml = StringUtils.join(new String[] {
        "<list>",
        "  <typeA/>",
        "  <typeA/>",
        "  <typeB>",
        "    <items>",
        "      <typeA reference=\"../../../typeA[2]\"/>",
        "    </items>",
        "  </typeB>",
        "</list>"
      }, "\n");

      // Read objects.
      XStream xsUsersAndGroups = new XStream();
      xsUsersAndGroups.alias("typeA", TypeA.class);
      xsUsersAndGroups.alias("typeB", TypeB.class);

      ObjectInputStream isUsersAndGroups =
xsUsersAndGroups.createObjectInputStream(IOUtils.toInputStream(xml));

      try
      {
        while (true)
        {
          Object nextObject = isUsersAndGroups.readObject();

          LOG.info("read: {}", nextObject);
        }
      }
      catch (EOFException e)
      {
      }
    }
    catch (Throwable t)
    {
      LOG.error(t.getMessage(), t);
    }
  }

  //~ Inner Classes
------------------------------------------------------------

  static class TypeA
  {
  }

  static class TypeB
  {
    //~ Members
----------------------------------------------------------------

    private List<TypeA> items;

    //~ Constructors
-----------------------------------------------------------

    public TypeB()
    {
      this.items = new LinkedList<TypeA>();
    }

    //~ Methods
----------------------------------------------------------------

    public List<TypeA> getItems()
    {
      return items;
    }

    public void setItems(List<TypeA> items)
    {
      this.items = items;
    }
  }
}

And the error I get:

ERROR [main] (TestXStreamReferencing.java:64) - Invalid reference
---- Debugging information ----
reference           : ../../../typeA[2]
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      :
com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /typeB/items/typeA
line number         : 6
class[1]            : com.wft.lcods.tool.TestXStreamReferencing$TypeB
converter-type[1]   :
com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null
-------------------------------
com.thoughtworks.xstream.converters.ConversionException: Invalid reference
---- Debugging information ----
reference           : ../../../typeA[2]
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      :
com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /typeB/items/typeA
line number         : 6
class[1]            : com.wft.lcods.tool.TestXStreamReferencing$TypeB
converter-type[1]   :
com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null
-------------------------------
    at
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:57)
    at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at
com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:71)
    at
com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:79)
    at
com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:72)
    at
com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:66)
    at
com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)
    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.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
    at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
    at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    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:1052)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1024)
    at com.thoughtworks.xstream.XStream$2.readFromStream(XStream.java:1716)
    at
com.thoughtworks.xstream.core.util.CustomObjectInputStream.readObjectOverride(CustomObjectInputStream.java:104)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:363)
    at
com.wft.lcods.tool.TestXStreamReferencing.main(TestXStreamReferencing.java:53)

Thanks for your help!

Michael



>

Reply via email to