Hi Carl,
I have the same problem as you. I have worked out a solution, but it is
not very elegant. It involves writing a custom ...FieldHandler class for
each typesafe enum field and for each class where such a field occurs.
For each enumField of type EnumClass in your class MyClass, you write a
field handler class like this:
public class MyEnumFieldHandler extends EnumFieldHandler
{
public Object getValue( Object object )
{
return ( (MyClass) object ).getEnumField();
}
public void setValue( Object object, Object value )
{
EnumClass enumValue = EnumClass.valueOf( (String) value );
if ( enumValue != null )
( (MyClass) object ).setEnumField( enumValue );
else
throw new RuntimeException( "Invalid value '" + value + "'" );
}
}
The base class EnumFieldHandler is derived from Castor's
AbstractFieldHandler and can be reused for all enum field handlers:
import org.exolab.castor.mapping.AbstractFieldHandler;
public abstract class EnumFieldHandler extends AbstractFieldHandler
{
public abstract void setValue( Object object, Object value );
public abstract Object getValue( Object object );
public Object newInstance( Object parent )
{
throw new UnsupportedOperationException();
}
public Object newInstance( Object parent, Object[] args )
{
throw new UnsupportedOperationException();
}
public void resetValue( Object object )
{
throw new UnsupportedOperationException();
}
}
In the mapping file, you specify:
<field name="enumField" type="EnumClass"
handler="MyEnumFieldHandler">...</field>
If anyone knows how to improve on this solution, I'd sure appreciate
their sharing it with this group!
Marko
>-----Original Message-----
>From: Letourneau, Carl [mailto:[EMAIL PROTECTED]
>Subject: Re: [castor-dev] typesafe enum in mapping file - how to?
>
>Is this possible to use a "type safe enum class" in a Mapping
>file? It keeps giving me error because I don't have a public
>default constructor on the class.
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev