David Withers wrote:
> David Withers wrote:
>   
>> Alan Williams wrote:
>>     
>>> David Withers wrote:
>>> [snip]
>>>
>>> Hello,
>>>
>>>       
>>>> After investigating some more I've found that although the XStream docs 
>>>> say "If a new field is added to the class, deserializing an old version 
>>>> will leave the field uninitialized", this is not the case. String fields 
>>>> are initialized to null, even if you set a default value for the field.
>>>>         
>>> What is happening?  Are you saying that new fields are initialized to 
>>> null?  If so, isn't a possible workaround for this to alter the 
>>> configuration bean so that when you do a getX it checks if x is null 
>>> and, if so, sets it to a value?
>>>       
>> If it's a new field that's not in the serialized file then yes, it's 
>> initialized to null, even though I'm setting a value when I declare the 
>> field, e.g. private String csvDelimiter = ",";
>>
>> So yes, the workaround in this case is:
>>
>> public String getCsvDelimiter() {
>>      return outputFormat == null ? "," : csvDelimiter;
>> }
>>
>>
>>     
>>>> If you add an enum field then an exception is thrown (which is the 
>>>> problem I'm hitting).
>>>>         
>>> What is throwing the exception?
>>>       
>> XStream. I'm guessing that it creates to object for the field and then 
>> sets the value from the serialized xml. However you can't create enums 
>> without knowing the value.
>>     
>
> Actually I'm wrong there. It seems that because the class is created 
> without calling a constructor any values that appear to be assigned as 
> part of the field declaration actually aren't assigned.
>
> Oh well, at least I have a workaround now.
>   
Its correct that it doesn't call the constructor, because these could 
have side affects. Its the same with the getters and setters.
The FAQ recommends adding a readResolve method to do any custom 
initialisation required during de-serialisation.
http://xstream.codehaus.org/faq.html (Under "XStream is not calling the 
default constructor during deserialization", about a 1/3rd down).

Stuart
> David.
>   


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
taverna-hackers mailing list
[email protected]
Web site: http://www.taverna.org.uk
Mailing lists: http://www.taverna.org.uk/taverna-mailing-lists/
Developers Guide: http://www.mygrid.org.uk/tools/developer-information

Reply via email to