Paul Clapham wrote:...if you don't follow standard JavaBean naming conventions, then anything which expects your bean to be following those conventions won't be able to use those methods.
Good point. With my trick, the properties I am "hiding" from XMLEncoder will be just as hidden from anything else that relies on the JavaBean naming conventions. This may not be inherently evil, though, as
the JavaBeans Specification appears to say it's an option you can choose deliberately:
Section 2.2 wrote:The methods a Java Bean exports are just normal Java methods which can be called from other
components or from a scripting environment. By default all of a bean’s public methods will be
exported, but a bean can choose to export only a subset of its public methods (see Section 8.5).
Frustratingly, Section 8.5 doesn't quite speak to this as directly as that excerpt made me think, but it does seem to say that a matching BeanInfo class can actually help overcome the "hiding" effect. I can't claim to be sure about that, though.
As it happens, I wasn't intending to write a JavaBean class in the first place. I just use getters/setters for everything that follow the get/set/is convention because that appears to be How Things Are Done (and because the NetBeans "Insert Code" wizard follows it automatically). That, in turn, made it possible for me to use XMLEncoder, which seemed like a much easier way to serialize my object than any other option I had (and that met my criteria of being human-readable, able to handle my data without me writing code for each new property I add, and not accompanied by warnings that it won't work across versions of
Java).
I suppose I could have asked this: What options do I have for serializing objects that meet my criteria of being human-readable, able to handle my data without me writing code for each new property I add, and not accompanied by warnings that it won't work across versions of Java, as well as easily allowing me to select some properties as persistent and some as transient? But, I don't like it when other people ask general "tell me how to do this so I won't have to look it up" questions, and I had spent a fair amount of time working on this already, so here I am. (That said, if you know an obvious alternative I should consider, please do let me know
)