Xstream inheritance problem with java 8

472 views
Skip to first unread message

Iakov Melnikov

unread,
Jun 7, 2017, 8:43:17 PM6/7/17
to XStream User
I have to upgrade java version from 1.7 to 1.8 in a project which is using Xstream (1.4.4) A class that needs to be serialized extends another class. Under java 1.7 it works fine, all the fields (including inherited ones) are present in resulted XML. However, under java 1.8 inherited fields are missing in XML. I tried and used the latest Xstream jar (1.4.10) with the same negative result. If I bring Xstream source code into the project under java 8 and don't reference Xstream jar everything works as it's supposed to, but we are using Ivy and I have to reference Xstream jar.
What is the reason for this behaviour? How it can be dealt with?
Thank you.

Jörg Schaible

unread,
Jun 8, 2017, 8:52:11 AM6/8/17
to xstrea...@googlegroups.com
Hi Iakov,
Cannot say, there are no known problems apart from the fact that a type from
the Java runtime might change its memory layout and that may cause trouble
if that type is handled by a reflection-based converter. So what type you're
extending?

Regards,
Jörg


Iakov Melnikov

unread,
Jun 8, 2017, 10:12:19 AM6/8/17
to XStream User, joerg.s...@bpm-inspire.com
Hi Jörg,

Thanks for taking a look. Below is the code I used for testing:

public class Address {

private String street;
private String city;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}
}

public class Person extends Address {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

public class XstreamTest {

public static void main(String[] args) {
XStream xstream = new XStream();

Person person = new Person();
person.setStreet("Main Street");
person.setCity("New York");
person.setName("John Smith");

System.out.println(xstream.toXML(person));
}

}

With Java 7 the result is:

<test.Person>
<street>Main Street</street>
<city>Toronto</city>
<name>John Smith</name>
</test.Person>

The same result is with Java 8 and Xstream source in the project

Java 8 and Xstream jar on the classpath the result is:

<test.Person>
<name>John Smith</name>
</test.Person>

Thank you.

Jörg Schaible

unread,
Jun 9, 2017, 7:03:11 PM6/9/17
to xstrea...@googlegroups.com
Hi Iakov,

Iakov Melnikov wrote:

These are my results with your code from above (just as expected):

==================== %< ==================
~ $ lsjar tmp/xstream-test.jar
=== tmp/xstream-test.jar ===
META-INF/MANIFEST.MF
test/Address.class
test/Person.class
test/XstreamTest.class
~ $ jdk17
~ $ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
~ $ java -cp
.m2/repository/com/thoughtworks/xstream/xstream/1.4.10/xstream-1.4.10.jar:.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar:.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar:tmp/xstream-
test.jar test.XstreamTest
<test.Person>
<street>Main Street</street>
<city>New York</city>
<name>John Smith</name>
</test.Person>
~ $ jdk18
~ $ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
~ $ java -cp
.m2/repository/com/thoughtworks/xstream/xstream/1.4.10/xstream-1.4.10.jar:.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar:.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar:tmp/xstream-
test.jar test.XstreamTest
<test.Person>
<street>Main Street</street>
<city>New York</city>
<name>John Smith</name>
</test.Person>
==================== %< ==================

However:

==================== %< ==================
~ $ jdk18
~ $ java -showversion -cp
.m2/repository/com/thoughtworks/xstream/xstream/1.4.4/xstream-1.4.4.jar:.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar:.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar:tmp/xstream-
test.jar test.XstreamTest
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

<test.Person>
<name>John Smith</name>
</test.Person>
~ $ java -showversion -cp
.m2/repository/com/thoughtworks/xstream/xstream/1.4.5/xstream-1.4.5.jar:.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar:.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar:tmp/xstream-
test.jar test.XstreamTest
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

<test.Person>
<name>John Smith</name>
</test.Person>
~ $ java -showversion -cp
.m2/repository/com/thoughtworks/xstream/xstream/1.4.6/xstream-1.4.6.jar:.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar:.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar:tmp/xstream-
test.jar test.XstreamTest
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

<test.Person>
<street>Main Street</street>
<city>New York</city>
<name>John Smith</name>
</test.Person>

==================== %< ==================

As you can see, your test application starts to work for me with Java 8
using XStream 1.4.6 or higher. However, I can currently not explain, what
triggers the misbehavior in older versions.

Cheers,
Jörg

Iakov Melnikov

unread,
Jun 18, 2017, 10:11:20 PM6/18/17
to XStream User, joerg.s...@gmx.de
Hi Jörg,

I have found the problem with my project. Another application with the older version of Xstream (1.4.7) was on the classpath. Changing the classpath order solved it for me.
Thank you.
Reply all
Reply to author
Forward
0 new messages