I am trying to un/marshal objects of a class with a member of an enum type, as
described below.
public enum Color {
RED("R"), GREEN("G"), BLUE("B");
private String value;
private Color(String v) {
value = v;
}
public static fromValue(String v) {
for (Color c : Color.values()) {
if (c.value.equals(v)) {
return c;
}
throw new IllegalArgumentException(v);
}
public String value() {
return value;
}
public String toString() {
return value;
}
}
public Test {
Color color = RED;
public Color getColor() {
return color;
}
public void setColor(Color c) {
color = c;
}
}
With XML mapping:
<class name="package.Test">
<map-to xml="Test"/>
<field name="color" type="package.Color">
<bind-xml name="color" node="element"/>
</field>
</class>
I am able to marshal objects of Test. However, I get exceptions when I
unmarshal the output file.
java.lang.IllegalArgumentException: No enum const class package.Color.R
The debugger shows that the method Color.valueOf is called instead of
Color.fromValue. How do I make Unmarshaller to call Color.fromValue? What did I
do wrong here? Please help.
Thank you.