I work for a company that uses XML for storing a bunch of serialized Java classes. The framework we use is Apache Betwixt (abandonded in 2008) and it's kind of pain to maintain and extend (it seemed like a good choice back in the day).
Crash course in Betwixt along with the detailed problem:
- Only supports public getters and setters, both getter and setter have to be present
- Does not support JAXB-style type adapters
- The XML evolves with the Java objects, resulting in redundant methods in our classes
Our list of xml serializable classes is well over 150, and the codebase can get very messy when changes to definitions are made. Since the XML is bound to Java getters and setters, we can only migrate XML by keeping old properties in the class definitions.
NowTake a look at this change:
public class ThingWithText {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
The resulting XML looks something like:
<thingWithText>
<text>Whatever</text>
</thingWithText>
Now consider a change in the model to add multiple locales to the ThingWithText
:
public class ThingWithText {
private List<TextWithLanguage> texts = new LinkedList<TextWithLanguage>();
// Getter must be here so the framework recognizes the property.
public String getText() {
return null;
}
// Has to be here so it can be serialized
public void setText(String text) {
this.texts.add(new TextWithLanguage(text));
}
public List<TextWithLanguage> getTexts() {
return texts;
}
public void addText(TextWithLanguage text) {
texts.add(text);
}
}
public class TextWithLanguage {
private Locale locale = // omitted;
private String text;
public TextWithLanguage(String text) {
this.text = text;
}
// Getter and setter omitted
}
The XML now looks like:
<thingWithText>
<texts>
<text locale="en">Whatever</text>
</texts>
</thingWithText>
Now the XML isn't bad looking or anything, but it still leaves the ThingWithText
class with several problems:
- A setter for a non-existing field
- A getter that always returns null, may cause exceptions because stuff still compiles
My questions go:
We have made the decision to do something about this, and it seems like a problem other people would have.
How does one handle migration between an old framework like Betwixt to a newer (JAXB, Xstream, whatever)?
If we were to keep our framework: How can we manage the considerable code complexity added by null methods and fake setters?