pablosaraiva
pablosaraiva

Reputation: 2343

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?

Upvotes: 0

Views: 598

Answers (2)

Chris Knight
Chris Knight

Reputation: 25074

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));  

Upvotes: 2

ajayr
ajayr

Reputation: 31

I believe XStream converters do not store context to the xstream object. This helps with coupling. Of course one option could be to declare a constructor argument and pass the xstream object to it. But i think a better solution would be to get the information about whatever you need from the Xstream object and pass that to the custom converter, so as to maintain loose coupling between XStream and it's converters

Upvotes: 1

Related Questions