I'm using XStream to serialize some objects to XML, and am facing a problem with Enums. The exception I get when I try to serialize the object: "ObjectAccessException: invalid final field java.lang.Enum.name".
Apparently, this is a problem with the reflection API implementation in android: It doesn't treat final fields correctly. This problem actually existed in past implementations of the official Sun (Oracle) JDK.
Can you confirm/refute this is the problem with Android? Can you suggest any other serialization API that could be used in this situation?
The only way i could find to get around this is to create a AbstractSingleValueConverter for enums and then register it with xstream.
public class SingleValueEnumConverter extends AbstractSingleValueConverter
{
private final Class enumType;
public SingleValueEnumConverter(Class type)
{
this.enumType = type;
}
public boolean canConvert(Class c)
{
return c.equals(enumType);
}
public Object fromString(String value)
{
return Enum.valueOf(enumType, value);
}
}
Use
XStream xml = new XStream();
xml.registerConverter(new SingleValueEnumConverter([ENUM].class));