Search code examples
javaxstream

How to access the XStream object inside the Converter


I have an instance of XStream where I've registered some converters and made some configuration the way I want things to work.

XStream xstream = new XStream();
xstream.registerConverter(new SomeConverter());
(...)

And I have a SomeConverter class that implements Converter.

For some reason, I'd like to access the xstream object inside the converter code.

Is there a way to get it from some Converter method/attribute or I'd have to get it from somewhere else?


Solution

  • Converter is just an interface, so there isn't anything stopping you from changing the constructor of SomeConverter to take in the XStream object. Then you would have access to it with your implemented methods. E.g.

    XStream xstream = new XStream();
    xstream.registerConverter(new SomeConverter(xstream));