java - wsimport (xjc) - why list getter has always null check? -


I wonder why the recipient (XSD) always checks for a list of random checks:

  Public class response {@XmlElement (Type = Integer.class) protected list & lt; Integer & gt; Integers; Public listing & lt; Integer & gt; GetIntegers () {if (integers == faucet) {integers = new ArrayList & lt; Integer & gt; (); } this. }}  

Question:

Why? What is the reason? Is there any goodness?

Because I'm asking in some cases it is not a good thing and it seems that there is no way to change this behavior.

After some of the excavations, the reason becomes obvious. I created some code with xjc and for a list property, it has made this comment:

  / ** * Get the value of the property of bars Is * * & lt; P & gt; * This accessory method returns a reference to the live list, not * snapshots. Therefore, whatever revision you will be given in the returned list will be present inside the JAXB object. * This is why someone & lt; CODE & gt; Set & lt; / CODE & gt; Method for Bar Property * * & Lt; P & gt; * For example, to add a new item, do the following: * & lt; Pre & gt; * GetBars (). Add (newItem); * & Lt; / Pre & gt; * * * & Lt; P & gt; * The objects of the following types (objects) are allowed in the list * {@link Bar} * * * /  

This makes their intentions clear that they wanted to be stuck with the object is impossible, therefore it always gives a live list instead of copying it means that the recipients (for various threads or different contexts, for example) many calls always reference to the same in-memory object Will be given. To do this, however, it meant that they could not have a setter, or they would break this contract - the reference could set the value, and context B would have an old context of the old value, and some of it There is no way to know that it has been changed.

Because, due to that design decision, they could not have a setter, so there are some ways to make a change in the list, if you need to add or remove items, otherwise, In the beginning, the blank list will always be null forever (except for reflections, except for shenanigans), this is the only way to allow this, the null < / Code> was to check, and lazy - at that time Was to start. This meant that the plan to change the entire list should be

  foo.getBars (). clear (); . Foo.getBars () addAll (someList);  Why   They chose this design ... there is no way to know the answer for anyone outside that team. However, this is usually a very good pattern to follow most codes (it reduces coupling, and eliminates common error conditions that the compiler can not warn you), so an argument against it It is difficult to do a lot about. If this is really bothering you (and you have not actually shown it how, how to do it, except for the fact that some of your objects are empty list after copy operation instead of zero), then I give you the only advice Either I do not use code generator, or write an extension for  xjc , it can be an existing extension for what you want to do, but I can not Received. 


Comments