tags:

views:

31

answers:

2

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?

+1  A: 

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

ajayr
I do need the xstream object. I want to change the xstream default alias configuration inside the converter on the fly. The xstream knows it's registered converters, I thought there would be a way for a converter to know the XStream object that called him somehow.
pablosaraiva
i had a similar problem, the best way i could find was to use the ClassAliasingMapper that XStream takes in as an argument to generate dynamic aliases. But, of course you need to have unique one-to-one bidirectional mapping between maps and classes. Maybe that might help you
ajayr
+1  A: 

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