xstream
xstream copied to clipboard
Add Converters for java.awt.Dimension & java.awt.DisplayMode
Hi, thanks for your contribution. Is it OK for you, when I add XStream's copyright notice? You will be credited as author of the classes.
<!--
Copyright (C) 2021 XStream committers.
All rights reserved.
The software in this package is published under the terms of the BSD
style license a copy of which has been included with this distribution in
the LICENSE.txt file.
-->
Yup, that works for me!
Hi,
I had a closer look now. Without the converters I have this:
public void testAwtDimension() {
final Dimension dimension = new Dimension(10, 20);
xstream.aliasType("awt-dimension", Dimension.class);
final String expected = ""
+ "<awt-dimension>\n"
+ " <width>10</width>\n"
+ " <height>20</height>\n"
+ "</awt-dimension>";
assertBothWays(dimension, expected);
}
public void testAwtDisplayMode() {
final DisplayMode displayMode = new DisplayMode(1280, 1024, 32, 60);
xstream.aliasType("awt-display-mode", DisplayMode.class);
final String expected = ""
+ "<awt-display-mode>\n"
+ " <size>\n"
+ " <width>1280</width>\n"
+ " <height>1024</height>\n"
+ " </size>\n"
+ " <bitDepth>32</bitDepth>\n"
+ " <refreshRate>60</refreshRate>\n"
+ "</awt-display-mode>";
assertBothWays(displayMode, expected);
}
Why do you think the converters are necessary?
Thanks for a taking a look into it!
I ran some tests in the project that I wrote these converters for and it looks like Dimension
works with aliasType
and that converter is unnecessary. However, DisplayMode
doesn't seem to work with aliasType
for me, so it might be necessary unless I'm using it incorrectly?
I'm testing with JDK 17 and both the stream creation code and error message are as follows:
private static XStream createStream() {
final var xStream = new XStream(new Xpp3DomDriver());
xStream.aliasType("awt-dimension", Dimension.class);
xStream.aliasType("awt-display-mode", DisplayMode.class);
xStream.allowTypes(new Class[] {
SettingsModel.class
});
return xStream;
}
Exception in thread "AWT-EventQueue-0" com.thoughtworks.xstream.converters.ConversionException: No converter available
---- Debugging information ----
message : No converter available
type : java.awt.DisplayMode
converter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
message[1] : Unable to make field private java.awt.Dimension java.awt.DisplayMode.size accessible: module java.desktop does not "opens java.awt" to unnamed module @7eb2a789
-------------------------------
at com.thoughtworks.xstream.core.DefaultConverterLookup.lookupConverterForType(DefaultConverterLookup.java:88)
at com.thoughtworks.xstream.XStream$1.lookupConverterForType(XStream.java:472)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:48)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:83)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:270)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:174)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:262)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:90)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:68)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:1243)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:1232)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:1205)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:1192)
at com.valkryst.Java2D_Experiment_000_Starter_Kit.serialization.Serialization.serialize(Serialization.java:53)
at com.valkryst.Java2D_Experiment_000_Starter_Kit.display.model.SettingsModel.save(SettingsModel.java:119)
at com.valkryst.Java2D_Experiment_000_Starter_Kit.display.controller.SettingsController.lambda$new$1(SettingsController.java:22)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Process finished with exit code 0