Giter Site home page Giter Site logo

Problem of TreeSet/TreeMap about xstream HOT 4 CLOSED

urn1ce avatar urn1ce commented on August 15, 2024
Problem of TreeSet/TreeMap

from xstream.

Comments (4)

joehni avatar joehni commented on August 15, 2024

This is no bug, this is by design. A TreeSet/TreeMap may have an own Comparator and the Java API has no official way, to get them, therefore reflection is the only way. So, do as the exception says, run your application with run time option "--add-opens java.base/java.util=ALL-UNNAMED"

from xstream.

urn1ce avatar urn1ce commented on August 15, 2024

This is no bug, this is by design. A TreeSet/TreeMap may have an own Comparator and the Java API has no official way, to get them, therefore reflection is the only way. So, do as the exception says, run your application with run time option "--add-opens java.base/java.util=ALL-UNNAMED"

I known your meaning, but if i change like this (still not use "--add-opens java.base/java.util=ALL-UNNAMED")
public static void main(String[] args) {
var ts = new TreeSet();
//var al = new ArrayList();
//al.add("ABC");
//ts.add(al);
ts.add("");//String is Comparable
var xs = new XStream();
xs.allowTypesByWildcard(new String[]{"**"});
var str = xs.toXML(ts);
System.out.println(str);
var obj = xs.fromXML(str);//TreeMap has the same problem
System.out.println(obj);
}
The problem like this:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.thoughtworks.xstream.converters.collections.TreeSetConverter.unmarshal(TreeSetConverter.java:62)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:74)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:68)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:52)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:136)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1464)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1441)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1321)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1312)
at com.example.webenv174jar.XStreamTest.main(XStreamTest.java:26)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private transient java.util.NavigableMap java.util.TreeSet.m accessible: module java.base does not "opens java.util" to unnamed module @11a9e7c8
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at com.thoughtworks.xstream.core.util.Fields.locate(Fields.java:40)
at com.thoughtworks.xstream.converters.collections.TreeSetConverter$Reflections.(TreeSetConverter.java:135)
This problem is also by design?

from xstream.

joehni avatar joehni commented on August 15, 2024

Yes, because how should XStream know, if a comparator has been set? There's just one way: Look into the private field.

from xstream.

urn1ce avatar urn1ce commented on August 15, 2024

Yes, because how should XStream know, if a comparator has been set? There's just one way: Look into the private field.

So even if not use comparator, also need "--add-opens java.base/java.util=ALL-UNNAMED", it maybe a bit redundant, if catch and handle exception may have a better experience., but i understand, thank you

from xstream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.