Added support for non-ByteEnum enums, which are stored using their string name.

This commit is contained in:
Michael Bayne
2010-12-01 19:36:01 +00:00
parent fd035fd463
commit eaadb3228a
2 changed files with 45 additions and 1 deletions
@@ -423,6 +423,10 @@ public abstract class FieldMarshaller<T>
@SuppressWarnings("unchecked") Class<Dummy> dtype = (Class<Dummy>)ftype;
return new ByteEnumMarshaller<Dummy>(dtype);
} else if (Enum.class.isAssignableFrom(ftype)) {
@SuppressWarnings("unchecked") Class<Dummy> dtype = (Class<Dummy>)ftype;
return new EnumMarshaller<Dummy>(dtype);
} else {
return null;
}
@@ -728,6 +732,36 @@ public abstract class FieldMarshaller<T>
protected Class<E> _eclass;
}
protected static class EnumMarshaller<E extends Enum<E>> extends FieldMarshaller<E> {
public EnumMarshaller (Class<E> clazz) {
_eclass = clazz;
}
@Override public String getColumnType (ColumnTyper typer, int length) {
return typer.getStringType(length);
}
@Override public E getFromObject (Object po)
throws IllegalArgumentException, IllegalAccessException {
@SuppressWarnings("unchecked") E value = (E) _field.get(po);
return value;
}
@Override public E getFromSet (ResultSet rs) throws SQLException {
String svalue = rs.getString(getColumnName());
return (svalue == null) ? null : Enum.valueOf(_eclass, svalue);
}
@Override public void writeToObject (Object po, E value)
throws IllegalArgumentException, IllegalAccessException {
_field.set(po, value);
}
@Override public void writeToStatement (PreparedStatement ps, int column, E value)
throws SQLException {
String svalue = (value == null) ? null : value.name();
ps.setString(column, svalue);
}
protected Class<E> _eclass;
}
// used to fool the type system when creating ByteEnumMarshallers; look away
protected static enum Dummy implements ByteEnum {
DUMMY;